You are on page 1of 32

IU2182820089 KUNAL NAGAR

1. Insert number check whether the number is negative,positive or zero.


import java.util.*;
class ExProgram1
{
public static void main(String args[])
{
Scanner sc;
sc= new Scanner(System.in);
int no;
System.out.println("Insert Number :- ");
no=sc.nextInt();
if(no>0)
{
System.out.println("number is positive");
}
else if(no<0)
{
System.out.println("number is negative");
}
else
{
System.out.println("number is zero");
}
}
}

2. Write a program to display input provided through command line arguments.


import java.util.*;
class Pro2
{
public static void main(String args[])
{
int x=0;
x= Integer.parseInt(args[0]);
System.out.println(x +" is the value of x");
}
}

3. Find factorial of a enter number. Check the entered number, if it’s negative then an error
message should print.
import java.util.*;
class Factorial
{
public static void main(String a[])
{

BCA-(A)IMCA (JAVA) 1
IU2182820089 KUNAL NAGAR

Scanner sc =new Scanner(System.in);


int n;
int i,ans=1;
System.out.print("Enter the n number:-");
n=sc.nextInt();
if(n<0)
{
System.out.println("Enter number is negative pleas enter positive number");
}
else
{
for(i=1;i<=n;i++)
{
ans=i*ans;
}
System.out.println("Factorial of n number is "+ ans);
}
}
}

4.Write a program to convert given no. of days into months, years and days; assume that each
month is of 30 days. For Example: if input is 69 than Output is 2 months and 9 days.
import java.util.*;
class Days
{
public static void main(String args[])
{
int n,month,year,days;
Scanner sc;
sc=new Scanner(System.in);
System.out.println("Enter days");
n=sc.nextInt();
year=n/365;
n=n%365;
System.out.println("years "+year);
month=n/30;
n=n%30;
System.out.println("month "+ month);
days=n;
System.out.println("Days "+days);
System.out.println("Output is:"+year+"years"+month+"month"+days+"days");
}
}

BCA-(A)IMCA (JAVA) 2
IU2182820089 KUNAL NAGAR

5. Write a program that prints the numbers between 1 and 100 which are divisible by 5 but not
divisible by 10.
import java.util.*;
class Divisible
{
public static void main(String args[])
{
Scanner sc;
sc=new Scanner(System.in);
int n,i=0;
System.out.println("Enter number");
n=sc.nextInt();
System.out.println(n);
for(i=1;i<=n;i++)
{
if(i%5==0 && i%10!=0)
{
System.out.println("numbers divisible by 5 only = " +i );
}
}
}
}

6. Write a program to calculate sum of even number till n numbers.[ N number is provided
through command line arguments] For Example: if input number is 20 then answer will be 20
because 2+4+6+8+10+12+14+16+18+20=110.
import java.util.*;
public class ExProgram6{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter No1: ");
int a = sc.nextInt();
int i=0,sum=0;
for(i=0;i<=a;i=i+2){
sum = sum+i;
System.out.println("All Even Numbers: "+i);
}
System.out.println("Sum Of All Even Numbers: "+sum);
}
}

BCA-(A)IMCA (JAVA) 3
IU2182820089 KUNAL NAGAR

7. Create class “Friend” which have following properties: Data Members: name, address,
mobile-no and email-id. Method: display. Write a program which will insert details of Friend
through no argument constructor and display information of it through display method.
import java.util.*;
class Friend
{
String name,add,email_id,mobile_no;
Friend()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the Name:-");
name=sc.nextLine();
System.out.print("Enter the Address:-");
add=sc.nextLine();
System.out.print("Enter the mobile_no:-");
mobile_no=sc.nextLine();
System.out.print("Enter the Email_id:-");
email_id=sc.nextLine();
}
void display()
{
System.out.println("Name="+name);
System.out.println("Address="+add);
System.out.println("Mobile_no="+mobile_no);
System.out.println("Email_no="+email_id);
}
}
public class Pro
{
public static void main(String args[])
{
Friend f=new Friend();
f.display();
}
}

