Saturday, 13 June 2020

Java Programs


Program1: Static Variable
Package end_term;
public class StaticVariable {
               static class student{
                              introllno;
                              String name;
                              static String college="Chitkara";
                              student(int r, String n)
                              {
                                             rollno=r;
                                             name=n;
                              }
                              void display()
                              {
                                             System.out.println("The Roll No. of Student is: " + rollno);
                                             System.out.println("The Name of Student is: " + name);
                              }
               }
               public static void main(String[] args) {
                              System.out.println("The name of the college is: " + student.college);
                              student S1=new student(1,"Adi");
                              student S2=new student(2,"Om");
                              S1.display();
                              S2.display();
}

}
Output:
The name of the college is: Chitkara
The Roll No. of Student is: 1
The Name of Student is: Adi
The Roll No. of Student is: 2
The Name of Student is: Om

Program2: Static Variable
packageend_term;
public class TriangleArea {
               static void area(double l1, double l2, double l3) {
                             
                              double s=(l1+l2+l3)/2;
                              double area1=s*(s-l1)*(s-l2)*(s-l3);
                              double Area=Math.sqrt(area1);
                              System.out.println("The Area of the Triangle is: " + Area);
               }
               public static void main(String[] args) {
                              double l1=6;
                              double l2=12;
                              double l3=8;
                              area(l1,l2,l3);
               }
}
Output:
The Area of the Triangle is: 21.330729007701542

Program3: Here inside main( ), the static method callme( ) and the static variable b are accessed through their class name StaticDemo.
package end_term;
classStaticDemo{
               staticint a= 20;
               staticint b=30;
               static void callme() {
                              System.out.println("The value of a is: " + a);
               }
}
public class StaticbyName {

               public static void main(String[] args) {
                              StaticDemo.callme();
                              System.out.println("The value of b is: " + StaticDemo.b);

               }
}
Output:
The value of a is: 20
The value of b is: 30

Program4: Inner Class
packageend_term;

class Outer
{
               intout_x=100;
               void test() {
               Inner inner=newInner();
               inner.display();
               }
               class Inner{
                              void display() {
                                             System.out.println("Display the value of out_x: " + out_x);
                                                                                          }
                                                            }            
}
publicclassInnerclass {

               publicstaticvoid main(String[] args) {
                              Outer out=newOuter();
                              out.test();

               }

}
Output:
Display the value of out_x: 100

Program 5:
packageend_term;

class Out
{
               intout_x=100;
               void test() {
               Inner inner=newInner();
               inner.display();
               }
               class Inner{
                              inty=10;
                              void display() {
                                             System.out.println("Display the value of out_x: " + out_x);
                                                                                          }
                                                            }
               void showy() {
                              System.out.println("Display the value of Y: " + y);//error “y cannot be resolve to a variable”
               }
}
publicclass Inner1 {

               publicstaticvoid main(String[] args) {
                              Out out1= newOut();
                                                            out1.test();

               }

}
Output:
Display the value of out_x: 100
Program 6: Inner Class
packageend_term;
class Outer1{
               intx=10;
               void test() {
                              for(inti=0; i<5;i++)
                              {
                                             class Inner{
                                                            void display() {
                                                                           System.out.println("Display the value of X: " + x);
                                                            }
                                             }
                                             Inner inner= newInner();
                                                                           inner.display();
                              }
                             
               }
              
              
}
publicclass Inner2 {

               publicstaticvoid main(String[] args) {
               Outer1 out=newOuter1();
               out.test();

               }

}
Output:
Display the value of X: 10
Display the value of X: 10
Display the value of X: 10
Display the value of X: 10
Display the value of X: 10

Program 7:CommandLine
publicclassCommandLine {

               publicstaticvoid main(String args[]) {
                              for(inti=0;i<args.length;i++) {
                                             System.out.println("args["+i+"]:" + args[i]);
                              }
               }

}

Output:
args[0]:This
args[1]:is
args[2]:a
args[3]:test
args[4]:100-1
Program 8: Variable Arguments
packageend_term;

