Source of MeasuringStuff.java


  2: /**
  3:  * A program to test out the Measurable interface. This version uses only the
  4:  * two abstract methods -- getArea and getPerimeter.
  5:  *
  6:  * @author Mark Young (A00000000)
  7:  */
  8: public class MeasuringStuff {

 10:     public static void main(String[] args) {
 11:         // create the shapes
 12:         Circle c1 = new Circle(10.0);
 13:         Circle c2 = new Circle(24.7);
 14:         Rectangle r1 = new Rectangle(10.0, 20.0);
 15:         Rectangle r2 = new Rectangle(0.4, 1000.0);

 17:         // print out their basic information
 18:         printCircleInfo("c1", c1);
 19:         printCircleInfo("c2", c2);
 20:         printRectangleInfo("r1", r1);
 21:         printRectangleInfo("r2", r2);

 23:         // print out their measurable information
 24:         printMeasurableInfo("c1", c1);
 25:         printMeasurableInfo("c2", c2);
 26:         printMeasurableInfo("r1", r1);
 27:         printMeasurableInfo("r2", r2);

 29:         // print out their roundnesses
 30:         System.out.println("The roundness of c1 is " + roundness(c1));
 31:         System.out.println("The roundness of c2 is " + roundness(c2));
 32:         System.out.println("The roundness of r1 is " + roundness(r1));
 33:         System.out.println("The roundness of r2 is " + roundness(r2));
 34:     }

 36:     // this method accepts Circles, but not Rectangles
 37:     private static void printCircleInfo(String name, Circle circ) {
 38:         System.out.println(name + " is a circle of radius " + circ.getRadius());
 39:     }

 41:     // this method accepts Rectangles, but not Circles
 42:     private static void printRectangleInfo(String name, Rectangle rect) {
 43:         System.out.println(name + " is a " + rect.getLength() + "x" 
 44:                 + rect.getWidth() + " rectangle");
 45:     }

 47:     // this method accepts any object that implements the Measurable interface
 48:     // which includes both Circles and Rectangles
 49:     private static void printMeasurableInfo(String name, Measurable m) {
 50:         System.out.println("Measures of " + name + ":");
 51:         System.out.println("\tArea:       " + m.getArea());
 52:         System.out.println("\tPerimeter:  " + m.getPerimeter());
 53:     }

 55:     // this method likewise accepts any Measurable object
 56:     private static double roundness(Measurable m) {
 57:         return 4 * Math.PI * m.getArea() / Math.pow(m.getPerimeter(), 2);
 58:     }

 60: }