BCA-(A)IMCA (JAVA) 4
IU2182820089 KUNAL NAGAR

8. Use above class structure and perform following task: 1. Create default constructor, one
argument constructor, two argument constructor and copy constructor. 2. Call all the above
created constructor one by one and display information of it through function.
import java.util.*;
class Friend
{
String name,add,email_id,mobile_no;
Friend()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the Name:-");
name=sc.nextLine();
System.out.print("Enter the Address:-");
add=sc.nextLine();
System.out.print("Enter the mobile_no:-");
mobile_no=sc.nextLine();
System.out.print("Enter the Email_id:-");
email_id=sc.nextLine();
}
Friend(String name1)
{
name=name1;
}
Friend(String name1,String add1)
{
name=name1;
add=add1;
}
Friend(Friend f5)
{
name=f5.name;
add=f5.add;
mobile_no=f5.mobile_no;
email_id=f5.email_id;
}
void display()
{
System.out.println("Name="+name);
System.out.println("Address="+add);
System.out.println("Mobile_no="+mobile_no);

BCA-(A)IMCA (JAVA) 5
IU2182820089 KUNAL NAGAR

System.out.println("Email_no="+email_id);
}
}
public class Pro
{
public static void main(String args[])
{
Friend f=new Friend();
System.out.println("----Default Constructor----");
f.display();
Friend f1=new Friend("kunal nagar");
System.out.println("----One Argument Constructor----");
f1.display();
Friend f2=new Friend("kunal nagar","Ahmedabad");
System.out.println("----Two Arument Constructor----");
f2.display();
Friend f3=new Friend(f);
System.out.println("----Copy Constructor----");
System.out.println("This Constructor copy the Default Constructor");
f3.display();
}
}

BCA-(A)IMCA (JAVA) 6
IU2182820089 KUNAL NAGAR

9. Create class “Student” which have following properties: Data Members: enrolmentNo,
name,course, and fee. Methods: insert and display. Write a program which will insert and
display student information through class methods.
import java.util.*;
class Student
{
String eno,name,add,course;
float fee;
void insert()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the Enrollment No:-");
eno=sc.nextLine();
System.out.print("Enter the Name:-");
name=sc.nextLine();
System.out.print("Enter the Course:-");
course=sc.nextLine();

BCA-(A)IMCA (JAVA) 7
IU2182820089 KUNAL NAGAR

System.out.print("Enter the Fees:-");


fee=sc.nextFloat();
}
void display()
{
System.out.println("Enrollment_no="+eno);
System.out.println("Name="+name);
System.out.println("Course="+course);
System.out.println("Fees="+fee);
}
}
public class Pro
{
public static void main(String args[])
{
Student s=new Student();
s.insert();
System.out.println("-----Display the Information-----");
s.display();
}
}

10. Write a program that read the elements of a one dimensional array print the sum of array
elements.
import java.util.*;
class Pro
{
public static void main(String args[])
{
int a[]=new int[5];
int i,sum=0;
Scanner sc=new Scanner(System.in);
for(i=0;i<a.length;i++)
{
System.out.print("Enter Element in a["+i+"]=");
a[i]=sc.nextInt();
sum=sum+a[i];
}
System.out.println("Sumation of array element is "+sum);

BCA-(A)IMCA (JAVA) 8
IU2182820089 KUNAL NAGAR

}
}

