CreateCircle Code

CreateCircle.java

/*
 * Mark Hesser
 * HesserCAN 
 * [email protected]
 * www.hessercan.com
 */
 
package createcircle;
 
/**
 * @author mark
 */
public class CreateCircle 
{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        Circle[] c = new Circle[2];
        c[0] = new Circle("Red", 2);
        c[1] = new Cylinder("Red", 2, 2);
 
        String info = "";
        for (int i = 0; i < c.length; i++)
        {
            if (c.length == (i + 1))
                info += c[i].toString() + "\n";
            else
                info += c[i].toString() + "\n\n";
        }
        DisplayInfo(info);
    }
 
    static void DisplayInfo(String info)
    {
        System.out.println(info);
    }
}
 
/*
The Cylinder class inherits the Color and Radius variables from the Superclass,
as well as the getColor and getRadius Methods. The subclass Cylinder defines
the initizlizes the height variables defines the getHeight and getVolume methods 
to retrieve those values. I have added the setArea and setVolume methods that 
are used to calculated the area and volume of a Cylinder using the Radius and 
Height variables with the Math.PI and Math.pow functions.
*/

Circle.java

/*
 * Mark Hesser
 * HesserCAN 
 * [email protected]
 * www.hessercan.com
 */
 
package createcircle;
 
/**
 * @author mark
 */
public class Circle 
{
    private double radius;
    private String color;
 
    public Circle (String c, double r)
    {
        color = c;
        radius = r;
    }
    public String getColor()
    {
        return color;
    }
    public double getRadius()
    {
        return radius;
    }
 
    @Override
    public String toString()
    {
        return String.format("Color: %s \nRadius: %.2f \n", color, radius);
    }
}

Cylinder.java

/*
 * Mark Hesser
 * HesserCAN 
 * [email protected]
 * www.hessercan.com
 */
 
package createcircle;
 
/**
 * @author mark
 */
public class Cylinder extends Circle
{
    private final double height;
    private double area, volume;
 
    public Cylinder(String c, double r, double h)
    {
        super(c, r);
        height = h;
        setArea(); setVolume();
    }
 
    public void setArea()
    {
        area = (2 * Math.PI * getRadius() * height) + 
                2 * Math.PI * Math.pow(getRadius(), 2);
    }
    public void setVolume()
    {
        volume = Math.PI * Math.pow(getRadius(), 2) * height;
    }
 
    public double getHeight()
    {
        return height;
    }
    public double getArea()
    {
        return area;
    }
    public double getVolume()
    {
        return volume;
    }
 
    @Override
    public String toString()
    {
        return String.format("Color: %s \nRadius: %.2f \nHeight: %.2f "
                + "\nArea: %.2f \nVolume: %.2f", 
                getColor(), getRadius(), height, area, volume);
    }
}