calculate area of rectangle using constructor overloading in java

Solutions on MaxInterview for calculate area of rectangle using constructor overloading in java by the best coders in the world

showing results for - "calculate area of rectangle using constructor overloading in java"
Wilbur
12 Oct 2020
1import java.util.*;
2class RectangleShape
3{
4   int length, breadth;
5   // rectangle constructor java
6   RectangleShape(int l, int b) 
7   { 
8      length = l; 
9      breadth = b; 
10   } 
11   RectangleShape(int l) 
12   { 
13      length = l; 
14      breadth = 20; 
15   } 
16   RectangleShape() 
17   { 
18      length = 6; 
19      breadth = 2; 
20   } 
21   float getArea() 
22   { 
23      return(length * breadth); 
24   }
25}
26public class FindArea 
27{
28   public static void main(String[] args)
29   {
30      RectangleShape rs1 = new RectangleShape(); 
31      RectangleShape rs2 = new RectangleShape(60); 
32      RectangleShape rs3 = new RectangleShape(40, 20); 
33      System.out.println("First rectangle : " + rs1.getArea()); 
34      System.out.println("Second rectangle : " + rs2.getArea()); 
35      System.out.println("Third Rectangle : " + rs3.getArea());
36   }
37}