11. Write a program to display the products of two diagonals array.[Assume that the array size
is 3x3].
import java.util.*;
class Pro
{
public static void main(String args[])
{
int a[][] =new int[3][3];
int i,j;
Scanner sc=new Scanner(System.in);
for(i=0;i<a.length;i++)
{
for(j=0;j<a[i].length;j++)
{
System.out.print("Enter Element in a["+i+"]["+j+"]=");
a[i][j]=sc.nextInt();
}
}
System.out.println("Matrix A=");
for(i=0;i<a.length;i++)
{
for(j=0;j<a[i].length;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.print("\n");
}
}
}

BCA-(A)IMCA (JAVA) 9
IU2182820089 KUNAL NAGAR

12. Write a menu driven program using two dimensional arrays to perform matrix addition,
subtraction and multiplication.( Use switch case) The program asks the user to input the
numbers of rows and columns for matrix A and matrix B before entering the elements of each
array.
import java.util.*;
class Pro
{
public static void main(String args[])
{
int a[][] =new int[100][100];
int b[][]=new int [100][100];
int ans[][]=new int [100][100];
int i,j,k,r,c,choice;
Scanner sc=new Scanner(System.in);
System.out.print("How many Rows DO you want to add in A:-");
r=sc.nextInt();
System.out.print("How many Columns DO you want to add in A:-");
c=sc.nextInt();
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print("Enter Element in a["+i+"]["+j+"]=");
a[i][j]=sc.nextInt();
}
}
System.out.println("Matrix A=");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(a[i][j]+"\t");
}

BCA-(A)IMCA (JAVA) 10
IU2182820089 KUNAL NAGAR

System.out.print("\n");
}
System.out.print("How many Rows DO you want to add in B:-");
r=sc.nextInt();
System.out.print("How many Columns DO you want to add in B:-");
c=sc.nextInt();
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print("Enter Element in b["+i+"]["+j+"]=");
b[i][j]=sc.nextInt();
}
}
System.out.println("Matrix B=");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(b[i][j]+"\t");
}
System.out.print("\n");
}
do
{
System.out.print("\n\n");
System.out.println("1.Addition");
System.out.println("2.Subtraction.");
System.out.println("3. Multiplication.");
System.out.println("4.Exit.");
System.out.println("Enter your choice:-");
choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.println("Additoion of Matrix A and B is :-");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
ans[i][j]=a[i][j]+b[i][j];
System.out.print(ans[i][j]+"\t");
}
System.out.print("\n");
}
break;
case 2:
System.out.println("Subtraction of Matrix A and B is :-");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{

BCA-(A)IMCA (JAVA) 11
IU2182820089 KUNAL NAGAR

ans[i][j]=a[i][j]-b[i][j];
System.out.print(ans[i][j]+"\t");
}
System.out.print("\n");
}
break;
case 3:
System.out.println("Multiplecation of Matrix A and B is :-");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
for(k=0;k<c;k++)
{
ans[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(ans[i][j]+"\t");
}
System.out.print("\n");
}
break;
}
}while(choice!=4);
}
}

BCA-(A)IMCA (JAVA) 12
IU2182820089 KUNAL NAGAR

