Class and Object Example: Area of Rectangle

Java Programming Language Class, Object and Methods in java (Article) Class, Object and Methods in java (Program)

5077

Program:

 // Save it as File name : RectangleClassExample1.java
// Class and Object Example: Initialization through constructor

class Rectangle{
 int length;
 int width;
 
 void insert(int l, int w){
  length=l;
  width=w;
 }
 void calculateArea()
 {
 System.out.println(length*width);
 }
}

class RectangleClassExample1{
 public static void main(String args[]){
  Rectangle r1=new Rectangle();
  Rectangle r2=new Rectangle();
  r1.insert(11,5);
  r2.insert(3,15);
  r1.calculateArea();
  r2.calculateArea();
  }
}

Output:

55
45
Press any key to continue . . .

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.