Question: You will write a new class called Square that inherits from Rectangle. Is a square a rectangle? Yes! A square is a rectangle where the

You will write a new class called Square that inherits from Rectangle. Is a square a rectangle? Yes! A square is a rectangle where the length and width are equal. Square will inherit length, width, and the draw method. You will write square constructors that will call the Rectangle constructors.

Make the class Square below inherit from Rectangle

Add a Square no-argument constructor that calls Rectangles constructor using super().

Add a Square constructor with 1 argument for a side that calls Rectangles constructor with 2 arguments using super.

Uncomment the objects in the main method to test drawing the squares.

Add an area() method to Rectangle that computes the area of the rectangle. Does it work for squares too? Test it.

Add another subclass called LongRectangle which inherits from Rectangle but has the additional condition that the length is always 2 x the width. Write constructors for it and test it out.

Create a Square class that inherits from Rectangle.

Please edit and fix this code for the question in java:

class Rectangle {

private int length;

private int width;

public Rectangle() {

length = 1;

width = 1;

}

public Rectangle(int l, int w) {

length = l;

width = w;

}

public void draw() {

for(int i=0; i < length; i++) {

for(int j=0; j < width; j++)

System.out.print("* ");

System.out.println();

}

System.out.println();

}

public int area() {

return length * width;

}

}

public class Square extends Rectangle {

public Square() {

super(1, 1);

}

public Square(int s) {

super(s, s);

}

public static void main(String[] args) {

Rectangle r = new Rectangle(3, 5);

r.draw();

Square s1 = new Square();

s1.draw();

Square s = new Square(3);

s.draw();

System.out.println("Area of s: " + s.area());

LongRectangle lr = new LongRectangle(4);

lr.draw();

System.out.println("Area of lr: " + lr.area());

}

}

class LongRectangle extends Rectangle {

public LongRectangle(int w) {

super(2 * w, w);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!