publicclassVarArs {
staticvoidvaTest(int...c) {
               System.out.println("Number of Arguments: " + c.length +" " + "Contents are:");
               for(intx:c)
               {
                              System.out.print(x +" ");
                              System.out.println(" ");
               }
}
               publicstaticvoid main(String[] args) {
                              vaTest(10);//1 argument
                              vaTest(1,2);//2 Arguments
                              vaTest(2,4,6);//Three arguments
                              vaTest();//0 arguments

               }

}

Output:
10 
Number of Arguments: 2 Contents are:
1 
2 
Number of Arguments: 3 Contents are:
2 
4 
6 
Number of Arguments: 0 Contents are:

Program 9: Use varargswith standard arguments.
packageend_term;

publicclass Vararg1 {
staticvoidvaTest(String msg,int...v) {
               System.out.println(msg + v.length +" Contents are: ");
               for(intx:v) {
                              System.out.print(x+" ");
                              System.out.println(" ");
               }
}
               publicstaticvoid main(String[] args) {

vaTest("Zero Varargs:");
vaTest("One Varargs:", 1);
vaTest("Two Varargs:", 1,2);
vaTest("Three Varargs:", 1,2,3);
               }

}
Output:
Zero Varargs:0 Contents are:
One Varargs:1 Contents are:
1 
Two Varargs:2 Contents are:
1 
2 
Three Varargs:3 Contents are:
1 
2 
3 


Program 10:Overloading Vararg Methods

Output:

Program 11:Stack

Output:

Program 12: Simple Inheritance
packageend_term;
class A{
               inti,j;
               voidshowij() {
                              System.out.println("The Value of i is: " + i+" The value of j is: "+j);
               }
}
class B extends A
{
               intk;
               voidshowk() {
                              System.out.println("The value of K is: " + k);
               }
               void sum() {
                              intresult;
                              result= i+j+k;
                              System.out.println("The sum of Three variables i,j and k is: " + result);
               }
}
publicclassSimpleInheritance {

               publicstaticvoid main(String[] args) {
A supob=newA();
B subob=newB();
supob.i=10;
supob.j=20;
System.out.println("The contents of Super Class: ");
supob.showij();
subob.i=2;
subob.j=4;
subob.k=6;
System.out.println("The contents of Sub Class: ");
subob.showij();
subob.showk();
subob.sum();


               }

}

Output:
The contents of Super Class:
The Value of i is: 10 The value of j is: 20
The contents of Sub Class:
The Value of i is: 2 The value of j is: 4
The value of K is: 6
The sum of Three variables i,j and k is: 12

Program 13:A SuperclassVariable Can Reference a Subclass Object

Output:
Program 14: This program uses inheritance to extend Box.
packageend_term;
class Box{
               doublewidth, height,depth;
               //construct clone of an object
               Box(Box ob){//pass object to the constructor
               width=ob.width;
               height=ob.height;
               depth=ob.depth;
               }
//constructor used when all dimensions are specified
               Box(doublew,doubleh, doubled){
                              width=w;
                              height=h;
                              depth=d;
               }
//constructor used when no dimensions are specified
               Box(){
                              //use -1 to indicate an uninitialized box
                              width=-1;
                              height=-1;
                              depth=-1;
               }
//constructor used when cube is created
               Box(doublelen){
                              width=height=depth=len;
               }
//compute and return volume
               double volume() {
                              returnwidth*height*depth;
               }
}
//Box is extends to include weight
classBoxWeightextends Box{
               doubleweight;
//constructor used when all dimensions are specified
                              BoxWeight(doublew,doubleh, doubled, doublem){
                                             width=w;
                                             height=h;
                                             depth=d;
                                             weight=m;
                              }
}
publicclass Inheritence1 {

               publicstaticvoid main(String[] args) {
                              BoxWeightmybox1=newBoxWeight(10,20,30,40);
                              BoxWeightmybox2=newBoxWeight(11,21,31,41);
                              doublevol;
                              vol=mybox1.volume();
                              System.out.println("The volume of mybox1 is: " + vol);
                              System.out.println(" ");
                              vol=mybox2.volume();
                              System.out.println("The volume of mybox2 is: " + vol);
                              System.out.println(" ");
                              System.out.println("The weight of mybox1 is: " + mybox1.weight);
                              System.out.println(" ");
                              System.out.println("The weight of mybox2 is: " + mybox2.weight);
               }

}