13. Create class Student which have following properties: Data Members: EnrollmentNo,
Name, Course, Marks of five subjects, Total and percentage and Grade. Methods: insert,
percentageCalculation, gradeCalculation, resultGeneration. Write a program which will display
students result according to grade wise. [Note: insert() is used for insertion which will call
percentageCalculation method; after calculating percentage it will call gradeCalculation(); after
calculating grade it will call resultGeneration() for displaying the result of students. Use the
concept of “array of object”]
import java.util.*;
class Student{
int EnrollmentNo, i;
float percent, total;
String grade;
int[] MOFS = new int[6];
String Name;
String Course;
public void insert(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Name: ");
Name = sc.nextLine();
System.out.println("Enter Course: ");
Course = sc.nextLine();
System.out.println("Enter Enrollment No: ");
EnrollmentNo = sc.nextInt();
System.out.println("Enter Marks Of 5 Subject: ");
for(i=1;i<=5;i++){
System.out.println("Enter "+i+" Subject: ");
MOFS[i] = sc.nextInt();

BCA-(A)IMCA (JAVA) 13
IU2182820089 KUNAL NAGAR

}
}
public void percentageCalculation(){
for(i=1;i<=5;i++){
total = total + MOFS[i];
}
percent = total/500;
percent = percent*100;
}
public void gradeCalculation(){
if(percent>=90){
grade = "A";
}
if(percent>=80 && percent<=89){
grade = "B";
}
if(percent>=70 && percent<=79){
grade = "C";
}
if(percent>=60 && percent<=69){
grade = "D";
}
if(percent>=0 && percent<=59){
grade = "F";
}
}
public void resultGeneration(){
System.out.println("================================================");
System.out.println("RESULT");
System.out.println("-------------------------------");
System.out.println("Name Of Student: " + Name);
System.out.println("Student Enrollment No: " + EnrollmentNo);
System.out.println("Course: " + Course);
System.out.println("------------");
for(i=1;i<=5;i++) {
System.out.println("Marks Of "+i+" Subject Is: " + MOFS[i]);
}
System.out.println("------------");
System.out.println("Total Of All Subject Is: " + total);
System.out.println("Total Percentage: " + percent + "%");
System.out.println("Grade: " + grade);
System.out.println("===============================================");
}
}
public class Pro13{
public static void main(String args[]){
Student s = new Student();
s.insert();
s.percentageCalculation();
s.gradeCalculation();
s.resultGeneration();
}

BCA-(A)IMCA (JAVA) 14
IU2182820089 KUNAL NAGAR

14.Create class Product which have following properties: Data Members: ProductNo, Name,
quantity and price per quantity. Methods: purchase, sale and display. Write a menu driven
program which ask end user whether he/she want to purchase, or sale product; according to
the user choice perform operation and make necessary updation in quantity. Note: before
giving choice to user insert minimum five product information through constructor. (Use the
concept of array of object)
import java.util.*;
class Product
{
int i=1, choose = 0, p;
int[] quantity = new int[7];
int[] productno = new int[7];
int[] PPQ = new int[7];
String[] name = new String[7];
Scanner sc = new Scanner(System.in);
Product()
{
for (i = 1; i <= 3; i++)
{
System.out.println("Info " + i);
System.out.print("Enter Name: ");
name[i] = sc.next();
System.out.print("Enter Product No: ");
productno[i] = sc.nextInt();
System.out.print("Enter Quantity: ");
quantity[i] = sc.nextInt();
System.out.print("Enter Price Per Quantity: ");
PPQ[i] = sc.nextInt();

BCA-(A)IMCA (JAVA) 15
IU2182820089 KUNAL NAGAR

System.out.println("---------------------------");
}
}
public void pursell()
{
System.out.println("[1] Purchase Product");
System.out.println("[2] Sell Product To Store");
System.out.println("--------------------------------");
System.out.println("Enter Choice:");
choose = sc.nextInt();
switch (choose)
{
case 1:
System.out.println("Enter Product No To Purchase");
p = sc.nextInt();
for(i=1;i<=3;i++)
{
if(p==productno[i])
{
quantity[i] = quantity[i]-1;
}
}
break;
case 2:
System.out.println("Enter Product No To Sell Your Product: ");
p = sc.nextInt();
for(i=1;i<=3;i++)
{
if(p==productno[i])
{
quantity[i] = quantity[i]+1;
}
}
break;
}
}
public void display()
{
for (i = 1; i <= 3; i++)
{
System.out.print("Name: "+ name[i]);
System.out.print(" ,Product No: "+ productno[i]);
System.out.print(" ,Quantity: "+ quantity[i]);
System.out.print(" ,Price Per Quantity: " + PPQ[i]);
System.out.println();
}
}
}
public class Pro14
{
public static void main(String args[])
{

BCA-(A)IMCA (JAVA) 16
IU2182820089 KUNAL NAGAR

Product p = new Product();


p.pursell();
p.display();
}
}

18.Create class ShapeArea which will calculate area of different geographical shape like
triangle, circle, square, rectangle, etc. Write a program which will calculate and display the
area of shapes using the concept of method Overloading.
class ShapeArea{
int height,breath,areaoftriangle, side, areaofsquare;
float areaofcircle, radius, length, areaofrect;
public void areaofclass(int height, int breath){
areaoftriangle = (breath * height)/2;
}
public void areaofclass(float radius){
areaofcircle = (float) 3.14 * radius * radius;
}
public void areaofclass(int side){
areaofsquare = side * side;
}
public void areaofclass(float length, int breath){

BCA-(A)IMCA (JAVA) 17
IU2182820089 KUNAL NAGAR

areaofrect = (float) length * breath;


}
public void display(){
System.out.println(" ");
System.out.println("Area Of Triangle: " + areaoftriangle);
System.out.println("Area Of Circle: " + areaofcircle);
System.out.println("Area Of Square: " + areaofsquare);
System.out.println("Area Of Rectangle: " + areaofrect);
System.out.println(" ");
}
}
public class ExProgram18{
public static void main(String args[]){
ShapeArea sp = new ShapeArea();
sp.areaofclass(5,6);
sp.areaofclass(5.0F);
sp.areaofclass(2);
sp.areaofclass(7.0F,8);
sp.display();
}
}

19. Write a program which will use the concept of method overloading and calculate volume of
a Box. Box class have following properties: Data Members: Width Height Depth Method:
volume() having three argument volume() having two argument display() Note: volume =
(width*height*depth)
class Box{
int height, width, volume1, depth = 9, volume2;
float width1;
public void Volume(int height, int width, int depth){
volume1 = height * width * depth;
}
public void Volume(float width1, int height) {
float temp = (float) height * width1;
volume2 = (int) temp * depth;
}
public void display(){
System.out.println("=======================");
System.out.println("Volume Of Box Using Three Argument: " + volume1);
System.out.println("Volume Of Box Using Two Argument: " + volume2);
System.out.println("=======================");
}
}
public class ExProgram19{
public static void main(String args[]){

BCA-(A)IMCA (JAVA) 18
IU2182820089 KUNAL NAGAR

Box b = new Box();


b.Volume(1,3,7);
b.Volume(2.0F, 4);
b.display();
}
}

20.Write a program to create class StringComparison in which data members are str1 and str2
of static String type, methods of a class are insert, comparison and display; insert and
comparison are static method. After insertion of a two string; call comparison method from
insert method which will check whether the string is equal or not after checking call display
method from comparison method for displaying appropriate result.
import java.util.*;
class StringComparison
{
static String str1;
static String str2;
static String a;
static void insert()
{
Scanner sc = new
Scanner(System.in);
System.out.println("Enter String 1: ");
str1 = sc.nextLine();
System.out.println("Enter String 2: ");
str2 = sc.nextLine();
}
static void comparison()
{
if(str1.equals(str2))
{
a = "Are Same.";
}
else
{
a = "Are Not Same.";
}
}
public void display()
{
System.out.println("String 1 And 2 " + a);
}
}
public class Pro20
{
public static void main(String args[])
{

BCA-(A)IMCA (JAVA) 19
IU2182820089 KUNAL NAGAR

StringComparison.insert();
StringComparison.comparison();
StringComparison sc = new StringComparison();
sc.display();
}
}

21.Create two class one is Friend and another is Buddy, Friend class has data member like
name and email-id, and Buddy class inherit the properties of Friend class and it has data
member like dob, mobile no and address and do the following operation. 1. Create three
constructor for base and derived class. 2. Insert data through derived class parameterized
constructor. 3. Illustrate the use of this reference and Super. 4. Display all the information
through parent class reference variable.
import java.util.*;
class Friend
{
String name;
String emailid;
Friend(){}
}
class Buddy extends Friend
{
int dob;
long mobileno;
String address;
Buddy(String name, String emailid, String address, int dob, long mobileno)
{
System.out.println("Name: " + name);
System.out.println("Email-ID: " + emailid);
System.out.println("Address: " + address);
System.out.println("DOB: " + dob);
System.out.println("Mobile-No: " + mobileno);
}
}
public class Pro21
{
public static void main(String args[])
{
Buddy b = new Buddy("", "nagar kunal@gmail.com", "Ahmedabad", 25, 1987654320);

}
}

BCA-(A)IMCA (JAVA) 20
IU2182820089 KUNAL NAGAR

22.Create three class Vehicle, TwoWheeler and FourWheeler. Vehicle is a parent class of
TwoWheeler and FourWheeler. In Vehicle class data member is company name, and methods
are input and display. In TwoWheelers class, data members are name, type(gear, non gear);
and methods are input and display. In FourWheelers data members are name, model no, fuel
type; and methods are input and display. Write a program which will input and display
information of two wheeler and four wheeler using the concept of method overriding.
import java.util.*;
class Vehicle
{
String companyname;
Scanner sc = new Scanner(System.in);
void insert()
{
System.out.println("Enter Company Name: ");
companyname = sc.nextLine();
}
void display(
){
System.out.println();
System.out.println(" ");
System.out.println("Company Name: " + companyname);
}
}
class TwoWheelers extends Vehicle
{
String name;
String type;
void insert()
{
System.out.println(" ");
System.out.println("Two Wheelers");
System.out.println(" ");
System.out.println("Enter Name: ");
name = sc.nextLine();
System.out.println("Enter Type(Gear Or Non-Gear): ");
type = sc.nextLine();
}
void display()
{
System.out.println(" ");
System.out.println("Two Wheelers");
System.out.println(" ");
System.out.println("Name: " + name);
System.out.println("Type: " + type);

BCA-(A)IMCA (JAVA) 21
IU2182820089 KUNAL NAGAR

}
}
class FourWheelers extends Vehicle
{
String fwname;
int modeltype;
String fueltype;
void insert()
{
System.out.println(" ");
System.out.println("Four Wheelers");
System.out.println(" ");
System.out.println("Enter Name: ");
fwname = sc.nextLine();
System.out.println("Enter Fuel Type: ");
fueltype = sc.nextLine();
System.out.println("Enter Model Type: ");
modeltype = sc.nextInt();
}
void display()
{
System.out.println(" ");
System.out.println("Four Wheelers");
System.out.println(" ");
System.out.println("Name: " + fwname);
System.out.println("Model Type: " + modeltype);
System.out.println("Fuel Type: " + fueltype);
System.out.println(" ");
}
}
public class Pro22{
public static void main(String args[])
{
FourWheelers fw = new FourWheelers();
TwoWheelers tw = new TwoWheelers();
Vehicle v = new Vehicle();
v.insert();
tw.insert();
fw.insert();
v.display();
tw.display();
fw.display();
}
}

BCA-(A)IMCA (JAVA) 22
IU2182820089 KUNAL NAGAR

23.Create three class Employee, Teaching, Nonteaching; In Employee class data members are
employee no, name and there are two abstract methods set and get. Teaching class inherit the
properties of Employee and its data members are course, designation and salary. Nonteaching
class inherits the properties of Employee class and its data members are department,
designation and salary. Write a program to display employee information according to
designation wise.
import java.util.*;

BCA-(A)IMCA (JAVA) 23
IU2182820089 KUNAL NAGAR

abstract class Employee


{
private int empNo;
private String name;
public void set()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter employee no.: ");
empNo = sc.nextInt();
sc.nextLine();
System.out.println("Enter name: ");
name = sc.nextLine();
}
public void get()
{
System.out.println("Employee No.: " + empNo);
System.out.println("Name: " + name);
}
}
class Teaching extends Employee
{
private String course;
private String designation;
private double salary;
public void set()
{
super.set();
Scanner sc = new Scanner(System.in);
System.out.println("Enter course: ");
course = sc.nextLine();
System.out.println("Enter designation: ");
designation = sc.nextLine();
System.out.println("Enter salary: ");
salary = sc.nextDouble();
}
public void get()
{
super.get();
System.out.println("Course: " + course);
System.out.println("Designation: " + designation);
System.out.println("Salary: " + salary);
}
}
class Nonteaching extends Employee
{
private String department;
private String designation;
private double salary;
public void set()
{
super.set();
Scanner sc = new Scanner(System.in);

BCA-(A)IMCA (JAVA) 24
IU2182820089 KUNAL NAGAR

System.out.println("Enter department: ");


department = sc.nextLine();
System.out.println("Enter designation: ");
designation = sc.nextLine();
System.out.println("Enter salary: ");
salary = sc.nextDouble();
}
public void get()
{
super.get();
System.out.println("Department: " + department);
System.out.println("Designation: " + designation);
System.out.println("Salary: " + salary);
}
}
public class Pro23
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int choice;
List<Employee> employees = new ArrayList<Employee>();
do
{
System.out.println(" ");
System.out.println("1. Add teaching employee");
System.out.println("2. Add non-teaching employee");
System.out.println("3. Display all employees");
System.out.println("4. Display teaching employees");
System.out.println("5. Display non-teaching employees");
System.out.println("6. Exit");
System.out.println("Enter your choice: ");
choice = sc.nextInt();
switch (choice)
{
case 1:
Teaching t = new Teaching();
t.set();
employees.add(t);
break;
case 2:
Nonteaching nt = new Nonteaching();
nt.set();
employees.add(nt);
break;
case 3:
for (Employee e : employees)
{
e.get();
System.out.println();
}
break;

BCA-(A)IMCA (JAVA) 25
IU2182820089 KUNAL NAGAR

case 4:
for (Employee e : employees)
{
if (e instanceof Teaching)
{
e.get();
System.out.println();
}
}
break;
case 5:
for (Employee e : employees)
{
if (e instanceof Nonteaching)
{
e.get();
System.out.println();
}
}
break;
case 6:
System.out.println("Exiting program... Thank you!");
break;
default:
System.out.println("Invalid choice!");
break;
}
} while (choice != 6);
}
}

