Rectangle

Updated Dani 0 Tallied Votes 2K Views Share

A program similar to CircleClass.java which defines a rectangle object. ClientProgram.java is the driver program for the Rectangle.java file.

// Rectangle.java:

package rectangle;
public class Rectangle
{
    private double width = 1;
    private double height = 1;
    private static String color = "white";
    public Rectangle() { }
    public Rectangle(double w, double h, String c)
    {
        width = w;
        height = h;
        color = c;
    }
    public double getWidth() { return width; }
    public void setWidth(double w) { width = w; }
    public double getHeight() { return height; }
    public void setHeight(double h) { height = h; }
    public static String getColor() { return color; }
    public static void setColor(String c) { color = c; }
    public double findArea() { return height * width; }
}

// ClientProgram.java

package rectangle;
public class ClientProgram
{
    public static void main(String args[])
    {
        Rectangle mySquare = new Rectangle(5, 5, "green");
        System.out.print("\n Height = " + mySquare.getHeight() );
        System.out.print("\n Width = " + mySquare.getWidth() );
        System.out.print("\n Color = " + mySquare.getColor() );
        mySquare.setHeight(10);
        mySquare.setWidth(15);
        System.out.print("\n\n Height = " + mySquare.getHeight() );
        System.out.print("\n Width = " + mySquare.getWidth() );
        System.out.print("\n Area = " + mySquare.findArea() );
    }
}