Java Lab Manual
Java Lab Manual
Java Lab Manual
member:
class rectangle
{
int length,width;
void getData(int x,int y)
{
length=x;
width=y;
}
int rectarea()
{
int area;
area=length*width;
return(area);
}
}
class rectArea
{
public static void main(String args[])
{
int area1,area2;
rectangle rect1=new rectangle();
rectangle rect2=new rectangle();
rect1.length=15;
rect1.width=10;
area1=rect1.length*rect1.width;
rect2.getData(10,20);
area2=rect2.rectarea();
System.out.println("area1="+area1);
Object Oriented Programming and Design with Java(20CS43P)
System.out.println("area2="+area2);
}
}
Output:
2
Object Oriented Programming and Design with Java(20CS43P)
}
void display()
{
System.out.println("Employee no:"+empno);
System.out.println("Employee name:"+name);
System.out.println("Designation:"+desig);
System.out.println("Salary:"+salary);
}
}
class EmployeeDemo
{
public static void main(String args[])
{
Employee e1=new Employee();
e1.display();
System.out.println("-----------------------");
Employee e2=new Employee(2,"koosappa","Doctor",50000);
e2.display();
System.out.println("-----------------------");
Employee e3=new Employee(e2);
e3.display();
System.out.println("-----------------------");
}
}
Output:
3
Object Oriented Programming and Design with Java(20CS43P)
String name;
float fee;
Student(int rollno,String name,float fee)
{
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display()
{
System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis2
{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}
}
Output:
4
Object Oriented Programming and Design with Java(20CS43P)
byte b = 100;
Byte B = b;
System.out. println(B);
short s = 100;
Short S = s;
System.out.println(S);
int i = 200;
Integer I = i;
System.out.println(I);
long l= 250;
Long L = l;
System.out.println(L);
float f =120L;
Float F = f;
System.out.println(F);
double d = 18.58;
Double D = d;
System.out.println(D);
5
Object Oriented Programming and Design with Java(20CS43P)
char c = 'C';
Character C = c;
System.out.println(C);
}
}
Output:
6
Object Oriented Programming and Design with Java(20CS43P)
a) package p1;
public class A
{
public void displayA()
{
7
Object Oriented Programming and Design with Java(20CS43P)
System.out.println("CLASS A");
}
}
b) package p2;
public class B
{
protected int m=10;
public void displayB()
{
System.out.println("CLASS B");
System.out.println("m="+m);
}
}
class A
{
void display()
{
System.out.println("This is from class A");
}
}
class B extends A
{
void display()
{
System.out.println("This is from class B");
}
}
class AB
{
8
Object Oriented Programming and Design with Java(20CS43P)
Output:
9
Object Oriented Programming and Design with Java(20CS43P)
return empage;
}
public void setempage(int newvalue)
{
empage=newvalue;
}
public void setempname(String newvalue)
{
empname=newvalue;
}
public void setempssn(int newvalue)
{
ssn=newvalue;
}
}
public class encapstest
{
public static void main(String args [])
{
encapsulationdemo obj=new encapsulationdemo();
obj.setempname("mario");
obj.setempage(32);
obj.setempssn(112233);
System.out.println("employee name:"+obj.getempname());
System.out.println("employee ssn:"+obj.getempssn());
System.out.println("employee age:"+obj.getempage());
}
}
Output:
10
Object Oriented Programming and Design with Java(20CS43P)
Output:
11
Object Oriented Programming and Design with Java(20CS43P)
Output:
12
Object Oriented Programming and Design with Java(20CS43P)
Output:
13
Object Oriented Programming and Design with Java(20CS43P)
14
Object Oriented Programming and Design with Java(20CS43P)
}
}
Output:
class B extends A
{
void display()
{
System.out.println("This is from class B ");
}
}
class AB
{
public static void main(String arg[])
15
Object Oriented Programming and Design with Java(20CS43P)
{
B obj=new B();
obj.display();
}
}
Output:
16
Object Oriented Programming and Design with Java(20CS43P)
Output:
17
Object Oriented Programming and Design with Java(20CS43P)
}
}
Output:
18
Object Oriented Programming and Design with Java(20CS43P)
{
System.out.println("An unexpected error is occured.");
exception.printStackTrace();
}
}
}
Output:
19
Object Oriented Programming and Design with Java(20CS43P)
else
{
System.out.println("The file does not exists.");
}
}
}
Output:
20
Object Oriented Programming and Design with Java(20CS43P)
Output:
Output:
21
Object Oriented Programming and Design with Java(20CS43P)
22
Object Oriented Programming and Design with Java(20CS43P)
System.out.println("y="+y);
}
}
}
Output:
23
Object Oriented Programming and Design with Java(20CS43P)
}
catch (ArrayStoreException e)
{
System.out.println("Wrong data type");
}
finally
{
int y=a[1]/a[0];
System.out.println("y="+y);
}
}
}
Output:
24