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. A class with stubs for methods will compile, but it does not yet behave correctly. You still need to supply implementation and the correct return values. The idea is that you can implement one method at a time and test it since the class will compile. This technique is frequently used in development of applications. You are given a RectangleTester class in Codecheck along with the starter for the Rectangle class. Copy both into your BlueJ project. RectangleTester is an application with a main method. To run your application, right click on it and execute its main method.
In java language
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.
A class with stubs for methods will compile, but it does not yet behave correctly. You still need to supply implementation and the correct return values. The idea is that you can implement one method at a time and test it since the class will compile. This technique is frequently used in development of applications.
You are given a RectangleTester class in Codecheck along with the starter for the Rectangle class. Copy both into your BlueJ project. RectangleTester is an application with a main method. To run your application, right click on it and execute its main method.
RectangleTester.java
public class RectangleTester { public static void main(String[] args) { Rectangle r1 = new Rectangle(); r1.setWidth(3.0); r1.setLength(5.2); System.out.printf("r1 Width: %.2f%n", r1.getWidth()); System.out.println("Expected: 3.00"); System.out.printf("r1 Length: %.2f%n", r1.getLength()); System.out.println("Expected: 5.20"); Rectangle r2 = new Rectangle(2.6, 5.4); System.out.printf("r2 Width: %.2f%n", r2.getWidth()); System.out.println("Expected: 2.60"); System.out.printf("r2 Length: %.2f%n", r2.getLength()); System.out.println("Expected: 5.40"); System.out.printf("r1 Area: %.2f%n", r1.area()); System.out.println("Expected: 15.60"); System.out.printf("r1 Perimeter: %.2f%n", r1.perimeter()); System.out.println("Expected: 16.40"); System.out.printf("r2 Area: %.2f%n", r2.area()); System.out.println("Expected: 14.04"); System.out.printf("r2 Perimeter: %.2f%n", r2.perimeter()); System.out.println("Expected: 16.00"); System.out.println(); r1.printDimensions(); } }
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images