Create two classes The first class name is Rectangle o It ha
Create two classes:
The first class name is Rectangle.
o It has two private instance variables, both doubles, named length and width
o The class has a private helper method named orderLengthAndWidth. It ensures that the value of the length variable is always greater than or equal to the width. Any time a calling program changes the length or width of this Rectangle object, this method should be invoked to ensure that the length is always the greater of the length and width of the Rectangle object.
o The class has one constructor with the signature:
Rectangle(double length, double width)
The constructor will ensure that the number stored as the length of the Rectangle is greater than the width by invoking the orderLengthAndWidth method.
o The class has accessors (getters) and mutators (setters) for the length and width. The mutators also will ensure that the length is greater than the width by invoking the orderLengthandWidth helper method.
o The class has a private method named computeArea that calculates the area of the rectangle (length x width) and returns it in a double
o The class has a public method named isSquare that returns true if this Rectangle object is a square (length = width) or false otherwise
o The toString method will return a String containing a description of the Rectangle that looks something like:
The rectangle’s length is (xx.xx), width is (yy.yy), and area is (zz.zz)
All numbers will be printed to two decimal places.
The next three methods (isLargerThan, isTheSameSize, and equals), are public and take another Rectangle object as a parameter. If the Rectangle passed as a parameter has not been initialized (i.e., is equal to null), then return false.
o The method named isLargerThan compares the area of this Rectangle with the area of the other rectangle. If the area of this rectangle is larger, then the method returns a boolean value of true, otherwise it returns false
o The method named isTheSameSize returns true if the area of this Rectangle is equal to the area of the other Rectangle object, otherwise it returns false.
o The equals method returns a Boolean value true if the length and width of this Rectangle are equal to the length and width of the other Rectangle object.
The second class will be a “driver” program (one that contains main()) to exercise the Rectangle class described on the prior page.
This program will:
1. Create a Rectangle object, passing an int of 40 for the length and a double of 35.5 as the width to the constructor. Print the details of the Rectangle using the toString() method
2. Create a second Rectangle using an int of 37 as both the length and width for the constructor. Print the details of the second Rectangle using the toString() method
3. Print whether the first and second Rectangles have the same area. If they are not the same size, then determine if the first Rectangle is larger or smaller than the second Rectangle.
4. Print whether the first and second Rectangles are squares.
5. Create a third Rectangle using doubles of 20.0 as the length and 71.0 as the width for the constructor. Print the details of the third Rectangle using toString()
6. Print whether the first and third Rectangles have the same area
7. Print whether the first and third Rectangles are equal
8. Create a fourth Rectangle using 35.5 as the length and 40 for the width for the constructor. Print the details of the fourth Rectangle
9. Print whether the third and fourth Rectangles have the same area.
10. Print whether the third and fourth Rectangles are equal.
11. Set the variable for the fourth Rectangle to null.
12. Print whether the third and fourth Rectangles have the same area.
13. Print whether the third and fourth Rectangles are equal.
Solution
package org.students;
import java.text.DecimalFormat;
public class Rectangle {
private double length;
private double width;
DecimalFormat df=null;
public Rectangle(double length, double width) {
super();
try
{
if(orderLengthAndWidth()==true)
{
df=new DecimalFormat(\"#.##\");
this.length = length;
this.width = width;
}
}
catch (Exception e)
{
System.out.println(\":: Length must be greater than width ::\");
System.out.println(e);
}
}
public double getLength() {
return length;
}
public void setLength(double length) {
if(orderLengthAndWidth())
{
this.length = length;
}
else
{
System.out.println(\":: Length must be greater than width ::\");
}
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
if(orderLengthAndWidth())
{
this.width = width;
}
else
{
System.out.println(\":: Length must be greater than width ::\");
}
}
private double computeArea()
{
return length*width;
}
public boolean isSquare()
{
boolean bool;
if(getLength()==getWidth())
bool=true;
else
bool=false;
return bool;
}
private boolean orderLengthAndWidth()
{
boolean bool=false;
if(length<width)
bool= false;
else if(length>width)
bool=true;
return bool;
}
public boolean isLargerThan(Rectangle rect)
{
boolean bool=false;
if(rect==null)
bool=false;
else if(this.computeArea()>rect.computeArea())
bool=true;
else if(this.computeArea()<rect.computeArea())
bool=false;
return bool;
}
public boolean isTheSameSize(Rectangle rect)
{
boolean bool=false;
if(rect==null)
bool=false;
else if(this.computeArea()==rect.computeArea())
bool=true;
else if(this.computeArea()==rect.computeArea())
bool=false;
return bool;
}
public boolean equals(Rectangle rect)
{
boolean bool=false;
if(rect==null)
bool=false;
else if(this.getLength()==rect.getLength() && this.getWidth()==this.getWidth())
bool=true;
else
bool=false;
return bool;
}
@Override
public String toString() {
return \"The rectangle’s length is \"+df.format(getLength())+\", width is \"+df.format(getWidth())+\", and area is \"+df.format(computeArea());
}
}
_______________________________________
I will complete this....



