Answer to Question #318571 in Java | JSP | JSF for superBoy

Question #318571

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.


1
Expert's answer
2022-03-26T08:42:36-0400
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()));
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS