Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The java code for square. /* * The square class models a square with a length and color. */ public class Square { // Save
The java code for square.
/* * The square class models a square with a length and color. */ public class Square { // Save as "Square.java" // private instance variable, not accessible from outside this class private double length; private String color; // The default constructor with no argument. // It sets the length and color to their default value. public square() { length = 1.0; color = "red"; // 2nd constructor with given length, but color default public square(double r) { length = r; color = "red" } // A public method for retrieving the length public double getlength() { return length; } // A public method for computing the area of square public double getArea() { return length*length; } }In Labl, you were provided with a Java class, called Square.java, which contained: Two private instance variables: length of the type double) and color (of the type String), with default value of 1.0 and "red", respectively. . Two overloaded constructors - a default constructor with no argument, and a constructor which takes a double argument for length. . Two public methods: getLength() and getArea (), which return the length and area of this instance, respectively. The source codes for square.java is in the file square.java which you submitted. [Task 1] In this task, you will create a subclass called Rectangle which is derived from the superclass Square. Study how the subclass Rectangle invokes the superclass' constructors (via super() and super(length)) and inherits the variables and methods from the superclass Square. You can reuse the Square class that you have created in the previous exercise. Make sure that you keep "Square.java" in the same directory. A partially complete (Skeleton) Rectangle.class code is shown below. public class Rectangle extends Square { // Save as "Rectangle.java" private double width; // private variable // Constructor with default color, length and width public Rectangle() { super(); // call superclass no-arg constructor Square () width = 1.0; , constructor with default length, color but given width public Rectangle (double width) { //put your code here } // Constructor with default color, but given length, width public Rectangle (double length, double width) { //put your code here } // A public method for retrieving the width public double getWidth() { //put your code here } // A public method for computing the area of Rectangle public double getArea() { //put your code here }
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