Output:
The volume of mybox1 is: 6000.0

The volume of mybox2 is: 7161.0

The weight of mybox1 is: 40.0

The weight of mybox2 is: 41.0

Program 15: Using super to overcome name hiding.
packageend_term;
class A1{
               inti;
}
class B1 extends A1{
               inti;//this i hides i in A
               B1(inta, intb){
                              super.i=a;//i in A
                              i=b;
               }
               void show() {
                              System.out.println("The value of i in SUPERCLASS is: " +super.i);
                              System.out.println("The value of i in SUBCLASS is: " +i);
               }
}
publicclassUseSuper {

               publicstaticvoid main(String[] args) {
                              B1 subob=newB1(1,2);
                              subob.show();


               }

}

Output:
The value of i in SUPERCLASS is: 1
The value of i in SUBCLASS is: 2

Program 16: Demonstrate when constructors are executed.
               AA(){
                              System.out.println("Inside AA's Constructor.");
               }
}
class BB extends AA{
               BB(){
                              System.out.println("Inside BB's Constructor.");
               }
}
class CC extends BB{
               CC(){
                              System.out.println("Inside CC's Constructor.");
               }
}
publicclassCallingCons {

               publicstaticvoid main(String[] args) {
                              CC ob=newCC();


               }

}
Output:
Inside AA's Constructor.
Inside BB's Constructor.
Inside CC's Constructor.
Program 17: Method Override
class A2{
               inti,j;
               A2(inta, intb){
                              i=a;
                              j=b;
               }
               //display i and j
               void show() {
                              System.out.println("The value of i and j is: " + i+ j);
               }
}
class B2 extends A2
{
               intk;
               B2(inta, intb, intc)
               {
                              super(a,b);
                              k=c;
               }
               void show() {
                              System.out.println("The value of K is: " + k);
               }
}
publicclassMethodOverride {

               publicstaticvoid main(String[] args) {
                              B2 subob=newB2(1,2,3);
                              subob.show();//this call show() in B2

               }

}

Output:
The value of K is: 3
Program 18: Methodoverride1
class A3{
               inti,j;
               A3(inta, intb){
                              i=a;
                              j=b;
               }
               //display i and j
               void show() {
                              System.out.println("The value of i is: " + i +" The value of j is: " + j);
               }
}
class B3 extends A3
{
               intk;
               B3(inta, intb, intc)
               {
                              super(a,b);
                              k=c;
               }
               void show() {
                              super.show();
                              System.out.println("The value of K is: " + k);
               }
}
publicclass MethodOverride1 {

               publicstaticvoid main(String[] args) {

                              B3 subob=newB3(1,2,3);
                              subob.show();
               }

}
Output:
The value of i is: 1 The value of j is: 2
The value of K is: 3
Program 19: Method Overload
class D
{
               inti,j;
               D(inta,intb)
               {
                              i=a;
                              j=b;
               }
               void show() {
                              System.out.println("The value of i is: " + i +"The value of j is: " + j);
               }
}
class E extends D
{
               intk;
               E(inta, intb, intc)
               {
                              super(a, b);
                              k=c;
               }
               void show(String msg)
               {
                              System.out.println("The message is: " + msg);
                              System.out.println("The value of K is: "+ k);
               }
              
}
publicclassMethodOverload {

               publicstaticvoid main(String[] args) {
                              E ob=newE(1,2,3);
                              ob.show("This is Subclass");
                              ob.show();


               }

}

Output:
The message is: This is Subclass
The value of K is: 3
The value of i is: 1The value of j is: 2

