Problem:
Write a class Rectangle. The class has attributes length and width, each of which defaults to 1. It has methods that calculate the perimeter and the area of the rectangle. 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 20.0. Include a predicate method isSquare which determines if the rectangle is a square.
Solution:
public class sophiRectangle
{
private float length, width;
public sophiRectangle()
{
length = 1;
width = 1;
}
public float doPerimeter()
{
return (length+width)*2;
}
public float doArea()
{
return length*width;
}
public void setValues(float length,float width)
{
if (length > 0 && length < 20)
this.length = length;
if (width > 0 && width < 20)
this.width = width;
}
public float doLength()
{
return length;
}
public float doWidth()
{
return width;
}
public boolean isSquare()
{
if (length == width)
return true;
else
return false;
}
}
No comments :
Post a Comment