BCA-(A)IMCA (JAVA) 26
IU2182820089 KUNAL NAGAR

24.Create two interface PrivateBank and GovernmentBank and one class Customer.
PrivateBank interface have three methods newAccount, withdrawAmount and
depositeAmount. GovernmentBank have three methods newAccount, withdrawAmount and
depositeAmount. Customer class inherit the properties of PrivateBank and GovernmentBank
and have data members like AccountNo, Name, and Amount and has method display. Write a
menu driven program to perform banking operations like open account, withdraw and deposit
amount for private as well as government bank.
import java.util.*;
interface PrivateBank
{
public void newAccount();
public void withdrawAmount();
public void depositeAmount();
}
interface GovernmentBank
{
public void newAccount();
public void withdrawAmount();
public void depositeAmount();
}
class Customer implements PrivateBank, GovernmentBank
{
private int AccountNo;
private String Name;
private double Amount;

BCA-(A)IMCA (JAVA) 27
IU2182820089 KUNAL NAGAR

public void display()


{
System.out.println("Account No.: " + AccountNo);
System.out.println("Name: " + Name);
System.out.println("Amount: " + Amount);
}
public void newAccount()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Account No.: ");
AccountNo = sc.nextInt();
sc.nextLine();
System.out.println("Enter Name: ");
Name = sc.nextLine();
System.out.println("Enter Amount: ");
Amount = sc.nextDouble();
}
public void withdrawAmount()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter amount to be withdrawn: ");
double amount = sc.nextDouble();
if (Amount < amount)
{
System.out.println("Insufficient balance");
return;
}
Amount -= amount;
System.out.println("Amount withdrawn successfully");
}
public void depositeAmount()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter amount to be deposited: ");
double amount = sc.nextDouble();
Amount += amount;
System.out.println("Amount deposited successfully");
}
}
public class Pro24
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int choice;
Customer c = new Customer();
do
{
System.out.println(" ");
System.out.println("1. Open account (Private Bank)");
System.out.println("2. Withdraw amount (Private Bank)");
System.out.println("3. Deposit amount (Private Bank)");

BCA-(A)IMCA (JAVA) 28
IU2182820089 KUNAL NAGAR

System.out.println("4. Open account (Government Bank)");


System.out.println("5. Withdraw amount (Government Bank)");
System.out.println("6. Deposit amount (Government Bank)");
System.out.println("7. Display account details");
System.out.println("8. Exit");
System.out.println("Enter your choice: ");
choice = sc.nextInt();
switch (choice)
{
case 1:
c.newAccount();
break;
case 2:
c.withdrawAmount();
break;
case 3:
c.depositeAmount();
break;
case 4:
c.newAccount();
break;
case 5:
c.withdrawAmount();
break;
case 6:
c.depositeAmount();
break;
case 7:
c.display();
break;
case 8:
System.out.println("Exiting program... Thank you!");
break;
default:
System.out.println("Invalid choice!");
break;
}
} while (choice != 8);
}
}