Program 20: Runtime Polymorphism
class Figure{
               intdim1;
               intdim2;
               Figure(inta, intb){
                              dim1=a;
                              dim2=b;
               }
               int area() {
                              System.out.println("The Area of the Figure is Undefined.");
                              return 0;
               }
}
class Rectangle extends Figure{
               Rectangle(inta, intb){
                              super(a,b);
               }
//override area for rectangle
               int area() {
                              intarea;
                              area=dim1*dim2;
                              System.out.println("Area of the Rectangle is: " + area);
                              returnarea;
               }
}

class Triangle extends Figure{
               Triangle(inta, intb){
                              super(a,b);
               }
//override area for triangle
               int area() {
                              intarea;
                              area=(dim1*dim2)/2;
                              System.out.println("Area of the Triangle is: " + area);
                              returnarea;
               }
}
publicclassRunTimePoly {

               publicstaticvoid main(String[] args) {
                              Figure f=newFigure(10,10);
                              Rectangle r= newRectangle(9,5);
                              Triangle t=newTriangle(12,3);
                              Figure figerf;
                              figerf=r;
                              System.out.println("Area of the Rectangle is: " + figerf.area());
                              figerf=t;
                              System.out.println("Area of the Triangle is: " + figerf.area());
                              figerf=f;
                              System.out.println("Area of the Figure is: " + figerf.area());
               }

}


Output:
Area of the Rectangle is: 45
Area of the Rectangle is: 45
Area of the Triangle is: 18
Area of the Triangle is: 18
The Area of the Figure is Undefined.
Area of the Figure is: 0
Program 21: Abstract Class
abstractclass A4{
               abstractvoidcallme();
               voidcallmetoo()
                              {
                              System.out.println("This is a concrete method.");
                              }
}
class B4 extendsA4{
               voidcallme() {
                              System.out.println("B's is implemented of call me.");
               }
}
publicclass Abstract {

               publicstaticvoid main(String[] args) {
               B4 b = newB4();
               b.callme();
               b.callmetoo();
               }

}

Output:
B's is implemented of call me.
This is a concrete method.

Program 20: final variable
packageend_term;
class Bike{
               finalintspeedlimit=90;//final variable
               void run() {
                              speedlimit=400;// final field sppedlimit cannot be further assigned
               }
}
publicclassFinalVaria {

               publicstaticvoid main(String[] args) {
                              Bike ob=newBike();
                              ob.run();

               }

}

Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
               The final field Bike.speedlimit cannot be assigned

               atPracticeSt/end_term.Bike.run(FinalVaria.java:5)
               atPracticeSt/end_term.FinalVaria.main(FinalVaria.java:12)

Program 21: final Parameter
packageend_term;
classBike1{
               int cube(finalinta) {
                              a=a+2;//Error a cannot be modified
                              returna;
               }
}
publicclassFinalParameter {

               publicstaticvoid main(String[] args) {
                              Bike1ob=newBike1();
                              ob.cube(10);

               }

}

Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
               The final local variable a cannot be assigned. It must be blank and not using a compound assignment

               atPracticeSt/end_term.Bike1.cube(FinalParameter.java:4)
               atPracticeSt/end_term.FinalParameter.main(FinalParameter.java:12)


Program 22: Static Blank Variable
packageend_term;
class S{
               staticfinalintdata;
               static{
                              data=5;
               }
}
publicclassStaticBlankFinal {
              
               publicstaticvoid main(String[] args) {
                              System.out.println(S.data);
               }

}

Output:
5
Program 23: Blank Final Variable
packageend_term;

publicclass BFV {
finalintspeed;//blank final variable
BFV(){
               speed=90;
               System.out.println("The speed is: " + speed);
}
               publicstaticvoid main(String[] args) {
                              new BFV();

               }

}
Output:
The speed is: 90

Program 24:

Output:
Program 25:

Output:

Program 26:

Output:
Program 27:

Output:

Program 28:

Output:
Program 29:

Output:

Program 30:

Output:
Program 31:

Output:

Program 32:

Output:
Program 33:

Output:

Program 34:

Output:
Program 35:

Output:

