Create a class to print the area of a rectangle. The class has two methods with the same name but different number of parameters. The method for printing area of rectangle has two parameters which are length and breadth.
import java.util.Scanner;
public class Area {
private double length;
private double breadth;
private double area;
public void setValues(double length, double breadth){
this.length = length;
this.breadth = breadth;
area = this.length * this.breadth;
}
public double getLenght(){
return this.length;
}
public double getBreadth(){
return this.breadth;
}
public double getArea(){
this.area = this.length * this.breadth;
return area;
}
public double getArea(double length, double breadth){
this.area = this.length * this.breadth;
return area;
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
Area area = new Area();
float l;
float b;
System.out.print("Enter the length of rectangle: ");
l = scanner.nextFloat();
System.out.print("Enter breadth of rectangle: ");
b = scanner.nextFloat();
area.setValues(l, b);
System.out.println("The first overloaded method");
System.out.println("Area of rectangle is = " + area.getArea());
System.out.println("\nThe second overloaded method");
System.out.println("Area of rectangle is = " + area.getArea(area.getLenght(), area.getBreadth()));
}
}
Comments
Leave a comment