Source of MeasuringStuff.java


  1: import java.util.Arrays;
  2: import java.util.List;
  3: import java.util.function.Function;

  5: /**
  6:  * A program to demonstrate the {@link Measurable} interface. This version of
  7:  * the program is different from the earlier version in that it uses a version
  8:  * of Measurable that has static methods and constants defined.
  9:  *
 10:  * @author Mark Young (A00000000)
 11:  */
 12: public class MeasuringStuff {

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

 22:         // print out their basic information
 23:         printCircleInfo("c1", c1);
 24:         printCircleInfo("c2", c2);
 25:         printRectangleInfo("r1", r1);
 26:         printRectangleInfo("r2", r2);
 27:         printRectangleInfo("r3", r3);

 29:         // print out their measurable information
 30:         printMeasurableInfo("c1", c1);
 31:         printMeasurableInfo("c2", c2);
 32:         printMeasurableInfo("r1", r1);
 33:         printMeasurableInfo("r2", r2);
 34:         printMeasurableInfo("r3", r3);

 36:         // print out their roundnesses
 37:         // Note how in this modified version of the program, we need to say
 38:         // that the roundness method is found in the Measurable interface
 39:         System.out.println("The roundness of c1 is " 
 40:                 + Measurable.roundness(c1));
 41:         System.out.println("The roundness of c2 is " 
 42:                 + Measurable.roundness(c2));
 43:         System.out.println("The roundness of r1 is " 
 44:                 + Measurable.roundness(r1));
 45:         System.out.println("The roundness of r2 is " 
 46:                 + Measurable.roundness(r2));
 47:         System.out.println("The roundness of r3 is " 
 48:                 + Measurable.roundness(r3));

 50:         // Now add some Lists and sorting
 51:         List<Measurable> items = Arrays.asList(c1, c2, r1, r2, r3);
 52:         
 53:         System.out.println("Here is a List: ");
 54:         printList(items);
 55:         items.sort(Measurable.BY_AREA);
 56:         System.out.println("Here sorted by area: ");
 57:         printList(items, m -> m.getArea());
 58:         items.sort(Measurable.BY_PERIMETER);
 59:         System.out.println("Here sorted by perimeter: ");
 60:         printList(items, m -> m.getPerimeter());
 61:         items.sort((a, b) -> 
 62:                 Double.compare(Measurable.roundness(a), 
 63:                         Measurable.roundness(b)));
 64:         System.out.println("Here sorted by roundness: ");
 65:         printList(items, m -> Measurable.roundness(m));
 66:     }

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

 73:     // this method accepts Rectangles, but not Circles
 74:     public static void printRectangleInfo(String name, Rectangle r) {
 75:         System.out.println(name + " is a " + r.getLength() + "x" + r.getWidth()
 76:                            + " rectangle");
 77:     }

 79:     // this method accepts any object that implements the Measurable interface
 80:     // which includes both Circles and Rectangles
 81:     public static void printMeasurableInfo(String name, Measurable m) {
 82:         System.out.println("Measures of " + name + ":");
 83:         System.out.println("\tArea:       " + m.getArea());
 84:         System.out.println("\tPerimeter:  " + m.getPerimeter());
 85:     }

 87:     // This method prints a List of Measurable objects
 88:     private static void printList(List<Measurable> items) {
 89:         for (Measurable m : items) {
 90:             System.out.printf("%30s%n", m);
 91:         }
 92:         System.out.println();
 93:     }

 95:     // This method prints a List of Measurable objects along with their sizes
 96:     // accounding to the given function
 97:     private static void printList(
 98:             List<Measurable> items, 
 99:             Function<Measurable, Double> map) {
100:         for (Measurable m : items) {
101:             System.out.printf("%30s%10.4f%n", m, map.apply(m));
102:         }
103:         System.out.println();
104:     }
105: }