Program1: Static Variable
Package end_term;
public class StaticVariable {
               static class student{
                              introllno;
                              String name;
                              static String college="Chitkara";
                              student(int r, String n)
                              {
                                             rollno=r;
                                             name=n;
                              }
                              void display()
                              {
                                             System.out.println("The Roll No. of Student is: " + rollno);
                                             System.out.println("The Name of Student is: " + name);
                              }
               }
               public static void main(String[] args) {
                              System.out.println("The name of the college is: " + student.college);
                              student S1=new student(1,"Adi");
                              student S2=new student(2,"Om");
                              S1.display();
                              S2.display();
}

}
Output:
The name of the college is: Chitkara
The Roll No. of Student is: 1
The Name of Student is: Adi
The Roll No. of Student is: 2
The Name of Student is: Om

Program2: Static Variable
packageend_term;
public class TriangleArea {
               static void area(double l1, double l2, double l3) {
                             
                              double s=(l1+l2+l3)/2;
                              double area1=s*(s-l1)*(s-l2)*(s-l3);
                              double Area=Math.sqrt(area1);
                              System.out.println("The Area of the Triangle is: " + Area);
               }
               public static void main(String[] args) {
                              double l1=6;
                              double l2=12;
                              double l3=8;
                              area(l1,l2,l3);
               }
}
Output:
The Area of the Triangle is: 21.330729007701542

Program3: Here inside main( ), the static method callme( ) and the static variable b are accessed through their class name StaticDemo.
packageend_term;
classStaticDemo{
               staticint a= 20;
               staticint b=30;
               static void callme() {
                              System.out.println("The value of a is: " + a);
               }
}
public class StaticbyName {

               public static void main(String[] args) {
                              StaticDemo.callme();
                              System.out.println("The value of b is: " + StaticDemo.b);

               }
}
Output:
The value of a is: 20
The value of b is: 30

Program4: Inner Class
packageend_term;

class Outer
{
               intout_x=100;
               void test() {
               Inner inner=newInner();
               inner.display();
               }
               class Inner{
                              void display() {
                                             System.out.println("Display the value of out_x: " + out_x);
                                                                                          }
                                                            }            
}
publicclassInnerclass {

               publicstaticvoid main(String[] args) {
                              Outer out=newOuter();
                              out.test();

               }

}
Output:
Display the value of out_x: 100

Program 5:
packageend_term;

class Out
{
               intout_x=100;
               void test() {
               Inner inner=newInner();
               inner.display();
               }
               class Inner{
                              inty=10;
                              void display() {
                                             System.out.println("Display the value of out_x: " + out_x);
                                                                                          }
                                                            }
               void showy() {
                              System.out.println("Display the value of Y: " + y);//error “y cannot be resolve to a variable”
               }
}
publicclass Inner1 {

               publicstaticvoid main(String[] args) {
                              Out out1= newOut();
                                                            out1.test();

               }

}
Output:
Display the value of out_x: 100
Program 6: Inner Class
packageend_term;
class Outer1{
               intx=10;
               void test() {
                              for(inti=0; i<5;i++)
                              {
                                             class Inner{
                                                            void display() {
                                                                           System.out.println("Display the value of X: " + x);
                                                            }
                                             }
                                             Inner inner= newInner();
                                                                           inner.display();
                              }
                             
               }
              
              
}
publicclass Inner2 {

               publicstaticvoid main(String[] args) {
               Outer1 out=newOuter1();
               out.test();

               }

}
Output:
Display the value of X: 10
Display the value of X: 10
Display the value of X: 10
Display the value of X: 10
Display the value of X: 10

Program 7:CommandLine
publicclassCommandLine {

               publicstaticvoid main(String args[]) {
                              for(inti=0;i<args.length;i++) {
                                             System.out.println("args["+i+"]:" + args[i]);
                              }
               }

}

Output:
args[0]:This
args[1]:is
args[2]:a
args[3]:test
args[4]:100-1
Program 8: Variable Arguments
packageend_term;

