Answer to Question #246989 in Java | JSP | JSF for raj

Question #246989

Create a class named 'Rectangle' with two datamembers 'length' and 'breadth' andtwo methods to print the area and perimeter of the rectangle respectively. Its constructorhaving parameters for length and breadth is used to initialize length and breadth of therectangle. Let class 'Square' inherit the 'Rectangle' class with its constructor having a

parameter for its side (suppose s) calling the constructor of its parent class as'super(s,s)'. Print the area and perimeter of a rectangle and a square.


1
Expert's answer
2021-10-05T18:15:01-0400
public class Rectangle {
    private double length;
    private double breadth;

    public Rectangle(double length, double breadth) {
        this.length = length;
        this.breadth = breadth;
    }

    public void area() {
        System.out.println(breadth * length);
    }

    public void perimeter() {
        System.out.println(2 * (breadth + length));
    }
}


public class Square extends Rectangle {
    public Square(double side) {
        super(side, side);
    }
}


public class Main {
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle(10, 5);
        Rectangle square = new Square(10);
        rectangle.area();
        rectangle.perimeter();
        System.out.println();
        square.area();
        square.perimeter();
    }
}

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