Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part I: Create a class named Rectangle.java with attributes length and width (bo

ID: 3847308 • Letter: P

Question

Part I:

Create a class named Rectangle.java with attributes length and width (both should be defined as double), each of which defaults (non-parameter constructor) to 1.0. The second constructor should accept the length and width as as arguments to initialize a Rectangle's length and width fields. Provide methods that calculate the rectangle's perimeter and area. It has set and get methods for both length and width. The set methods should verify that length and width are each floating-point numbers larger than 0.0 and less than 50.0. Provide toString method that returns its rectangle information in the format as shown below:

[length: 2.00, width: 2.00, Perimeter: 8:00, area: 4.00 ]

methods headers:

public double perimeter()

public double area()

Part II:

Write a program called RectangleDriver.java to test Rectangle class with the following code (simply copy the following code into your main method):

Rectangle rec=new Rectangle(1.0, 5.0);

Rec.setLength(-2.50);

System.out.println(rec);

Explanation / Answer

Here is your class Rectangle.java:

public class Rectangle {

public Rectangle() {

//non parameterized constructor

}

private double length = 1.0;//declaration of length
private double width = 1.0;//declaration of width

public Rectangle(double length, double width) {

//parameterized constructor which accepts length and width as parameters
this.length = length;
this.width = width;
}

public double getLength() {

//returns current length value
return this.length;
}

public boolean setLength(double length) {
// checking for the length value so that it should not accept values less than 0.0
if (length > 0.0 && length < 50.0) {
return true;
}
return false;
}

public double getWidth() {

//returns the current value of width
return this.width;
}

public boolean setWidth(double width) {
// checking for the width value so that it should not accept values less than 0.0
if (width > 0.0 && width < 50.0) {
return true;
}
return false;
}

@Override
public String toString() {

//displays values to the console
return "length=" + this.length + ", width=" + this.width + ",perimeter=" + perimeter() + ",area=" + area();
}

public double perimeter() {

//returns the perimeter after calculation
return 2 * (this.length + this.width);
}

public double area() {

//returns area after calculating
return this.length * this.width;
}
}

To check the correctness of the provided values we need to call Rectangle class by creating its object and passing the length and width values to the rectangle object.

and we are setting length value to negative count to check whether it accepts it or not.

Here is our Test Class named RectangleDriver.java:

public class RectangleDriver {

public static void main(String[] args) {
Rectangle rec=new Rectangle(1.0, 5.0);
rec.setLength(-2.50);
System.out.println(rec);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote