MCSL025 Java Lab Records 2

Session 4 : Inheritance and Polymorphism 

 

Ex 10: Write a Java program to show that private member of a super class cannot be accessed from derived
classes. 

 

Code:
class room
{
private int l,b;
room(int x,int y)
{ l=x; b=y;}
int area()
{return(l*b);}
}
class class_room extends room
{
int h;
class_room(int x,int y,int z)
{
super(x,y);
h=z;
}
int volume()
{
return(area()*h);
}
}
class s04_01
{
public static void main(String args[])
{
class_room cr=new class_room(10,20,15);
int a1=cr.area();
int v1=cr.volume();
System.out.println("Area of Room : "+a1);
System.out.println("Volume of Room : "+v1);
}
} 

 

Ex 11: Write a program in Java to create a Player class. Inherit the classes Cricket _Player, Football _Player
and Hockey_ Player from Player class. 

 

Code:
class player
{
String name;
int age;
player(String n,int a)
{ name=n; age=a; }
void show()
{
System.out.println("\n");
System.out.println("Player name : "+name);
System.out.println("Age : "+age);
}
}
class criket_player extends player
{
String type;
criket_player(String n,String t,int a)
{
super(n,a);
type=t;
}
public void show()
{
super.show();
System.out.println("Player type : "+type);
}
}
class football_player extends player
{
String type;
football_player(String n,String t,int a)
{
super(n,a);
type=t;
}
public void show()
{
super.show();
System.out.println("Player type : "+type);
}
}
class hockey_player extends player
{
String type;
hockey_player(String n,String t,int a)
{
super(n,a);
type=t;
}
public void show()
{
super.show();
System.out.println("Player type : "+type);
}
}
//--------- main -----------
class s04_02
{
public static void main(String args[])
{
criket_player c=new criket_player("Ameer","criket",25);
football_player f=new football_player("arun","foot ball",25);
hockey_player h=new hockey_player("Ram","hockey",25);
c.show();
f.show();
h.show();
}
} 

 

 

Ex 12: Write a class Worker and derive classes DailyWorker and SalariedWorker from it. Every worker has
a name and a salary rate. Write method ComPay (int hours) to compute the week pay of every worker. A
Daily Worker is paid on the basis of the number of days s/he works. The Salaried Worker gets paid the wage
for 40 hours a week no matter what the actual hours are. Test this program to calculate the pay of workers.
You are expected to use the concept of polymorphism to write this program. 

 

Code:
class worker
{
String name;
int empno;
worker(int no,String n)
{ empno=no; name=n; }
void show()
{
System.out.println("\n--------------------------");
System.out.println("Employee number : "+empno);
System.out.println("Employee name : "+name);
}
}
class dailyworker extends worker
{
int rate;
dailyworker(int no,String n,int r)
{
super(no,n);
rate=r;
}
void compay(int h)
{
show();
System.out.println("Salary : "+rate*h);
}
}
class salariedworker extends worker
{
int rate;
salariedworker(int no,String n,int r)
{
super(no,n);
rate=r;
}
int hour=40;
void compay()
{
show();
System.out.println("Salary : "+rate*hour);
}
}
//--------- main -----------
class s04_03
{
public static void main(String args[])
{
dailyworker d=new dailyworker(254,"Arjun",75);
salariedworker s=new salariedworker(666,"Unni",100);
d.compay(45);
s.compay();
}
} 

 

 

Ex 13: Consider the trunk calls of a telephone exchange. A trunk call can be ordinary, urgent or lightning.
The charges depend on the duration and the type of the call. Writ a program using the concept of
polymorphism in Java to calculate the charges. 

 

Code:
import java.io.*;
class call
{
float dur;
String type;
float rate()
{
if(type.equals("urgent"))
return 4.5f;
else if(type=="lightning")
return 3.5f;
else
return 3f;
}
}
class bill extends call
{
float amount;
DataInputStream in=null;
bill()
{
try
{
in=new DataInputStream(System.in);
}
catch(Exception e)
{ System.out.println(e); }
}
void read()throws Exception
{
String s;
System.out.println("enter call type(urgent,lightning,ordinary):");
type=in.readLine();
System.out.println("enter call duration:");
s=in.readLine();
dur=Float.valueOf(s).floatValue();
}
void calculate()
{
if(dur<=1.5)
amount=rate()*dur+1.5f;
else if(dur<=3)
amount=rate()*dur+2.5f;
else if(dur<=5)
amount=rate()*dur+4.5f;
else
amount=rate()*dur+5f;
}
void print()
{
System.out.println("**********************");
System.out.println(" PHONE BILL ");
System.out.println("**********************");
System.out.println(" Call type : "+type);
System.out.println(" Duration : "+dur);
System.out.println(" CHARGE : "+amount);
System.out.println("**********************");
}
}
class s04_04
{
public static void main(String arg[])throws Exception
{
bill b=new bill();
b.read();
b.calculate();
b.print();
}
}

 

Session 5 : Package and Interrface 

 

Ex 14: Write a program to make a package Balance in which has Account class with Display_Balance
method in it. Import Balance package in another program to access Display_Balance method of Account
class. 

 

Code:
class s05_01
{
public static void main(String ar[])
{
try
{
balance.account a=new balance.account();
a.read();
a.disp();
}
catch(Exception e)
{ System.out.println(e); }
}
}
package balance;
import java.io.*;
public class account
{
long acc,bal;
String name;
public void read()throws Exception
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the name :");
name=in.readLine();
System.out.println("Enter the account number :");
acc=Long.parseLong(in.readLine());
System.out.println("Enter the account balance :");
bal=Long.parseLong(in.readLine());
}
public void disp()
{
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("--- Account Details ---");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("Name :"+name);
System.out.println("Account number :"+acc);
System.out.println("Balance :"+bal);
}
} 

 

 

