Write a Rectangle class. A Rectangle has properties of width and length. You construct a Rectangle object by providing the width and length in that order. If no width and length are provided to the constructor, construct a Rectangle with width 0.0 and length 0.0. We want to be able to get and set both the width and the length independently. We also want to be able to ask for the area of the rectangle and the perimeter of the rectangle. What will the object need to remember? width and length - those are the instance variables. Rectangle class has these constructors : public Rectangle() - Constructs a new rectangle with width and length of 0.0. public Rectangle(double width, double length) - Constructs a new rectangle with the given width and length. Remember that the job of the constructor is to initialize the instance variables. It has these methods. public double getWidth() - Gets the width of this Rectangle public double getLength() - Gets the length of this Rectangle public void setWidth(double aWidth) - Sets a new width for this Rectangle public void setLength(double aLength) - Sets a new length for this Rectangle public double area() - Calculates and returns this Rectangle's area (product of width and length) public double perimeter() - Calculates and returns this Rectangle's perimeter (sum of all four sides) public void printDimensions() - Displays the width and length of this Rectangle in the following format (to two decimal places): Width: 3.48 Height: 7.27 The methods and constructor are provided as stubs in the starter file. A stub has a method header and a body with no implementation. The stub for an accessor returns 0 for numbers or null for objects like strings. A stub for a mutator method has no body at all.
Write a Rectangle class. A Rectangle has properties of width and length. You construct a Rectangle object by providing the width and length in that order. If no width and length are provided to the constructor, construct a Rectangle with width 0.0 and length 0.0. We want to be able to get and set both the width and the length independently. We also want to be able to ask for the area of the rectangle and the perimeter of the rectangle.
What will the object need to remember? width and length - those are the instance variables.
Rectangle class has these constructors :
- public Rectangle() - Constructs a new rectangle with width and length of 0.0.
- public Rectangle(double width, double length) - Constructs a new rectangle with the given width and length. Remember that the job of the constructor is to initialize the instance variables.
It has these methods.
- public double getWidth() - Gets the width of this Rectangle
- public double getLength() - Gets the length of this Rectangle
- public void setWidth(double aWidth) - Sets a new width for this Rectangle
- public void setLength(double aLength) - Sets a new length for this Rectangle
- public double area() - Calculates and returns this Rectangle's area (product of width and length)
- public double perimeter() - Calculates and returns this Rectangle's perimeter (sum of all four sides)
- public void printDimensions() - Displays the width and length of this Rectangle in the following format (to two decimal places):
- Width: 3.48
- Height: 7.27
The methods and constructor are provided as stubs in the starter file. A stub has a method header and a body with no implementation.
- The stub for an accessor returns 0 for numbers or null for objects like strings.
- A stub for a mutator method has no body at all.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images