Java Program
Java Program
Java Program
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();
pr.name = "Rohit yadav";
pr.age = 19;
pr.sal = 52000;
System.out.println("Name of the Person is : "+pr.name);
System.out.println("Age of the Person is : "+pr.age);
System.out.println("Salary of the Person is : "+pr.sal);
}
}
output
class circle
{
int x=0,y=0;
float radius;
public void show()
{
System.out.println("Coordinates of center are : " + x + " "+ y);
System.out.println("length of the radius is : " + radius);
}
}
class prg16
{
public static void main(String args[])
{
circle c1 = new circle();
c1.x=10;
c1.y=20;
c1.radius=13;
c1.show();
}
}
output
class circle
{
int x=0,y=0;
float radius;
circle(int a, int b ,float c)
{
x=a;
y=b;
radius=c;
}
public void show()
{
System.out.println("Coordinates of center are : " + x + " "+ y);
System.out.println("length of the radius is : " + radius);
}
}
class prg17
{
public static void main(String args[])
{
circle c1 = new circle(5,9,10);
c1.show();
}
}
output
class circle
{
int x=0,y=0;
float radius=0;
circle(int a, int b ,float c)
{
x=a;
y=b;
radius=c;
}
circle(int a ,int b)
{
x=a;
y=b;
}
public void show()
{
System.out.println("Coordinates of center are : " + x + " "+ y);
System.out.println("length of the radius is : " + radius);
}
}
class prg18
{
public static void main(String args[])
{
circle c1 = new circle(5,9);
c1.show();
}
}
output
class circle
{
int x=0,y=0;
float radius=0;
double area;
void setData(int a,int b,float r)
{
x=a;
y=b;
radius=r;
}
void area()
{
area = Math.PI*radius*radius;
System.out.println("Area of the circle is : "+area);
}
}
class prg19
{
public static void main(String args[])
{
circle c1 = new circle();
c1.setData(5, 9, 7);
c1.area();
}
}
output
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg19
Area of the circle is : 153.93804002589985
modify Ex.19 by adding two more functions int translate(int,int) which passes x and y values
for translation and returns ara and int scale(int) which passes the scaling factor and returns
the area
class circle
{
int x=0,y=0;
float radius=0;
int area;
void setData(int a,int b,float r)
{
x=a;
y=b;
radius=r;
}
void area()
{
area = (int)(Math.PI*radius*radius);
System.out.println("Area of the circle is : "+area);
}
int translate(int a,int b)
{
x=x+a;
y=y+b;
area = (int)(Math.PI*radius*radius);
return area;
}
int scale(int a)
{
radius= radius*a;
area = (int)(Math.PI*radius*radius);
return area;
}
}
class prg20
{
public static void main(String args[])
{
int a;
circle c1 = new circle();
c1.setData(5, 9, 7);
c1.area();
a=c1.translate(5,9);
System.out.println("Area of the circle is : "+a);
a=c1.scale(7);
System.out.println("Area of the circle is : "+a);
}
}
output
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg20
output
2
write an application that implements method overloading
class test
{
int x,y;
float m;
double n;
void setData(int a,int b)
{
x=a;
y=b;
System.out.println("Values are "+x+" "+y);
}
void setData(int a,int b,float c)
{
x=a;
y=b;
m=c;
System.out.println("Values are "+x+" "+y+" "+m);
}
void setData(int a,int b,float c,double d)
{
x=a;
y=b;
m=c;
n=d;
System.out.println("Values are "+x+" "+y+" "+m+" "+n);
}
}
class prg23
{
public static void main(String args[])
{
test t1 = new test();
t1.setData(9,5);
t1.setData(9,5,23);
t1.setData(9,5,23,5.66);
}
}
output
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg23
Values are 9 5
}
}
class prg24
{
public static void main(String args[])
{
circle c1 = new circle(5,6,9);
c1.area();
circle c2 = new circle(c1);
System.out.println("After passing the first object as parameter:");
c2.area();
}
}
output
class trapezoid
{
int b1=0,b2=0,height=0;
double area;
trapezoid(int a,int b,int h)
{
b1=a;
b2=b;
height=h;
}
void area()
{
area = 0.5*height*(b1+b2);
System.out.println("Area of the trapezoid is : "+area);
}
}
class prg25
{
public static void main(String args[])
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
trapezoid t1 = new trapezoid(a,b,c);
t1.area();
}
}
output
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg25 5 6 9
class box
{
int width=1,height=1,depth=1;
float volume;
box(int a,int b,int h)
{
width=a;
height=b;
depth=h;
}
box(int a)
{
width = height = depth = a;
}
box(box bx)
{
width=bx.width;
height=bx.height;
depth=bx.depth;
}
box()
{
}
float volume()
{
volume = width*height*depth;
return volume;
}
}
class prg26
{
public static void main(String args[])
{
float v;
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
System.out.println("Constructor with three arguments");
box bx1 = new box(a,b,c);
v=bx1.volume();
System.out.println("volume of the box is : "+ v);
System.out.println("Constructor with 1 arguments");
box bx2 = new box(a);
v=bx2.volume();
System.out.println("volume of the box is : "+ v);
System.out.println("Constructor with 0 arguments");
box bx3 = new box();
v=bx3.volume();
System.out.println("volume of the box is : "+ v);
System.out.println("Constructor with object as argument");
box bx4 = new box(bx1);
v=bx4.volume();
System.out.println("volume of the box is : "+ v);
}
}
output
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg26 2 8 7
class test
{
public int x,y;
public void show()
{
System.out.println("Values are "+x+" "+y);
}
}
class use extends test
{
void setData()
{
x=10;
y=20;
}
}
class prg27
{
public static void main(String args[])
{
use u1 = new use();
u1.setData();
u1.show();
}
}
output
Values are 10 20
write an application to illustrate method overriding
class test
{
public int x,y;
void setData()
{
x=5;
y=6;
}
public void show()
{
System.out.println("Values are "+x+" "+y);
}
}
class use extends test
{
void setData()
{
x=10;
y=20;
}
}
class prg28
{
public static void main(String args[])
{
use u1 = new use();
u1.setData();
u1.show();
}
}
output
Values are 10 20
modify Ex.26 . Box is inherited by boxweight and colorbox. Boxweight adds another
parameter weight and a constructor . Colorbox adds another parameter color and a
constructor
class box
{
int width=1,height=1,depth=1;
float volume;
box()
{
}
float volume()
{
volume = width*height*depth;
return volume;
}
}
class boxweight extends box
{
int weight;
boxweight(int a,int b,int h,int w)
{
width=a;
height=b;
depth=h;
weight=w;
}
}
class colorbox extends box
{
String color;
colorbox(int a,int b,int h,String cl)
{
width=a;
height=b;
depth=h;
color=cl;
}
}
class prg29
{
public static void main(String args[])
{
float v;
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
int d = Integer.parseInt(args[3]);
System.out.println("using boxweight class inheritance");
boxweight bw1 = new boxweight(a,b,c,d);
v=bw1.volume();
System.out.println("volume of the box is : "+ v);
System.out.println("Weight of the box is : "+ bw1.weight);
System.out.println("using colorbox class inheritance");
colorbox cb1 = new colorbox(a,b,c,d);
v=cb1.volume();
System.out.println("volume of the box is : "+ v);
System.out.println("color of the box is : "+ cb1.color);
}
}
output
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg29 2 8 7 9
using boxweight class inheritance
volume of the box is : 112.0
Weight of the box is : 9
using colorbox class inheritance
volume of the box is : 112.0
color of the box is : 9
write an application to illustrating a superclass variable referencing a subclass object
class test
{
int x;
void show()
{
System.out.println("Value of x is : "+x);
}
}
class use extends test
{
int y;
void show()
{
System.out.println("Value of y is : "+y);
}
}
class prg30
{
public static void main(String args[])
{
use u1 = new use();
u1.x= 10;
u1.y=5;
u1.show();
System.out.println("Value of x is : "+ u1.x);
}
}
output
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg30
Value of y is : 5
Value of x is : 10
write an application to illustrating all the uses of super keyword
class test
{
int x,y;
test(int a, int b)
{
x=a;
y=b;
}
void show()
{
System.out.println("value of x is : "+x);
System.out.println("value of y is : "+y);
}
}
class prg31
{ public static void main(String args[])
{
test t1 = new test(10,5);
t1.show();
use u1 = new use(5,10);
u1.show();
}
}
output
class first
{
first()
{
System.out.println("constructor of first class");
}
}
class second extends first
{
second()
{
System.out.println("constructor of second class");
}
}
class third extends second
{
third()
{
System.out.println("constructor of third class");
}
}
class prg32
{
public static void main(String args[])
{
System.out.println("using object of third class");
third t1 = new third();
System.out.println("using object of second class");
second s1 = new second();
System.out.println("using object of first class");
first f1 = new first();
}
}
output
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg32
using object of third class
constructor of first class
constructor of second class
constructor of third class
using object of second class
constructor of first class
constructor of second class
using object of first class
constructor of first class
write an application that illustrates dynamic method dispatch
class test
{
int x,y;
test(int a,int b)
{
x=a;
y=b;
}
void show()
{
System.out.println("Value of x is : "+x);
System.out.println("Value of y is : "+y);
}
}
class use extends test
{
use(int a,int b)
{
super(a,b);
}
void show()
{
System.out.println("Value of x is : "+x);
System.out.println("Value of y is : "+y);
}
}
class prg33
{
public static void main(String args[])
{
test t;
test t1 = new test(5,7);
use u1 = new use(10,5);
t = t1;
t.show();
t= u1;
t.show();
}
}
output
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg33
Value of x is : 5
Value of y is : 7
Value of x is : 10
Value of y is : 5
create an abstract class shape let rectangle and triangle inherit this shape class and necessary
functions
void area()
{
double area;
area = h*w;
System.out.println("Area of rectangle is : "+area);
}
}
class triangle extends shape
{
triangle(int x,int y)
{
super(x,y);
}
void area()
{
double area;
area = 0.5*h*w;
System.out.println("Area of triangle is : "+area);
}
}
class prg34
{
public static void main(String args[])
{