1 15
1 15
Write an application in Java that prompts the user for the radius of a circle and uses a
method called circleArea to calculate the area of the circle and uses a method circlePerimeter
to calculate the perimeter of the circle.
import java.util.Scanner;
class Cal
void circleArea(double r)
System.out.println("Area:"+3.14*r*r);
System.out.println("Perimeter:"+2*3.14*r);
class ques_1
System.out.println("Enter r:");
double r=sc.nextDouble();
Cal c=new Cal();
c.circleArea(r);
c.circlePerimeter(r);
2. Write an application in Java to create a super class Employee with information first name
& last name and methods getFirstName(), getLastName() derive the sub-classes
ContractEmployee and RegularEmployee with the information about department, designation
& method displayFullName() , getDepartment(), getDesig() to print the salary and to set
department name & designation of the corresponding sub-class objects respectively.
import java.io.*;
import java.util.Scanner;
class Employee
{
private String firstName;
private String lastName;
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public void setFirstName(String firstName)
{
this.firstName=firstName;
}
public void setLastName(String lastName)
{
this.lastName=lastName;
}
}
class ContractEmployee extends Employee
{
String depart;
String desig;
double salary;
public void setDepartment(String depart)
{
this.depart=depart;
}
public void setDesignation(String desig)
{
this.desig=desig;
}
public void setSalary(double salary)
{
this.salary=salary;
}
public String getDepartment()
{
return depart;
}
public String getDesignation()
{
return desig;
}
public double getSalary()
{
return salary;
}
public void dispFullName()
{
System.out.println(getFirstName()+getLastName());
}
}
class RegularEmployee extends Employee
{
String depart;
String desig;
double salary;
public void setDepartment(String depart)
{
this.depart=depart;
}
public void setDesignation(String desig)
{
this.desig=desig;
}
public void setSalary(double salary)
{
this.salary=salary;
}
public String getDepartment()
{
return depart;
}
public String getDesignation()
{
return desig;
}
public double getSalary()
{
return salary;
}
public void dispFullName()
{
System.out.print(getFirstName() + " " + getLastName());
}
}
class ques_2
{
public static void main(String args[])
{
int ch;
Scanner sc=new Scanner(System.in);
do
{
System.out.println("\nEMPLOYEE\n1.Regular Employee\n2.Contract Employee\
n3.Exit");
System.out.println("Enter choice:");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Regular Employee");
RegularEmployee rg=new RegularEmployee();
System.out.println("Enter firstname: ");
String fn2=sc.nextLine();
String fn=sc.nextLine();
System.out.println("Enter lastname: ");
String ln=sc.nextLine();
System.out.println("Enter Department: ");
String d=sc.nextLine();
System.out.println("Enter Designation: ");
String de=sc.nextLine();
System.out.println("Enter Salary: ");
Long s=sc.nextLong();
rg.setFirstName(fn);
rg.setLastName(ln);
rg.setDepartment(d);
rg.setDesignation(de);
rg.setSalary(s);
System.out.println("First Name : "+rg.getFirstName());
System.out.println("Last Name : "+rg.getLastName());
System.out.println("Department : "+rg.getDepartment());
System.out.println("Designation : "+rg.getDesignation());
System.out.println("Salary : "+rg.getSalary());
System.out.print("Fullname : ");
rg.dispFullName();
break;
case 2:
System.out.println("Contract Employee");
ContractEmployee ce=new ContractEmployee();
System.out.println("Enter firstname: ");
String fn22=sc.nextLine();
String fn1=sc.nextLine();
System.out.println("Enter lastname: ");
String ln1=sc.nextLine();
System.out.println("Enter Department: ");
String d1=sc.nextLine();
System.out.println("Enter Designation: ");
String de1=sc.nextLine();
System.out.println("Enter Salary: ");
Long s1=sc.nextLong();
ce.setFirstName(fn1);
ce.setLastName(ln1);
ce.setDepartment(d1);
ce.setDesignation(de1);
ce.setSalary(s1);
System.out.println("First Name : "+ce.getFirstName());
System.out.println("Last Name : "+ce.getLastName());
System.out.println("Department : "+ce.getDepartment());
System.out.println("Designation : "+ce.getDesignation());
System.out.println("Salary : "+ce.getSalary());
System.out.print("Fullname : ");
ce.dispFullName();
break;
case 3:
System.out.println("Exiting");
break;
}
}while(ch!=3);
}
}
3. Write an application in Java to create a super class Vehicle with information vehicle
number,insurance number,color and methods getConsumption() displayConsumption().
Derive the sub-classes TwoWheeler and FourWheeler with method maintenance() and
average() to print the maintenance and average of vehicle
import java.util.Scanner;
class Vehicle
{
int vno;
int ino;
String color;
double fuel;
Vehicle(int vno,int ino,String color)
{
this.vno=vno;
this.ino=ino;
this.color=color;
}
void getConsumption(double fuel)
{
this.fuel=fuel;
}
void displayConsumption()
{
System.out.println("Fuel Comsumption : "+fuel);
}
}
class TwoWheeler extends Vehicle
{
double avg;
double mt;
TwoWheeler(double avg,double mt,int vno,int ino,String color)
{
super(vno,ino,color);
this.avg=avg;
this.mt=mt;
}
double maintenance()
{
return mt;
}
double average()
{
return avg;
}
}
class FourWheeler extends Vehicle
{
double avg;
double mt;
FourWheeler(double avg,double mt,int vno,int ino,String color)
{
super(vno,ino,color);
this.avg=avg;
this.mt=mt;
}
double maintenance()
{
return mt;
}
double average()
{
return avg;
}
}
class ques_3
{
public static void main(String args[])
{
int ch;
Scanner sc=new Scanner(System.in);
do
{
System.out.println("\nMENU\n1.Two Wheeler\n2.Four Wheeler");
System.out.println("Enter Choice: ");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter Vehicle No:");
int vn=sc.nextInt();
System.out.println("Enter Insurance No:");
int in=sc.nextInt();
System.out.println("Enter Color:");
String c9=sc.nextLine();
String c=sc.nextLine();
System.out.println("Enter Consumption:");
double c1=sc.nextDouble();
//Vehicle v=new Vehicle(vn,in,c);
System.out.println("Enter Average:");
double a=sc.nextDouble();
System.out.println("Enter Maintanace:");
double m=sc.nextDouble();
TwoWheeler tw=new TwoWheeler(a,m,vn,in,c);
tw.getConsumption(c1);
System.out.println("Vehicle no: "+tw.vno);
System.out.println("Insurance no: "+tw.ino);
System.out.println("Color: "+tw.color);
tw.displayConsumption();
System.out.println("Maintenance: "+tw.maintenance());
System.out.println("Average: "+tw.average());
break;
case 2:
System.out.println("Enter Vehicle No:");
int vn1=sc.nextInt();
System.out.println("Enter Insurance No:");
int in1=sc.nextInt();
System.out.println("Enter Color:");
String c8=sc.nextLine();
String c11=sc.nextLine();
System.out.println("Enter Consumption:");
double c111=sc.nextDouble();
System.out.println("Enter Average:");
double a1=sc.nextDouble();
System.out.println("Enter Maintanace:");
double m1=sc.nextDouble();
FourWheeler fw=new FourWheeler(a1,m1,vn1,in1,c11);
fw.getConsumption(c111);
System.out.println("Vehicle no: "+fw.vno);
System.out.println("Insurance no: "+fw.ino);
System.out.println("Color: "+fw.color);
fw.displayConsumption();
System.out.println("Maintenance: "+fw.maintenance());
System.out.println("Average: "+fw.average());
break;
case 3:
System.out.println("Exiting");
break;
}
}while(ch!=3);
}
}
4. . Using Java create an abstract class Shape which calculate the area and volume of 2-d and
3-d shapes with methods getArea() and getVolume(). Reuse this class to calculate the area
and volume of square ,circle ,cube and sphere.
import java.util.*;
abstract class Shapes
{
public abstract void getArea();
public abstract void getVolume();
}
class Dimensions extends Shapes
{
Scanner s=new Scanner(System.in);
public void getArea()
{
double As,Ac,l,r;
System.out.println("***AREA***");
System.out.println("Square");
System.out.println("PLease enter side");
l=s.nextDouble();
As=4*l;
System.out.println("Area of Square = "+As);
System.out.println("Circle");
System.out.println("Please enter radius");
r=s.nextDouble();
Ac=3.14*r*r;
System.out.println("Area of Circle = "+Ac);
}
public void getVolume()
{double Vcb,Vsp,sd,R;
System.out.println("\n***VOLUME***");
System.out.println("Cube");
System.out.println("Please enter side");
sd=s.nextDouble();
Vcb=sd*sd*sd;
System.out.println("Volume of cube = "+Vcb);
System.out.println("Sphere");
System.out.println("Please enter radius");
R=s.nextDouble();Vsp=(4*3.14*R*R*R)/3;
System.out.println("Volume of Sphere = "+Vsp);
}
}
class ques_4
{
public static void main(String sb[])
{
Dimensions d=new Dimensions();
d.getArea();
d.getVolume();
}
}
5. In Java, create an abstract class Employee with methods getAmount() which displays the
amount paid to employee. Reuse this class to calculate the amount to be paid to
WeeklyEmployeed and HourlyEmployee according to no. of hours and total hours for
HourlyEmployee and no. of weeks and total weeks for WeeklyEmployee
import java.util.*;
abstract class Employee
{
public abstract void getAmount();
}
class Amount extends Employee
{
int ch=0;
Scanner s=new Scanner(System.in);
public void getAmount()
{
double Hramt,Wkamt,hrunit=5000.00,wkunit=35000.00,hrswork,weekwork;
do
{
System.out.println("Press#\n 1. For Hourly Salary\n 2. For Weekly Salary\n3.Exit");
System.out.println("Enter your choice:");
ch=s.nextInt();
switch(ch)
{
case 1:
{
System.out.println("***Amount paid to Hourly Employee***");
System.out.println("Please enter Number of Hours Worked");
hrswork=s.nextDouble();
Hramt=hrunit*hrswork;
System.out.println("Hourly amount to be paid to Employee "+Hramt);
break;
}
case 2:
{
System.out.println("\n***Amount paid to weekly Employee***");
System.out.println("Please enter Number of Weeks Worked");
weekwork=s.nextDouble();
Wkamt=wkunit*weekwork;
System.out.println("Weekly amount to be paid = Employee "+Wkamt);
break;
}
default:
System.out.println("You have entered wrong choice...");
}
}while(ch!=3);
}
}
class ques_5
{
public static void main(String agrs[])
{
Amount obj=new Amount();
obj.getAmount();
}
}
interface Vehicle
{
public String getColor();
public int getNumbers();
public double getConsumption();
}
class TwoWheeler implements Vehicle
{
String name;
String color;
int number;
double fuelcon;
void setData(String name,String color,int number,double fuelcon)
{
this.name=name;
this.color=color;
this.number=number;
this.fuelcon=fuelcon;
}
public String getName()
{
return name;
}
public String getColor()
{
return color;
}
public int getNumbers()
{
return number;
}
public double getConsumption()
{
return fuelcon;
}
}
class FourWheeler implements Vehicle
{
String name;
String color;
int number;
double fuelcon;
void setData(String name,String color,int number,double fuelcon)
{
this.name=name;
this.color=color;
this.number=number;
this.fuelcon=fuelcon;
}
public String getName()
{
return name;
}
public String getColor()
{
return color;
}
public int getNumbers()
{
return number;
}
public double getConsumption()
{
return fuelcon;
}
}
class VehicleInterface
{
public static void main(String args[])
{
TwoWheeler ob=new TwoWheeler();
ob.setData("Splendour","Black Blue",1490,500.12);
String name=ob.getName();
String color=ob.getColor();
int num=ob.getNumbers();
double cons=ob.getConsumption();
System.out.println("\n Name : "+name+"\n Color : "+color+"\n Number : "+num+"\n
Consumption : "+cons);
FourWheeler ob1=new FourWheeler();
ob1.setData("Buggatti","Vibrant Red",9900,80000);
name=ob1.getName();
color=ob1.getColor();
num=ob1.getNumbers();
cons=ob1.getConsumption();
System.out.println("\n Name : "+name+"\n Color : "+color+"\n Number : "+num+"\n
Consumption : "+cons);
}
}
}
class Hostler implements StudentFee
{
String fname;
String lname;
String address;
String contact;
double amount;
void setData(String fname,String lname,String address,String contact,double amount)
{
this.fname=fname;
this.lname=lname;
this.address=address;
this.contact=contact;
this.amount=amount;
}
public double getAmount()
{
return amount;
}
public String getFirstName()
{
return fname;
}
public String getLastName()
{
return lname;
}
public String getAddress()
{
return address;
}
public String getContact()
{
return contact;
}
}
class NonHostler implements StudentFee
{
String fname;
String lname;
String address;
String contact;
double amount;
void setData(String fname,String lname,String address,String contact,double amount)
{
this.fname=fname;
this.lname=lname;
this.address=address;
this.contact=contact;
this.amount=amount;
}
public double getAmount()
{
return amount;
}
public String getFirstName()
{
return fname;
}
public String getLastName()
{
return lname;
}
public String getAddress()
{
return address;
}
public String getContact()
{
return contact;
}
}
class StudentFeeInterface
{
public static void main(String args[])
{
Hostler h=new Hostler();
h.setData("Deepak","Tiwari","Sagar","9584620767",(10000+50000));
double amount=h.getAmount();
String fnm=h.getFirstName();
String lnm=h.getLastName();
String address=h.getAddress();
String contact=h.getContact();
System.out.println("\nFirst Name : "+fnm+"\nLast Name : "+lnm+"\nAddress:
"+address+"\nContact : "+contact+"\n Amount :"+amount);
NonHostler nh=new NonHostler();
nh.setData("Chitrank","Dixit","Indore","9893194000",(50000));
amount=nh.getAmount();
fnm=nh.getFirstName();
lnm=nh.getLastName();
address=nh.getAddress();
contact=nh.getContact();
System.out.println("\nFirst Name : "+fnm+"\nLast Name : "+lnm+"\nAddress:
"+address+"\nContact : "+contact+"\n Amount :"+amount);
}
}
8. Write a Java program to create package called dept. Create four classes as CSE, ECE, ME
and CE add methods in each class which can display subject names of your respect year.
access this package classes from main class
package dept;
package dept;
public class ECE
{
public void displaySubjects()
{
System.out.println("Subjects for ECE year 1: Maths, Physics, Chemistry");
System.out.println("Subjects for ECE year 2: Analog Electronics, Digital Electronics,
Signals and Systems");
System.out.println("Subjects for ECE year 3: Communication Systems, Control
Systems, Microprocessors");
System.out.println("Subjects for ECE year 4: VLSI Design, Antenna and Wave
Propagation, Optical Communications");
}
}
package dept;
public class ME
{
public void displaySubjects()
{
System.out.println("Subjects for ME year 1: Maths, Physics, Chemistry");
System.out.println("Subjects for ME year 2: Mechanics, Thermodynamics,
Manufacturing Technology");
System.out.println("Subjects for ME year 3: Fluid Mechanics, Heat Transfer, Power
Plant Engineering");
System.out.println("Subjects for ME year 4: Refrigeration and Air Conditioning, Turbo
Machinery, Robotics and Automation");
}
}
package dept;
public class CE
{
public void displaySubjects()
{
System.out.println("Subjects for CE year 1: Maths, Physics, Chemistry");
System.out.println("Subjects for CE year 2: Surveying, Strength of Materials, Fluid
Mechanics");
System.out.println("Subjects for CE year 3: Environmental Engineering, Transportation
Engineering, Geotechnical Engineering");
System.out.println("Subjects for CE year 4: Construction Management, Remote
Sensing, GIS");
}
}
import dept.*;
ME me = new ME();
me.displaySubjects();
CE ce = new CE();
ce.displaySubjects();
}
}
9. Create a class First (With display()), Second (With show()) in one package (myp), create a
class Third (With show()) in second package (mys). Create a class Test (main()) & call all the
methods in different classes.
//first.java
package myp;
//Third.java
package mys;
public class Third
{
public void show()
{
System.out.println("This is class Third");
System.out.println("This is in mys package");
}
}
//Test.java
import myp.*;
import mys.*;
class Test
{
public static void main(String args[])
{
First obj1=new First();
obj1.display();
Second obj2=new Second();
obj2.show();
Third obj3=new Third();
obj3.show();
}
}
10.Write a Java program to implement i) Null pointer exception, ii) String Index Out Of
Bounds Exception iii) FileNotFound and iv) InputMismatch Exception.
PROGRAM:
import java.io.*;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.lang.*;
class String_Exception
void display()
String s;
char c[];
Scanner sc=new Scanner(System.in);
s=sc.nextLine();
try
int idx=sc.nextInt();
catch(StringIndexOutOfBoundsException e)
class Input_Exception
void display()
try
{
int a=sc.nextInt();
catch(InputMismatchException e)
class File_Exception
void display()
String fname=sc.nextLine();
try
System.out.println(data);
br.close();
catch (FileNotFoundException e)
catch (IOException e)
System.out.println("Exception occured");
class Pointer_Exception
{
void display()
String str1=null;
try
System.out.println(str1.toLowerCase());
catch(NullPointerException e)
{u
class ques_10_predefException
System.out.println("i)NullPointerException ");
pe.display();
System.out.println("ii)StringIndexOutOfBoundsException ");
se.display();
System.out.println("iii)FileNotFoundException ");
fe.display();
System.out.println("iv)InputMismatchException ");
ie.display();
11.Create a class to handle a user defined exception (Name: Your preferred name), to check if
user can attend the university exam. If the user says he/she is tested corona positive, user is
not eligible to attend the exam. If the user says he/she is tested corona negative, user is
eligible to attend the exam
import java.util.Scanner;
import java.lang.Exception;
class MyException extends Exception
{
MyException(String msg)
{
super(msg);
}
}
class ques_12_UserException
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter name of student:");
String name=sc.nextLine();
try
{
System.out.println("Result of Corona Test(positive/negative): ");
String status=sc.nextLine();
if("positive".equals(status))
throw new MyException("You are not eligible to write the Exam");
else
throw new MyException("You are eligible to write the Exam");
}
catch(MyException e)
{
System.out.println(e.getMessage());
}
}
}
12. Create an abstract class Eatable with an abstract method eat(), Create a child class Fruit
(Make it as abstract) add abstract method taste(), Create a child class to Fruit as nameofFruit
(class name) and call the methods eat, taste in a test class
abstract class eatable
{
abstract void eat();
}
abstract class Fruits extends eatable
{
abstract void taste();
}
class Apple extends Fruits {
@Override
void eat()
{
System.out.println("\n-------------------\nEating Apple");
}
void taste() {
System.out.println("Sweet taste");
void taste()
{
System.out.println("Sour taste");
}
}
public class ques_12_Abstractcls
{
public static void main(String[] args) {
Apple a = new Apple();
Pineapple p = new Pineapple();
a.eat();
a.taste();
p.eat();
p.taste();
}
}
13. Develop a Java program for generating three threads to perform the following Operations:
i) Accept ‘N’ numbers ii) Print the greatest number and iii)Print the smallest number
import java.util.Scanner;
import java.lang.Thread;
class GenerateNum extends Thread
{
int arr[]=new int [30];
int n;
@Override
public void run()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of array:");
n=sc.nextInt();
System.out.println("Enter elements:");
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
System.out.println("Displaying the elements:");
for(int i=0;i<n;i++)
System.out.print(arr[i]+" ");
Greatest g=new Greatest(arr);
g.start();
Smallest s=new Smallest(arr);
s.start();
}
}
class Smallest extends Thread
{
int a[];
Smallest(int a1[])
{
a=a1;
}
public void run()
{
int min=a[0];
for(int i=0;i< a.length ; i++)
{
if(a[i]<min)
{
min=a[i];
}
}
System.out.println("Smallest Number: "+ min);
}
}
class Greatest extends Thread
{
int b[];
Greatest(int b1[])
{
b=b1;
}
public void run()
{
int max=b[0];
for(int i=0;i< b.length ; i++)
{
if(b[i]>max)
{
max=b[i];
}
}
System.out.println("\nGreatest Number: "+max);
}
}
class ques_13_Thread
{
public static void main(String args[])
{
GenerateNum gn=new GenerateNum();
gn.start();
}
}
14. Write a Java program to rethrow an exception – Define methods one() & two(). Method
two() should initially throw an exception. Method one() should call two(), catch the exception
and rethrow it Call one() from main() and catch the rethrown.
import java.util.Scanner;
class ques_14_RethrowException
{
//int a;
public static void main(String args[])
{
int a ;
Scanner sc=new Scanner(System.in);
try
{
System.out.println("Enter a number: ");
a=sc.nextInt();
ques_14_RethrowException ob=new ques_14_RethrowException();
int cat=ob.one();
if(a>50)
throw ( new ArithmeticException("......myMessage...... \n The number is
greater than 50"));
System.out.println("no is less then 50 ="+a);
}
catch(ArithmeticException e)
{
System.out.println("Error found"+e.getMessage());
}
finally
{
System.out.println("Done .....");
}
}
int one()throws ArithmeticException
{
ques_14_RethrowException ob1=new ques_14_RethrowException();
int a=10, b=0;
int c=ob1.two(a,b);
return c;
}
int two(int a,int b)
{
int c1=a/b;
return c1;
}
}
15. Create an Interface payable with method getAmount ().Calculate the amount to be paid to
Invoice and Employee by implementing Interface.
import java.util.Scanner;
interface Payable
{
double getAmount();
}
class Employee implements Payable
{
String fname;
//String lname;
double sal;
void setData(String fname,double sal)
{
this.fname=fname;
this.sal=sal;
}
@Override
public double getAmount()
{
return sal;
}
void show()
{
System.out.println("Name: "+fname);
}
}
class ques_15_Interface
{
public static void main(String args[])
{