java program
TRANSCRIPT
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(); 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
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg15Name of the Person is : Rohit yadavAge of the Person is : 19Salary of the Person is : 52000.0
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 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
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg16Coordinates of center are : 10 20length of the radius is : 13.0kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$
modify EX.16 to have a constructor in circle class to initialize and display instance variables
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
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg17
Coordinates of center are : 5 9
length of the radius is : 10.0
modify Ex.17 and show constructor overloading
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
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg18
Coordinates of center are : 5 9
length of the radius is : 0.0
modify Ex.18 and add a function area() to calculate and display the area of circle and another function setData() to get the values of instance variables through member functions
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(); }}
outputkurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg19Area 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 areaclass 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);
}}
outputkurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg20
Area of the circle is : 153
Area of the circle is : 153
Area of the circle is : 7542
write any program to display the uses of this keywordclass test{ int x,y; float a; void setData(int x,int y,float a) { this.x= x; this.y=y; this.a=a; } void show() { System.out.println("Values are :"+x+" "+y+" "+a); }}class prg21{ public static void main(String args[]) { test t1 = new test(); t1.setData(9,5,6); t1.show();\ }}
output
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg21
Values are :9 5 6.0
write an application that implements stackclass stack{ int top,size=20; int stk[]; stack() { stk = new int[size]; top=-1; } void push(int x) { if(top==size-1) System.out.println("overflow"); stk[++top]=x; } int pop() { int z; if(top==-1) System.out.println("Underflow"); z=stk[top]; top--; return z; }}class prg22{ public static void main(String args[]) { stack st = new stack(); int a[]; a = new int[args.length]; for(int i=0;i<args.length;i++) { a[i] =Integer.parseInt(args[i]); st.push(a[i]); } for(int i=0;i<args.length;i++) { a[i]=st.pop(); System.out.println(a[i]);\ } }}
outputkurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg22 2 3 6 5 8 9
9
8
5
6
3
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); }}
outputkurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg23
Values are 9 5
Values are 9 5 23.0
Values are 9 5 23.0 5.66
write an application that shows passing object as parameterclass circle{ int x=0,y=0; float radius=0; double area; circle(int a,int b,float r) { x=a; y=b; radius=r; } circle(circle c) { x=c.x; y=c.y; radius=c.radius; } void area() { area = Math.PI*radius*radius; System.out.println("Area of the circle is : "+area);
}}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
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg24
Area of the circle is : 254.46900494077323
After passing the first object as parameter:
Area of the circle is : 254.46900494077323
a trapezoid is a geometric shape that has four sides . Two of these are parallel and are called the bases. The height is the distance between the bases. Define a “trapezoid” class that encapsulates the height and length of each base. Provide appropriate constructors and area() method to calculate area 0.5*height*(base1+base2).
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(); }}
outputkurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg25 5 6 9
Area of the trapezoid is : 49.5
write an application that creates a 'box' classes with attributes as width,height and depth . Introduce 0-arg,1-arg,3-arg and another constructor with a box object as parameter. Also add a function that returns volume()
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); }}
outputkurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg26 2 8 7
Constructor with three arguments
volume of the box is : 112.0
Constructor with 1 arguments
volume of the box is : 8.0
Constructor with 0 arguments
volume of the box is : 1.0
Constructor with object as argument
volume of the box is : 112.0
write a program to illustrate simple inheritance
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
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg27
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
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg28
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 constructorclass 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); }}
outputkurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg29 2 8 7 9using boxweight class inheritancevolume of the box is : 112.0Weight of the box is : 9using colorbox class inheritancevolume of the box is : 112.0color 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);
}}outputkurosaki@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 use extends test{
use(int a ,int b){
super(a,b);}void show(){
super.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
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg31value of x is : 10value of y is : 5value of x is : 5value of y is : 10value of x is : 5value of y is : 10
write an application that shows the order of calling of constructor in a multi level hierarchy
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();
}}
outputkurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg32using object of third classconstructor of first classconstructor of second classconstructor of third classusing object of second classconstructor of first classconstructor of second classusing object of first classconstructor 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();
}}outputkurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg33Value of x is : 5Value of y is : 7Value of x is : 10Value of y is : 5
create an abstract class shape let rectangle and triangle inherit this shape class and necessary functions
abstract class shape{
int h,w;abstract void area();shape(int a, int b){
h=a;w=b;
}}class rectangle extends shape{
rectangle(int x, int y){
super(x,y);}
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[]){
rectangle r1 = new rectangle(10,5);r1.area();triangle t1 = new triangle(10,5);t1.area();
}}
output
kurosaki@kurosaki-desktop:~/NetBeansProjects/helloworld/src$ java prg34
Area of rectangle is : 50.0
Area of triangle is : 25.0