Source of MeasuringStuff.java


  1: /**
  2:  *  An example of using a simple interface
  3:  * See also:
  4:  *  Measurable.java
  5:  *  Circle.java
  6:  *  Rectangle.java
  7:  *
  8:  * @author Mark Young (A00000000)
  9:  */
 10: public class MeasuringStuff {

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

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

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

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

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

 43:     // this method accepts Rectangles, but not Circles
 44:     public static void printRectangleInfo(String name, Rectangle r) {
 45:         System.out.println(name + " is a " + r.getWidth() + "x" + r.getHeight()
 46:             + " rectangle");
 47:     }

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

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

 62: }