Pranav Malaviya Java Practical 74
Pranav Malaviya Java Practical 74
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
{
BCA-(A)IMCA 1
IU2182820074 PRANAV MALAVIYA
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 2
IU2182820074 PRANAV MALAVIYA
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 3
IU2182820074 PRANAV MALAVIYA
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
{p
public static void main(String args[])
{
Friend f=new Friend();
f.display();
}
}
BCA-(A)IMCA 4
IU2182820074 PRANAV MALAVIYA
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);
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("Tanvi Chovatiya");
System.out.println("----One Argument Constructor----");
BCA-(A)IMCA 5
IU2182820074 PRANAV MALAVIYA
f1.display();
Friend f2=new Friend("Tanvi Chovatiya","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();
}
}
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();
System.out.print("Enter the Fees:-");
fee=sc.nextFloat();
}
void display()
{
System.out.println("Enrollment_no="+eno);
BCA-(A)IMCA 6
IU2182820074 PRANAV MALAVIYA
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 7
IU2182820074 PRANAV MALAVIYA
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");
}
}
}
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];
BCA-(A)IMCA 8
IU2182820074 PRANAV MALAVIYA
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");
}
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:-");
BCA-(A)IMCA 9
IU2182820074 PRANAV MALAVIYA
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++)
{
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 10
IU2182820074 PRANAV MALAVIYA
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();
}
}
public void percentageCalculation(){
for(i=1;i<=5;i++){
total = total + MOFS[i];
}
percent = total/500;
percent = percent*100;
}
public void gradeCalculation(){
BCA-(A)IMCA 11
IU2182820074 PRANAV MALAVIYA
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 12
IU2182820074 PRANAV MALAVIYA
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();
System.out.println("---------------------------");
}
}
public void pursell()
{
System.out.println("[1] Purchase Product");
System.out.println("[2] Sell Product To Store");
System.out.println("--------------------------------");
BCA-(A)IMCA 13
IU2182820074 PRANAV MALAVIYA
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[])
{
Product p = new Product();
p.pursell();
p.display();
}
}
BCA-(A)IMCA 14
IU2182820074 PRANAV MALAVIYA
16.Write a program that creates string variables called Name, father’s name, Surname and
myName. Using concatenation, assigns the value of Name, father’s name and surname into
myName, and displays it on screen
public class ExProgram16{
public static void main(String args[]){
String firstname = "malaviya pranav";
String fathername = "malaviya pranav";
String surname = "malaviya";
String myname = firstname + " " + fathername + " " + surname;
System.out.println("My Name: "+myname);
}
}
BCA-(A)IMCA 15
IU2182820074 PRANAV MALAVIYA
17.Write a program to input a sentence (with blank space) and display all characters and their
occurrence in inputted string. i.e. LIRIL -> then output like following I – 2 L – 2 R – 1
import java.util.*;
public class ExProgram17{
public static void main(String args[]){
String a;
int length, i;
int[] b = new int[256];
Scanner sc = new Scanner(System.in);
System.out.println("Enter String: ");
a = sc.nextLine();
length = a.length();
for(i=0;i<length;i++){
b[(int) a.charAt(i)]++;
}
for(i=0;i<256;i++) {
if (b[i] != 0) {
System.out.println((char) i + " --> " + b[i]);
}
}
}
}
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){
areaofrect = (float) length * breath;
}
public void display(){
System.out.println(" ");
System.out.println("Area Of Triangle: " + areaoftriangle);
System.out.println("Area Of Circle: " + areaofcircle);
BCA-(A)IMCA 16
IU2182820074 PRANAV MALAVIYA
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[]){
Box b = new Box();
b.Volume(1,3,7);
b.Volume(2.0F, 4);
b.display();
}
}
BCA-(A)IMCA 17
IU2182820074 PRANAV MALAVIYA
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[])
{
StringComparison.insert();
StringComparison.comparison();
StringComparison sc = new StringComparison();
sc.display();
}
}
BCA-(A)IMCA 18
IU2182820074 PRANAV MALAVIYA
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("Tanvi", "[email protected]", "Ahmedabad", 25,
1987654320);
}
}
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.
BCA-(A)IMCA 19
IU2182820074 PRANAV MALAVIYA
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);
}
}
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();
BCA-(A)IMCA 20
IU2182820074 PRANAV MALAVIYA
BCA-(A)IMCA 21
IU2182820074 PRANAV MALAVIYA
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.*;
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;
BCA-(A)IMCA 22
IU2182820074 PRANAV MALAVIYA
BCA-(A)IMCA 23
IU2182820074 PRANAV MALAVIYA
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;
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!");
BCA-(A)IMCA 24
IU2182820074 PRANAV MALAVIYA
break;
default:
System.out.println("Invalid choice!");
break;
}
} while (choice != 6);
}
}
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;
public void display()
{
System.out.println("Account No.: " + AccountNo);
System.out.println("Name: " + Name);
System.out.println("Amount: " + Amount);
}
public void newAccount()
BCA-(A)IMCA 25
IU2182820074 PRANAV MALAVIYA
{
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)");
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();
BCA-(A)IMCA 26
IU2182820074 PRANAV MALAVIYA
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);
}
}
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;
BCA-(A)IMCA 27
IU2182820074 PRANAV MALAVIYA
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)
{
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");
BCA-(A)IMCA 28
IU2182820074 PRANAV MALAVIYA
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);
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 29