BCA-(A)IMCA (JAVA) 29
IU2182820089 KUNAL NAGAR

25.Create three interfaces and one class called Hockey, Cricket, Tennis and Player
respectively. Player class inherits the properties of Hockey, Cricket and Tennis. In Hockey;
method is h(), in Cricket;method is c(),and in Tennis method is t(); all the methods insert no of
player required for playing the particular game. Player class have data members player_name
and type_of_sport and method insertInfo() and display(). Write a menu driven program which
will ask user choice for the sports and according to user choice call appropriate method to
insert no of player required. After getting the information of sports and no of player required
call insertInfo() which will take players name. After performing all the necessary operation
display information of sports and player in a proper format.
import java.util.Scanner;
interface Hockey
{
void h(int players);
}
interface Cricket
{
void c(int players);
}
interface Tennis
{
void t(int players);
}
class Player implements Hockey, Cricket, Tennis
{
String player_name;
String type_of_sport;
public void h(int players)

BCA-(A)IMCA (JAVA) 30
IU2182820089 KUNAL NAGAR

{
System.out.println("Number of players required for hockey: " + players);
}
public void c(int players)
{
System.out.println("Number of players required for cricket: " + players);
}
public void t(int players)
{
System.out.println("Number of players required for tennis: " + players);
}
public void insertInfo(String player_name, String type_of_sport)
{
this.player_name = player_name;
this.type_of_sport = type_of_sport;
}
public void display()
{
System.out.println("Player Name: " + player_name);
System.out.println("Type of Sport: " + type_of_sport);
}
}
public class Pro25
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
Player player = new Player();
String player_name, type_of_sport;
int players;
while (true)
{
System.out.println("\nSelect a sport:");
System.out.println("1. Hockey");
System.out.println("2. Cricket");
System.out.println("3. Tennis");
System.out.println("4. Exit");
int choice = scanner.nextInt();
if (choice == 4)
{
break;
}
System.out.println("Enter the number of players required:");
players = scanner.nextInt();
switch (choice)
{
case 1:
player.h(players);
type_of_sport = "Hockey";
break;
case 2:
player.c(players);

BCA-(A)IMCA (JAVA) 31
IU2182820089 KUNAL NAGAR

type_of_sport = "Cricket";
break;
case 3:
player.t(players);
type_of_sport = "Tennis";
break;
default:
System.out.println("Invalid choice");
continue;
}
System.out.println("Enter player name:");
player_name = scanner.next();
player.insertInfo(player_name, type_of_sport);
}
System.out.println("\nPlayer information:");
player.display();
}
}

BCA-(A)IMCA (JAVA) 32

You might also like