Question
Create a Square class that inherits from Rectangle. class Rectangle { private int length; private int width; public Rectangle() { length = 1; width =
Create a Square class that inherits from Rectangle.
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();
}
}
// 1. Make the class square inherit from Rectangle
class Square extends Rectangle {
// 2. Add a Square no-argument constructor
Square() {
super();
}
// 3. Add a Square constructor with 1 argument for a side
Square(int side) {
super(side, side);
}
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();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started