Untitled
unknown
plain_text
2 years ago
6.5 kB
15
Indexable
public class ShapeTest
{
public static void main(String args[])
{
Shape[] shapes = new Shape[6];
shapes[0] = new Point (7, 11);
shapes[1] = new Circle (22, 8, 3.5);
shapes[2] = new Square (5, 8, 7.0);
shapes[3] = new Cylinder (10, 10, 3.3, 10);
shapes[4] = new Point (10, 4);
shapes[5] = new Cylinder (5, 7, 8.3, 5);
for (int i = 0; i < 6; i++)
{
System.out.print ((shapes[i]).name());
System.out.print (": ");
shapes[i].print();
System.out.println ("\nArea = " + (Math.round(shapes[i].area())));
System.out.println ("Volume = " + (Math.round(shapes[i].volume())) + "\n");
}
} // end of main
} // end of ShapeTest
//===================================================================
// CLASS Shape is the root of our general hierarchy of shapes.
// All shapes have these four methods (area,volume,name and print).
//===================================================================
abstract class Shape
{
// area is zero by default if not overridden by a subclass
public double area()
{
return 0.0;
}
// volume is zero by default if not overridden by a subclass
public double volume()
{
return 0.0;
}
// non-abstract subclasses must define a name method, which
// returns the name of the class
public abstract String name();
// non-abstract subclasses must define a print method, which
// prints out the class' attributes.
public abstract void print();
} // end of Shape
//===================================================================
// CLASS Point is a degenerate "shape", consisting of only a
// pair of coordinates.
//===================================================================
class Point extends Shape
{
protected double x, y;
public Point(double new_x, double new_y)
{
setPoint(new_x,new_y);
}
public void setPoint(double new_x, double new_y)
{
x = new_x;
y = new_y;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public String name()
{
return "Point";
}
// print out the coordinates in "[x, y]" format
public void print()
{
System.out.print("["+getX()+", "+getY()+"]");
}
} // end of Point
//===================================================================
// CLASS Circle uses Point to define the center of the circle, and
// also has a separate variable for the radius.
//===================================================================
class Circle extends Point
{
protected double radius;
public Circle(double new_x, double new_y, double new_radius)
{
super(new_x,new_y); // the Point is the center of the Circle
setRadius(new_radius);
}
public void setRadius(double new_radius)
{
radius = new_radius;
}
public double getRadius()
{
return radius;
}
// use grade-school pi*r^2 formula for the area
public double area()
{
// I assigned the value of getRadius() to a temporary variable
// instead of doing getRadius()*getRadius() to avoid calling
// the same method twice. We're not particularly concerned
// about performance in this class, so don't worry too much
// about this.
double currentRadius = getRadius();
// 3.14 instead of Math.PI is fine for this assignment
return Math.PI * currentRadius * currentRadius;
}
public String name()
{
return "Circle";
}
// print out circle info in "Center=[x, y]; Radius=r" format, using
// superclass' print() to print out the center coords.
public void print()
{
System.out.print("Center=");
super.print();
System.out.print("; Radius="+getRadius());
}
} // end of Circle
//===================================================================
// CLASS Square uses Point to define the start of the square (I
// assume would be one of the corners, but which corner is not
// important for this assignment). A separate variable for the
// side length of the square is also maintained.
//===================================================================
class Square extends Point
{
protected double length;
public Square(double new_x, double new_y, double new_length)
{
super(new_x,new_y); // the Point is the start of the square
setLength(new_length);
}
public void setLength(double new_length)
{
length = new_length;
}
public double getLength()
{
return length;
}
// square's area is its side squared
public double area()
{
double currentLength = getLength();
return currentLength * currentLength;
}
public String name()
{
return "Square";
}
// print out square info in "Start=[x, y]; Length=l" format, using
// superclass' print() to print out the start coords.
public void print()
{
System.out.print("Start=");
super.print();
System.out.print("; Length="+getLength());
}
} // end of Square
//===================================================================
// CLASS Cylinder uses Circle to define the top/bottom of the
// cylinder, plus another variable height to store the height of
// the shape.
//===================================================================
class Cylinder extends Circle
{
protected double height;
public Cylinder(double new_x, double new_y, double new_radius, double new_height)
{
super(new_x,new_y,new_radius); // the Circle defines the top/bottom of the Cylinder
setHeight(new_height);
}
public void setHeight(double new_height)
{
height = new_height;
}
public double getHeight()
{
return height;
}
// surface area = 2*endArea + sidewall
// end area = circle's area (uses super.area())
// sidewall = circumference * height = (2*pi*r)*h
public double area()
{
// endArea is the area of the two ends of the cylinder
double endArea = 2*super.area();
// total area is equal to the sum of the ends + the sidewall (circumference * height)
return endArea + 2*Math.PI*getRadius()*getHeight();
}
// cylinder's volume is its end area (top or bottom) times its height.
public double volume()
{
return super.area() * getHeight();
}
public String name()
{
return "Cylinder";
}
// print out circle info in "Center=[x, y]; Radius=r; Height = h" format, using
// superclass' print() to print out the center and radius info
public void print()
{
super.print();
System.out.print("; Height="+getHeight());
}
} // end of Cylinder
Editor is loading...