Create a class to represent a Rectangle.
Your class should contain instance variables for length and
width, as well as member method to calculate the area and perimeter.
```
public class Rectangle
{
private double length;
private double width;
public Rectangle()
{
length = 0;
width = 0;
}
public Rectangle(double l, double w)
{
setLength(l);
setWidth(w);
}
public void setLength(double l)
{
if(l >= 0)
length = l;
else
System.out.println("Fatal error: Length may not be negative!");
}
public void setWidth(double w)
{
if(w >= 0)
width = w;
else
System.out.println("Fatal error: Width may not be negative!");
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public double getArea()
{
return length * width;
}
public double getPerimeter()
{
return 2 * (length + width);
}
public String toString()
{
return "Length = " + length + "\nWidth = " + width;
}
public boolean equals(Object o)
{
if(o == null)
return false;
else if(getClass() != o.getClass())
return false;
else
{
Rectangle otherRectangle = (Rectangle) o;
return((length == otherRectangle.length) &&
(width == otherRectangle.width));
}
}
}
```
You might also like to view...
Which is not a format control string flag?
a) – b) space c) newline d) #
An _________ is a control structure that contains the key information needed by the operating system for a particular file.
Fill in the blank(s) with the appropriate word(s).