publicclassVarArs {
staticvoidvaTest(int...c) {
               System.out.println("Number of Arguments: " + c.length +" " + "Contents are:");
               for(intx:c)
               {
                              System.out.print(x +" ");
                              System.out.println(" ");
               }
}
               publicstaticvoid main(String[] args) {
                              vaTest(10);//1 argument
                              vaTest(1,2);//2 Arguments
                              vaTest(2,4,6);//Three arguments
                              vaTest();//0 arguments

               }

}

Output:
10 
Number of Arguments: 2 Contents are:
1 
2 
Number of Arguments: 3 Contents are:
2 
4 
6 
Number of Arguments: 0 Contents are:

Program 9: Use varargswith standard arguments.
packageend_term;

publicclass Vararg1 {
staticvoidvaTest(String msg,int...v) {
               System.out.println(msg + v.length +" Contents are: ");
               for(intx:v) {
                              System.out.print(x+" ");
                              System.out.println(" ");
               }
}
               publicstaticvoid main(String[] args) {

vaTest("Zero Varargs:");
vaTest("One Varargs:", 1);
vaTest("Two Varargs:", 1,2);
vaTest("Three Varargs:", 1,2,3);
               }

}
Output:
Zero Varargs:0 Contents are:
One Varargs:1 Contents are:
1 
Two Varargs:2 Contents are:
1 
2 
Three Varargs:3 Contents are:
1 
2 
3 


Program 10:Overloading Vararg Methods

Output:

Program 11:Stack

Output:

Program 12: Simple Inheritance
packageend_term;
class A{
               inti,j;
               voidshowij() {
                              System.out.println("The Value of i is: " + i+" The value of j is: "+j);
               }
}
class B extends A
{
               intk;
               voidshowk() {
                              System.out.println("The value of K is: " + k);
               }
               void sum() {
                              intresult;
                              result= i+j+k;
                              System.out.println("The sum of Three variables i,j and k is: " + result);
               }
}
publicclassSimpleInheritance {

               publicstaticvoid main(String[] args) {
A supob=newA();
B subob=newB();
supob.i=10;
supob.j=20;
System.out.println("The contents of Super Class: ");
supob.showij();
subob.i=2;
subob.j=4;
subob.k=6;
System.out.println("The contents of Sub Class: ");
subob.showij();
subob.showk();
subob.sum();


               }

}

Output:
The contents of Super Class:
The Value of i is: 10 The value of j is: 20
The contents of Sub Class:
The Value of i is: 2 The value of j is: 4
The value of K is: 6
The sum of Three variables i,j and k is: 12

Program 13:A SuperclassVariable Can Reference a Subclass Object

Output:
Program 14: This program uses inheritance to extend Box.
packageend_term;
class Box{
               doublewidth, height,depth;
               //construct clone of an object
               Box(Box ob){//pass object to the constructor
               width=ob.width;
               height=ob.height;
               depth=ob.depth;
               }
//constructor used when all dimensions are specified
               Box(doublew,doubleh, doubled){
                              width=w;
                              height=h;
                              depth=d;
               }
//constructor used when no dimensions are specified
               Box(){
                              //use -1 to indicate an uninitialized box
                              width=-1;
                              height=-1;
                              depth=-1;
               }
//constructor used when cube is created
               Box(doublelen){
                              width=height=depth=len;
               }
//compute and return volume
               double volume() {
                              returnwidth*height*depth;
               }
}
//Box is extends to include weight
classBoxWeightextends Box{
               doubleweight;
//constructor used when all dimensions are specified
                              BoxWeight(doublew,doubleh, doubled, doublem){
                                             width=w;
                                             height=h;
                                             depth=d;
                                             weight=m;
                              }
}
publicclass Inheritence1 {

               publicstaticvoid main(String[] args) {
                              BoxWeightmybox1=newBoxWeight(10,20,30,40);
                              BoxWeightmybox2=newBoxWeight(11,21,31,41);
                              doublevol;
                              vol=mybox1.volume();
                              System.out.println("The volume of mybox1 is: " + vol);
                              System.out.println(" ");
                              vol=mybox2.volume();
                              System.out.println("The volume of mybox2 is: " + vol);
                              System.out.println(" ");
                              System.out.println("The weight of mybox1 is: " + mybox1.weight);
                              System.out.println(" ");
                              System.out.println("The weight of mybox2 is: " + mybox2.weight);
               }

}

