You can use this assignment in your class!
Mastery Progress(100.0%)
i

Unit 9: Inheritance - Review Assignment

Create a subclass with multiple constructors

Create a class named Rectangle with two instance variables length and width of type int. The Rectangle class should have a constructor that accepts two int parameters and assigns them to length and width. Then, create a subclass named Square that extends Rectangle. The Square class should have two constructors: one that accepts a single int parameter and passes it to the superclass constructor twice, and another that accepts no parameters and passes the number 1 to the superclass constructor twice.

You answeredJUST NOW
Java
import java.io.*;
import java.util.*;
class Rectangle {
int length;
int width;
Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
}
class Square extends Rectangle {
// WRITE YOUR CODE HERE
}
// Testing mechanism - do not modify!
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Square square1 = new Square(in.nextInt());
System.out.println(square1.length);
System.out.println(square1.width);
Square square2 = new Square();
System.out.println(square2.length);
System.out.println(square2.width);
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
You passed all test cases!
STDIN
5
Expected STDOUT
5
5
1
1
Your STDOUT
5
5
1
1
STDIN
1
Expected STDOUT
1
1
1
1
Your STDOUT
1
1
1
1
STDIN
9
Expected STDOUT
9
9
1
1
Your STDOUT
9
9
1
1

Did you like this question?

(Voting helps us personalize your learning experience!)
imgInstructor solution
Alec KretchAPR 24, 2024, 8:16:56 AM
Java
import java.io.*;
import java.util.*;
class Rectangle {
int length;
int width;
Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
}
class Square extends Rectangle {
Square(int side) {
super(side, side);
}
Square() {
super(1, 1);
}
}
// Testing mechanism - do not modify!
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Square square1 = new Square(in.nextInt());
System.out.println(square1.length);
System.out.println(square1.width);
Square square2 = new Square();
System.out.println(square2.length);
System.out.println(square2.width);
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Was this helpful?

(Voting helps us personalize your learning experience!)
Think you've got it?

How do you create multiple constructors in a subclass in Java?

Select one of the following options:
  • A.

    By defining a single constructor in the subclass with multiple parameters and calling super with different arguments based on conditional statements.

  • B.

    By defining multiple constructors in the superclass and calling super with the same arguments in the subclass constructor.

  • C.

    By defining multiple constructors in the subclass with different parameters and calling super with the appropriate arguments in each constructor.

  • D.

    By defining a single constructor in the subclass with no parameters and calling super with different arguments based on user input.

Submit answer

Was this helpful?

(Voting helps us personalize your learning experience!)

You may exit out of this review and return later without penalty.