// Name:
Bradley Shedd
// Program: Quiz#1
// Date: October
6, 2010
// Class: Java1302
// Filename: Rectangle.java
public
class Rectangle
{
private
float length;
private
float width;
public
Rectangle()
{
length = 1.0f;
width = 1.0f;
}
public
Rectangle(
float newLength,
float
newWidth)
{
setLength (newLength);
setWidth (newWidth);
}
public
float calculatePerimeter()
{
if
( length !=0.0 && width != 0.0)
return
(length + width) * 2;
else
return
0.0f;
}
public
float calculateArea()
{
if
( length !=0.0 && width != 0.0 )
return
length * width;
else
return
0.0f;
}
public
float getLength()
{
return
length;
}
public
float getWidth()
{
return
width;
}
public
void setLength(
float
newLength )
{
if
( newLength > 0.0 && newLength < 20.0)
length = newLength;
else
{
System.out.println(
"Length can't be negative or larger than 20." );
}
}
public
void setWidth(
float
newWidth )
{
if
( newWidth > 0.0 && newWidth < 20.0 )
width = newWidth;
else
{
System.out.println(
"Width can't be negative or larger than 20." );
}
}
public
String toString()
{
return
"Length:\t" + length +
"\nWidth:\t"
+ width +
"\nPerimeter:\t"
+
this.calculatePerimeter() +
"\nArea:\t"
+
this.calculateArea();
}
public
boolean equals ( Object a1 )
{
if
( ! ( a1
instanceof Rectangle))
return
false;
else
{
Rectangle rec1 = ( Rectangle ) a1;
return
Math.abs( calculateArea () - rec1.calculateArea()) < 0.0001;
}
}
}