Ex 15: Write a program in Java to show the usefulness of Interfaces as a place to keep constant value of the
program. 

 

Code:
interface area
{
static final float pi=3.142f;
float compute(float x,float y);
}
class rectangle implements area
{
public float compute(float x,float y)
{return(x*y);}
}
class circle implements area
{
public float compute(float x,float y)
{return(pi*x*x);}
}
class s05_02
{
public static void main(String args[])
{
rectangle rect=new rectangle();
circle cr=new circle();
area ar;
ar=rect;
System.out.println("Area of the rectangle= "+ar.compute(10,20));
ar=cr;
System.out.println("Area of the circle= "+ar.compute(10,0));
}
}

 

 Ex 16: Create an Interface having two methods division and modules. Create a class, which overrides these
methods. 

 

Code:
interface course
{
void division(int a);
void modules(int b);
}
class stud implements course
{
String name;
int div,mod;
void name(String n)
{ name=n; }
public void division(int a)
{ div=a; }
public void modules(int b)
{ mod=b; }
void disp()
{
System.out.println("Name :"+name);
System.out.println("Division :"+div);
System.out.println("Modules :"+mod);
}
}
//--------main---------------
class s05_03
{
public static void main(String args[])
{ stud s=new stud();
("Arun");s.name
s.division(5);
s.modules(15);
s.disp();
}
}

Session 6 : Exception Handling 

 

Ex 18: Write a program in Java to display the names and roll numbers of students. Initialize respective array
variables for 10 students. Handle Array Index Out Of Bounds Exeption, so that any such problem doesn‟t
cause illegal termination of program. 

 

Code:
import java.io.*;
class student
{
String name,grade;
int reg,m1,m2,m3;
void read()throws Exception
{
DataInputStream in= new DataInputStream(System.in);
System.out.println("enter the register no : ");
reg=Integer.parseInt(in.readLine());
System.out.println("enter the name : ");
name=in.readLine();
System.out.println("enter mark1 : ");
m1=Integer.parseInt(in.readLine());
System.out.println("enter mark2 : ");
m2=Integer.parseInt(in.readLine());
System.out.println("enter mark3 : ");
m3=Integer.parseInt(in.readLine());
}
public void disp_grade()
{
int tt=m1+m2+m3;
if(tt>=250) grade="A";
else if(tt>=200) grade="B";
else if(tt>=150) grade="C";
else if(tt>=100) grade="D";
else grade="E";
System.out.println("Grade :"+grade);
}
void disp()
{
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println(" MARK LIST OF STUDENTS ");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("Register No :"+reg);
System.out.println("Name :"+name);
System.out.println("Mark1 :"+m1);
System.out.println("Mark2 :"+m2);
System.out.println("Mark3 :"+m3);
disp_grade();
}
}
class s06_01
{
public static void main(String ar[])
{
int no=0;
student s=new student();
try
{
DataInputStream in= new DataInputStream(System.in);
System.out.println("enter the number of students : ");
no=Integer.parseInt(in.readLine());
for(int i=0;i<no;i++);
s.read();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("the maximum students should be ten\n");
no=10;
}
catch(Exception e)
{ System.out.println(e); }
for(int i=0;i<no;i++);
s.disp();
}
}

 

 

 Ex 19: Write a Java program to enable the user to handle any chance of divide by zero exception. 

 

Code:
class s06_02
{
public static void main(String ar[])
{
int no=0,m=10,result=0;
try
{
result=m/no;
}
catch(ArithmeticException e)
{
System.out.println(" division by zero ");
System.out.println(" value of result has been set as one");
result=1;
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Result :"+result);
}
} 

 

 

Ex 20: Create an exception class, which throws an exception if operand is nonnumeric in calculating
modules. (Use command line arguments). 

 

Code:
class NonNum extends Exception
{
NonNum()
{ super("the value is non numeric \n"); }
}
class s06_03
{
public static void main(String ar[])
{
int a,b,c=0;
try
{
a=Integer.parseInt(ar[0]);
throw new NonNum();
}
catch(NumberFormatException e)
{System.out.println(e);}
catch(NonNum e)
{ System.out.println(e);}
}
}

 

 

 Ex 21: On a single track two vehicles are running. As vehicles are going in same direction there is no
problem. If the vehicles are running in different direction there is a chance of collision. To avoid collisions
write a Java program using exception handling. You are free to make necessary assumptions. 

 

Code:
import java.io.*;
class collision extends Exception
{
collision(String s)
{ super(s); }
}
class s06_04
{
public static void main(String ar[])
{
String t1=null,t2=null;
try
{
DataInputStream in= new DataInputStream(System.in);
System.out.println("enter the direction of vehicle1:(left/right):");
t1=in.readLine();
System.out.println("enter the direction of vehicle2:(left/right):");
t2=in.readLine();
if(!t1.equals(t2))
throw new collision("truck2 has to go on "+ t1 +" direction");
}
catch(collision e)
{
System.out.println(e);
t2=t1;
System.out.println("the collision has been avoided by redirection
truck2");
}
catch(Exception e)
{ System.out.println(e); }
System.out.println("direction of truck1 :"+t1);
System.out.println("direction of truck2 :"+t2);
}
}



No comments

Powered by Blogger.