Output:
The volume of mybox1 is: 6000.0

The volume of mybox2 is: 7161.0

The weight of mybox1 is: 40.0

The weight of mybox2 is: 41.0

Program 15: Using super to overcome name hiding.
packageend_term;
class A1{
               inti;
}
class B1 extends A1{
               inti;//this i hides i in A
               B1(inta, intb){
                              super.i=a;//i in A
                              i=b;
               }
               void show() {
                              System.out.println("The value of i in SUPERCLASS is: " +super.i);
                              System.out.println("The value of i in SUBCLASS is: " +i);
               }
}
publicclassUseSuper {

               publicstaticvoid main(String[] args) {
                              B1 subob=newB1(1,2);
                              subob.show();


               }

}

Output:
The value of i in SUPERCLASS is: 1
The value of i in SUBCLASS is: 2

Program 16: Demonstrate when constructors are executed.
               AA(){
                              System.out.println("Inside AA's Constructor.");
               }
}
class BB extends AA{
               BB(){
                              System.out.println("Inside BB's Constructor.");
               }
}
class CC extends BB{
               CC(){
                              System.out.println("Inside CC's Constructor.");
               }
}
publicclassCallingCons {

               publicstaticvoid main(String[] args) {
                              CC ob=newCC();


               }

}
Output:
Inside AA's Constructor.
Inside BB's Constructor.
Inside CC's Constructor.
Program 17: Method Override
class A2{
               inti,j;
               A2(inta, intb){
                              i=a;
                              j=b;
               }
               //display i and j
               void show() {
                              System.out.println("The value of i and j is: " + i+ j);
               }
}
class B2 extends A2
{
               intk;
               B2(inta, intb, intc)
               {
                              super(a,b);
                              k=c;
               }
               void show() {
                              System.out.println("The value of K is: " + k);
               }
}
publicclassMethodOverride {

               publicstaticvoid main(String[] args) {
                              B2 subob=newB2(1,2,3);
                              subob.show();//this call show() in B2

               }

}

Output:
The value of K is: 3
Program 18: Methodoverride1
class A3{
               inti,j;
               A3(inta, intb){
                              i=a;
                              j=b;
               }
               //display i and j
               void show() {
                              System.out.println("The value of i is: " + i +" The value of j is: " + j);
               }
}
class B3 extends A3
{
               intk;
               B3(inta, intb, intc)
               {
                              super(a,b);
                              k=c;
               }
               void show() {
                              super.show();
                              System.out.println("The value of K is: " + k);
               }
}
publicclass MethodOverride1 {

               publicstaticvoid main(String[] args) {

                              B3 subob=newB3(1,2,3);
                              subob.show();
               }

}
Output:
The value of i is: 1 The value of j is: 2
The value of K is: 3
Program 19: Method Overload
class D
{
               inti,j;
               D(inta,intb)
               {
                              i=a;
                              j=b;
               }
               void show() {
                              System.out.println("The value of i is: " + i +"The value of j is: " + j);
               }
}
class E extends D
{
               intk;
               E(inta, intb, intc)
               {
                              super(a, b);
                              k=c;
               }
               void show(String msg)
               {
                              System.out.println("The message is: " + msg);
                              System.out.println("The value of K is: "+ k);
               }
              
}
publicclassMethodOverload {

               publicstaticvoid main(String[] args) {
                              E ob=newE(1,2,3);
                              ob.show("This is Subclass");
                              ob.show();


               }

}

Output:
The message is: This is Subclass
The value of K is: 3
The value of i is: 1The value of j is: 2

