/** * This class represents a Rectangle. * *
There are two new fields:
* private double height
* private double width
*
* @version 4.0, 9/19/2022
* @author Bob Myers myers@cs.fsu.edu
*/
public class Rectangle
{
/**
* define constant for shortest height
*/
public static final double SHORTEST_HEIGHT = 1.0;
/**
* define constant for shortest width
*/
public static final double SHORTEST_WIDTH = 1.0;
/**
* define constant for default height
*/
public static final double DEFAULT_HEIGHT = 1.0;
/**
* define constant for default width
*/
public static final double DEFAULT_WIDTH = 1.0;
/**
* height of the rectangle
*/
private double height;
/**
* width of the rectangle
*/
private double width;
/**
* Set this object's height value.
* @param h set this object height value to the parameter,
* with a minimum size of SHORTEST_HEIGHT
*/
private void privateSetHeight(double h)
{
if (h < SHORTEST_HEIGHT)
height = SHORTEST_HEIGHT;
else
height = h;
}
/**
* Set this object's width value.
* @param w set this object width value to the parameter,
* with a minimum size of SHORTEST_WIDTH
*/
private void privateSetWidth(double w)
{
if (width < SHORTEST_WIDTH)
width = SHORTEST_WIDTH;
else
width = w;
}
/**
* Complete constructor.
* @param height the requested height
* @param width the requested width
*/
public Rectangle(double height, double width)
{
privateSetHeight(height);
privateSetWidth(width);
}
/**
* Empty constructor.
*
height
and width
set to
* DEFAULT_HEIGHT
and DEFAULT_WIDTH
respectivly
*/
public Rectangle()
{
this(DEFAULT_HEIGHT, DEFAULT_WIDTH);
}
/**
* Copy constructor.
* @param rectangle this object will have height
and
* width
set to copy of incoming rectangle
*/
public Rectangle(Rectangle rectangle)
{
this(rectangle.height, rectangle.width);
}
/**
* This object height value.
* @return this object height value
*/
public double getHeight()
{
return (height);
}
/**
* This object width value.
* @return this object width value
*/
public double getWidth()
{
return (width);
}
/**
* Set this object height value.
* @param height set this object height value to the parameter
*/
public void setHeight(double height)
{
privateSetHeight(height);
}
/**
* Set this object width value.
* @param width set this object width value to the parameter
*/
public void setWidth(double width)
{
privateSetHeight(height);
}
/**
* Complete set.
* @param height height
* @param width width
*/
public void setRectangle(double height, double width)
{
setHeight(height);
setWidth(width);
}
/**
* Empty set.
*
height
and width
set to
* DEFAULT_HEIGHT
and DEFAULT_WIDTH
respectivly
*/
public void setRectangle()
{
setRectangle(DEFAULT_HEIGHT, DEFAULT_WIDTH);
}
/**
* Copy set.
* @param rectangle this object will have height
and
* width
set to copy of incoming rectangle data
*/
public void setRectangle(Rectangle rectangle)
{
setRectangle(rectangle.height, rectangle.width);
}
/**
* The area of this object.
* @return the area of this object
*/
public double area()
{
return (height * width);
}
/**
* The perimeter of this object.
* @return the perimeter of this object
*/
public double perimeter()
{
return (2.0 * (height + width));
}
/**
* A String representation of this object.
* @return Rectangle[height = value, width = value]
*/
public String toString()
{
return("Rectangle[height = " + height + ", width = " + width + "]");
}
}