You are on page 1of 25

Write an application that declares a class named person it should have instance variables to

record name age and salary. Create a person object . Set and display its instance variables
class person
{
String name;
int age;
float sal;
}
class prg15
{
public static void main(String args[])
{
person pr = new person();
prname = !"o#it $adav!;
prage = 1%;
prsal = 5&''';
S$stemoutprintln(!(ame of t#e )erson is * !+prname);
S$stemoutprintln(!,ge of t#e )erson is * !+prage);
S$stemoutprintln(!Salar$ of t#e )erson is * !+prsal);
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg15
(ame of t#e )erson is * "o#it $adav
,ge of t#e )erson is * 1%
Salar$ of t#e )erson is * 5&''''
write an application that creates a class named circle with instance variables for the center
and the radius create a circle object . Initialize and display its instance varibales
class circle
{
int 5='6$=';
float radius;
public void s#ow()
{
S$stemoutprintln(!7oordinates of center are * ! + 5 + ! !+ $);
S$stemoutprintln(!lengt# of t#e radius is * ! + radius);
}
}
class prg18
{
public static void main(String args[])
{
circle c1 = new circle();
c15=1';
c1$=&';
c1radius=19;
c1s#ow();
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg18
7oordinates of center are * 1' &'
lengt# of t#e radius is * 19'
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4
modify EX.! to have a constructor in circle class to initialize and display instance variables
class circle
{
int 5='6$=';
float radius;
circle(int a6 int b 6float c)
{
5=a;
$=b;
radius=c;
}
public void s#ow()
{
S$stemoutprintln(!7oordinates of center are * ! + 5 + ! !+ $);
S$stemoutprintln(!lengt# of t#e radius is * ! + radius);
}
}
class prg1:
{
public static void main(String args[])
{
circle c1 = new circle(56%61');
c1s#ow();
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg1:
7oordinates of center are * 5 %
lengt# of t#e radius is * 1''
modify E".# and show constructor overloading
class circle
{
int 5='6$=';
float radius=';
circle(int a6 int b 6float c)
{
5=a;
$=b;
radius=c;
}
circle(int a 6int b)
{
5=a;
$=b;
}
public void s#ow()
{
S$stemoutprintln(!7oordinates of center are * ! + 5 + ! !+ $);
S$stemoutprintln(!lengt# of t#e radius is * ! + radius);
}
}
class prg1;
{
public static void main(String args[])
{
circle c1 = new circle(56%);
c1s#ow();
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg1;
7oordinates of center are * 5 %
lengt# of t#e radius is * ''
modify E".$ and add a function area%& to calculate and display the area of circle and another
function set'ata%& to get the values of instance variables through member functions
class circle
{
int 5='6$=';
float radius=';
double area;
void set<ata(int a6int b6float r)
{
5=a;
$=b;
radius=r;
}
void area()
{
area = =at#)>?radius?radius;
S$stemoutprintln(!,rea of t#e circle is * !+area);
}
}
class prg1%
{
public static void main(String args[])
{
circle c1 = new circle();
c1set<ata(56 %6 :);
c1area();
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg1%
,rea of t#e circle is * 159%9;'@''&5;%%;5
modify E".( by adding two more functions int translate%int)int& which passes " and y values
for translation and returns ara and int scale%int& which passes the scaling factor and returns
the area
class circle
{
int 5='6$=';
float radius=';
int area;
void set<ata(int a6int b6float r)
{
5=a;
$=b;
radius=r;
}
void area()
{
area = (int)(=at#)>?radius?radius);
S$stemoutprintln(!,rea of t#e circle is * !+area);
}
int translate(int a6int b)
{
5=5+a;
$=$+b;
area = (int)(=at#)>?radius?radius);
return area;
}
int scale(int a)
{
radius= radius?a;
area = (int)(=at#)>?radius?radius);
return area;
}
}
class prg&'
{
public static void main(String args[])
{
int a;
circle c1 = new circle();
c1set<ata(56 %6 :);
c1area();
a=c1translate(56%);
S$stemoutprintln(!,rea of t#e circle is * !+a);
a=c1scale(:);
S$stemoutprintln(!,rea of t#e circle is * !+a);
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg&'
,rea of t#e circle is * 159
,rea of t#e circle is * 159
,rea of t#e circle is * :5@&
write any program to display the uses of this *eyword
class test
{
int 56$;
float a;
void set<ata(int 56int $6float a)
{
t#is5= 5;
t#is$=$;
t#isa=a;
}
void s#ow()
{
S$stemoutprintln(!Aalues are *!+5+! !+$+! !+a);
}
}
class prg&1
{
public static void main(String args[])
{
test t1 = new test();
t1set<ata(%6568);
t1s#ow();B
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg&1
Aalues are *% 5 8'
write an application that implements stac*
class stac-
{
int top6siCe=&';
int st-[];
stac-()
{
st- = new int[siCe];
top=/1;
}
void pus#(int 5)
{
if(top==siCe/1)
S$stemoutprintln(!overflow!);
st-[++top]=5;
}
int pop()
{
int C;
if(top==/1)
S$stemoutprintln(!Dnderflow!);
C=st-[top];
top//;
return C;
}
}
class prg&&
{
public static void main(String args[])
{
stac- st = new stac-();
int a[];
a = new int[argslengt#];
for(int i=';iEargslengt#;i++)
{
a[i] =>ntegerparse>nt(args[i]);
stpus#(a[i]);
}
for(int i=';iEargslengt#;i++)
{
a[i]=stpop();
S$stemoutprintln(a[i]);B
}
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg&& & 9 8 5 ; %
%
;
5
8
9
&
write an application that implements method overloading
class test
{
int 56$;
float m;
double n;
void set<ata(int a6int b)
{
5=a;
$=b;
S$stemoutprintln(!Aalues are !+5+! !+$);
}
void set<ata(int a6int b6float c)
{
5=a;
$=b;
m=c;
S$stemoutprintln(!Aalues are !+5+! !+$+! !+m);
}
void set<ata(int a6int b6float c6double d)
{
5=a;
$=b;
m=c;
n=d;
S$stemoutprintln(!Aalues are !+5+! !+$+! !+m+! !+n);
}
}
class prg&9
{
public static void main(String args[])
{
test t1 = new test();
t1set<ata(%65);
t1set<ata(%656&9);
t1set<ata(%656&96588);

}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg&9
Aalues are % 5
Aalues are % 5 &9'
Aalues are % 5 &9' 588
write an application that shows passing object as parameter
class circle
{
int 5='6$=';
float radius=';
double area;
circle(int a6int b6float r)
{
5=a;
$=b;
radius=r;
}
circle(circle c)
{
5=c5;
$=c$;
radius=cradius;
}
void area()
{
area = =at#)>?radius?radius;
S$stemoutprintln(!,rea of t#e circle is * !+area);
}
}
class prg&@
{
public static void main(String args[])
{
circle c1 = new circle(5686%);
c1area();
circle c& = new circle(c1);
S$stemoutprintln(!,fter passing t#e first ob3ect as parameter*!);
c&area();
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg&@
,rea of t#e circle is * &5@@8%''@%@'::9&9
,fter passing t#e first ob3ect as parameter*
,rea of t#e circle is * &5@@8%''@%@'::9&9
a trapezoid is a geometric shape that has four sides . +wo of these are parallel and are called
the bases. +he height is the distance between the bases. 'efine a ,trapezoid- class that
encapsulates the height and length of each base. .rovide appropriate constructors and area%&
method to calculate area /.01height1%base2base3&.
class trapeCoid
{
int b1='6b&='6#eig#t=';
double area;
trapeCoid(int a6int b6int #)
{
b1=a;
b&=b;
#eig#t=#;
}
void area()
{
area = '5?#eig#t?(b1+b&);
S$stemoutprintln(!,rea of t#e trapeCoid is * !+area);
}
}
class prg&5
{
public static void main(String args[])
{
int a = >ntegerparse>nt(args[']);
int b = >ntegerparse>nt(args[1]);
int c = >ntegerparse>nt(args[&]);
trapeCoid t1 = new trapeCoid(a6b6c);
t1area();
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg&5 5 8 %
,rea of t#e trapeCoid is * @%5

write an application that creates a 4bo"4 classes with attributes as width)height and depth .
Introduce /5arg)5arg)65arg and another constructor with a bo" object as parameter. 7lso add
a function that returns volume%&
class bo5
{
int widt#=16#eig#t=16dept#=1;
float volume;
bo5(int a6int b6int #)
{
widt#=a;
#eig#t=b;
dept#=#;
}
bo5(int a)
{
widt# = #eig#t = dept# = a;
}
bo5(bo5 b5)
{
widt#=b5widt#;
#eig#t=b5#eig#t;
dept#=b5dept#;
}
bo5()
{
}
float volume()
{
volume = widt#?#eig#t?dept#;
return volume;
}
}
class prg&8
{
public static void main(String args[])
{
float v;
int a = >ntegerparse>nt(args[']);
int b = >ntegerparse>nt(args[1]);
int c = >ntegerparse>nt(args[&]);
S$stemoutprintln(!7onstructor wit# t#ree arguments!);
bo5 b51 = new bo5(a6b6c);
v=b51volume();
S$stemoutprintln(!volume of t#e bo5 is * !+ v);
S$stemoutprintln(!7onstructor wit# 1 arguments!);
bo5 b5& = new bo5(a);
v=b5&volume();
S$stemoutprintln(!volume of t#e bo5 is * !+ v);
S$stemoutprintln(!7onstructor wit# ' arguments!);
bo5 b59 = new bo5();
v=b59volume();
S$stemoutprintln(!volume of t#e bo5 is * !+ v);
S$stemoutprintln(!7onstructor wit# ob3ect as argument!);
bo5 b5@ = new bo5(b51);
v=b5@volume();
S$stemoutprintln(!volume of t#e bo5 is * !+ v);
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg&8 & ; :
7onstructor wit# t#ree arguments
volume of t#e bo5 is * 11&'
7onstructor wit# 1 arguments
volume of t#e bo5 is * ;'
7onstructor wit# ' arguments
volume of t#e bo5 is * 1'
7onstructor wit# ob3ect as argument
volume of t#e bo5 is * 11&'
write a program to illustrate simple inheritance
class test
{
public int 56$;
public void s#ow()
{
S$stemoutprintln(!Aalues are !+5+! !+$);
}
}
class use e5tends test
{
void set<ata()
{
5=1';
$=&';
}
}
class prg&:
{
public static void main(String args[])
{
use u1 = new use();
u1set<ata();
u1s#ow();
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg&:
Aalues are 1' &'
write an application to illustrate method overriding
class test
{
public int 56$;
void set<ata()
{
5=5;
$=8;
}
public void s#ow()
{
S$stemoutprintln(!Aalues are !+5+! !+$);
}
}
class use e5tends test
{
void set<ata()
{
5=1';
$=&';
}
}
class prg&;
{
public static void main(String args[])
{
use u1 = new use();
u1set<ata();
u1s#ow();
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg&;
Aalues are 1' &'
modify E".3! . 8o" is inherited by bo"weight and colorbo". 8o"weight adds another
parameter weight and a constructor . Colorbo" adds another parameter color and a
constructor
class bo5
{
int widt#=16#eig#t=16dept#=1;
float volume;
bo5()
{

}
float volume()
{
volume = widt#?#eig#t?dept#;
return volume;
}
}
class bo5weig#t e5tends bo5
{
int weig#t;
bo5weig#t(int a6int b6int #6int w)
{
widt#=a;
#eig#t=b;
dept#=#;
weig#t=w;
}
}
class colorbo5 e5tends bo5
{
String color;
colorbo5(int a6int b6int #6String cl)
{
widt#=a;
#eig#t=b;
dept#=#;
color=cl;
}
}
class prg&%
{
public static void main(String args[])
{
float v;
int a = >ntegerparse>nt(args[']);
int b = >ntegerparse>nt(args[1]);
int c = >ntegerparse>nt(args[&]);
int d = >ntegerparse>nt(args[9]);
S$stemoutprintln(!using bo5weig#t class in#eritance!);
bo5weig#t bw1 = new bo5weig#t(a6b6c6d);
v=bw1volume();
S$stemoutprintln(!volume of t#e bo5 is * !+ v);
S$stemoutprintln(!Feig#t of t#e bo5 is * !+ bw1weig#t);
S$stemoutprintln(!using colorbo5 class in#eritance!);
colorbo5 cb1 = new colorbo5(a6b6c6d);
v=cb1volume();
S$stemoutprintln(!volume of t#e bo5 is * !+ v);
S$stemoutprintln(!color of t#e bo5 is * !+ cb1color);
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg&% & ; : %
using bo5weig#t class in#eritance
volume of t#e bo5 is * 11&'
Feig#t of t#e bo5 is * %
using colorbo5 class in#eritance
volume of t#e bo5 is * 11&'
color of t#e bo5 is * %
write an application to illustrating a superclass variable referencing a subclass object
class test
{
int 5;
void s#ow()
{
S$stemoutprintln(!Aalue of 5 is * !+5);
}
}
class use e5tends test
{
int $;
void s#ow()
{
S$stemoutprintln(!Aalue of $ is * !+$);
}
}
class prg9'
{
public static void main(String args[])
{
use u1 = new use();
u15= 1';
u1$=5;
u1s#ow();
S$stemoutprintln(!Aalue of 5 is * !+ u15);
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg9'
Aalue of $ is * 5
Aalue of 5 is * 1'
write an application to illustrating all the uses of super *eyword
class test
{
int 56$;
test(int a6 int b)
{
5=a;
$=b;
}
void s#ow()
{
S$stemoutprintln(!value of 5 is * !+5);
S$stemoutprintln(!value of $ is * !+$);
}
}
class use e5tends test
{
use(int a 6int b)
{
super(a6b);
}
void s#ow()
{
supers#ow();
S$stemoutprintln(!value of 5 is * !+5);
S$stemoutprintln(!value of $ is * !+$);
}
}
class prg91
{ public static void main(String args[])
{
test t1 = new test(1'65);
t1s#ow();
use u1 = new use(561');
u1s#ow();
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg91
value of 5 is * 1'
value of $ is * 5
value of 5 is * 5
value of $ is * 1'
value of 5 is * 5
value of $ is * 1'
write an application that shows the order of calling of constructor in a multi level hierarchy
class first
{
first()
{
S$stemoutprintln(!constructor of first class!);
}
}
class second e5tends first
{
second()
{
S$stemoutprintln(!constructor of second class!);
}
}
class t#ird e5tends second
{
t#ird()
{
S$stemoutprintln(!constructor of t#ird class!);
}
}
class prg9&
{
public static void main(String args[])
{
S$stemoutprintln(!using ob3ect of t#ird class!);
t#ird t1 = new t#ird();
S$stemoutprintln(!using ob3ect of second class!);
second s1 = new second();
S$stemoutprintln(!using ob3ect of first class!);
first f1 = new first();
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg9&
using ob3ect of t#ird class
constructor of first class
constructor of second class
constructor of t#ird class
using ob3ect of second class
constructor of first class
constructor of second class
using ob3ect of first class
constructor of first class
write an application that illustrates dynamic method dispatch
class test
{
int 56$;
test(int a6int b)
{
5=a;
$=b;
}
void s#ow()
{
S$stemoutprintln(!Aalue of 5 is * !+5);
S$stemoutprintln(!Aalue of $ is * !+$);
}
}
class use e5tends test
{
use(int a6int b)
{
super(a6b);
}
void s#ow()
{
S$stemoutprintln(!Aalue of 5 is * !+5);
S$stemoutprintln(!Aalue of $ is * !+$);
}
}
class prg99
{
public static void main(String args[])
{
test t;
test t1 = new test(56:);
use u1 = new use(1'65);
t = t1;
ts#ow();
t= u1;
ts#ow();
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg99
Aalue of 5 is * 5
Aalue of $ is * :
Aalue of 5 is * 1'
Aalue of $ is * 5
create an abstract class shape let rectangle and triangle inherit this shape class and necessary
functions
abstract class s#ape
{
int #6w;
abstract void area();
s#ape(int a6 int b)
{
#=a;
w=b;
}
}
class rectangle e5tends s#ape
{
rectangle(int 56 int $)
{
super(56$);
}
void area()
{
double area;
area = #?w;
S$stemoutprintln(!,rea of rectangle is * !+area);
}
}
class triangle e5tends s#ape
{
triangle(int 56int $)
{
super(56$);
}
void area()
{
double area;
area = '5?#?w;
S$stemoutprintln(!,rea of triangle is * !+area);
}
}
class prg9@
{
public static void main(String args[])
{
rectangle r1 = new rectangle(1'65);
r1area();
triangle t1 = new triangle(1'65);
t1area();
}
}
output
-urosa-i.-urosa-i/des-top*01(et2eans)ro3ects1#elloworld1src4 3ava prg9@
,rea of rectangle is * 5''
,rea of triangle is * &5'

You might also like