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 edition of Java is geared toward developing large-scale, distributed networking applications and web-based applications?
a. Standard Edition. b. Industrial Edition. c. Enterprise Edition. d. Micro Edition.
A situation in which multiple threads or processes read and write a shared data item and the final result depends on the relative timing of their execution is a ________
Fill in the blank(s) with the appropriate word(s).
A ____ is a TCP/IP-based concept (container) within Active Directory that is linked to IP subnets.
A. forest B. domain C. site D. tree
When a programmer exploits written code that doesn't check for a defined amount of memory space they are executing which of the following attacks?
A. buffer overflow B. DoS C. DDoS D. session hijacking