Program 20: Runtime Polymorphism
class Figure{
               intdim1;
               intdim2;
               Figure(inta, intb){
                              dim1=a;
                              dim2=b;
               }
               int area() {
                              System.out.println("The Area of the Figure is Undefined.");
                              return 0;
               }
}
class Rectangle extends Figure{
               Rectangle(inta, intb){
                              super(a,b);
               }
//override area for rectangle
               int area() {
                              intarea;
                              area=dim1*dim2;
                              System.out.println("Area of the Rectangle is: " + area);
                              returnarea;
               }
}

class Triangle extends Figure{
               Triangle(inta, intb){
                              super(a,b);
               }
//override area for triangle
               int area() {
                              intarea;
                              area=(dim1*dim2)/2;
                              System.out.println("Area of the Triangle is: " + area);
                              returnarea;
               }
}
publicclassRunTimePoly {

               publicstaticvoid main(String[] args) {
                              Figure f=newFigure(10,10);
                              Rectangle r= newRectangle(9,5);
                              Triangle t=newTriangle(12,3);
                              Figure figerf;
                              figerf=r;
                              System.out.println("Area of the Rectangle is: " + figerf.area());
                              figerf=t;
                              System.out.println("Area of the Triangle is: " + figerf.area());
                              figerf=f;
                              System.out.println("Area of the Figure is: " + figerf.area());
               }

}


Output:
Area of the Rectangle is: 45
Area of the Rectangle is: 45
Area of the Triangle is: 18
Area of the Triangle is: 18
The Area of the Figure is Undefined.
Area of the Figure is: 0
Program 21: Abstract Class
abstractclass A4{
               abstractvoidcallme();
               voidcallmetoo()
                              {
                              System.out.println("This is a concrete method.");
                              }
}
class B4 extendsA4{
               voidcallme() {
                              System.out.println("B's is implemented of call me.");
               }
}
publicclass Abstract {

               publicstaticvoid main(String[] args) {
               B4 b = newB4();
               b.callme();
               b.callmetoo();
               }

}

Output:
B's is implemented of call me.
This is a concrete method.

Program 20: final variable
packageend_term;
class Bike{
               finalintspeedlimit=90;//final variable
               void run() {
                              speedlimit=400;// final field sppedlimit cannot be further assigned
               }
}
publicclassFinalVaria {

               publicstaticvoid main(String[] args) {
                              Bike ob=newBike();
                              ob.run();

               }

}

Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
               The final field Bike.speedlimit cannot be assigned

               atPracticeSt/end_term.Bike.run(FinalVaria.java:5)
               atPracticeSt/end_term.FinalVaria.main(FinalVaria.java:12)

Program 21: final Parameter
packageend_term;
classBike1{
               int cube(finalinta) {
                              a=a+2;//Error a cannot be modified
                              returna;
               }
}
publicclassFinalParameter {

               publicstaticvoid main(String[] args) {
                              Bike1ob=newBike1();
                              ob.cube(10);

               }

}

Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
               The final local variable a cannot be assigned. It must be blank and not using a compound assignment

               atPracticeSt/end_term.Bike1.cube(FinalParameter.java:4)
               atPracticeSt/end_term.FinalParameter.main(FinalParameter.java:12)


Program 22: Static Blank Variable
packageend_term;
class S{
               staticfinalintdata;
               static{
                              data=5;
               }
}
publicclassStaticBlankFinal {
              
               publicstaticvoid main(String[] args) {
                              System.out.println(S.data);
               }

}

Output:
5
Program 23: Blank Final Variable
packageend_term;

publicclass BFV {
finalintspeed;//blank final variable
BFV(){
               speed=90;
               System.out.println("The speed is: " + speed);
}
               publicstaticvoid main(String[] args) {
                              new BFV();

               }

}
Output:
The speed is: 90

Program 24:

Output:
Program 25:

Output:

Program 26:

Output:
Program 27:

Output:

Program 28:

Output:
Program 29:

Output:

Program 30:

Output:
Program 31:

Output:

Program 32:

Output:
Program 33:

Output:

Program 34:

Output:
Program 35:

Output:


No comments:

Post a Comment

Java Notes by Priyanka

Packages ·        Packages are containers for classes. They are used to keep the class name space compartmentalized. ·        A packa...