Question
Design a public class named Rectangle to represent a rectangle following the example of the Circle class we did in Lesson #5. The Rectangle class
Design a public class named Rectangle to represent a rectangle following the example of the Circle class we did in Lesson #5.
The Rectangle class contains:
Data fields:
o private double data field width with default value 2
o private double data field length with default value 3
Two constructor methods:
o A no-argument constructor that creates a default rectangle with the default values
o A constructor that creates a rectangle with the specified width and length in the formal parameters
Instance methods:
o A public method named getPerimeter() that returns the perimeter of the rectangle (double)
o A public method named getArea() that returns the area of the rectangle (double)
o Public getter methods to access the data fields, specifically:
getWidth()
getLength()
o Public setter methods to set the data fields to values passed in
setWidth (double newWidth)
setLength(double newLength)
Write a simple test program call TestRectangle like we did in class that creates three Rectangle objects:
rect1 with default values for width and length (call the non-argument constructor)
rect2 with width = 18.5 and length = 33.92
rect3 with width default values for width and length (call the non-argument constructor)
However, you will test the setter methods and set the width and length as follows:
o Set the width as a random number between 5 and 10 (excluding 5 and 10)
o Set the length as a random number between 10 and 20 (excluding 10 and 20)
o Check out Slide #40 Lesson #3 to see how to generate random numbers between two numbers
For each rectangle, print out: o width, length, area, and perimeter o Print each element to 2 significant digits (they are all doubles)
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