Source of MoreMeasures.java


  1: import java.util.ArrayList;
  2: import java.util.Arrays;
  3: import java.util.List;

  5: /**
  6:  * A program to test out the extensions to the Measurable interface. 
  7:  *
  8:  * @see MeasuringStuff
  9:  *
 10:  * @author Mark Young (A00000000)
 11:  */
 12: public class MoreMeasures {

 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:         RegularPolygon rp = new RegularPolygon(6, 9.0);

 22:         // make a list of the shapes
 23:         List<Measurable> shapes = new ArrayList<>();
 24:         shapes.addAll(Arrays.asList(c1, c2, r1, r2, rp));

 26:         // announce your intentions
 27:         System.out.println();
 28:         System.out.println("Sorting a list of shapes in various ways!");
 29:         System.out.println();

 31:         // sort by area
 32:         shapes.sort(Measurable.BY_AREA);
 33:         System.out.println("Sorted by area:");
 34:         shapes.forEach(s -> 
 35:                 System.out.printf("%30s: %8.1f%n", 
 36:                         s.toString(), s.getArea()));
 37:         System.out.println();

 39:         // sort by perimeter
 40:         shapes.sort(Measurable.BY_PERIMETER);
 41:         System.out.println("Sorted by perimeter:");
 42:         shapes.forEach(s -> 
 43:                 System.out.printf("%30s: %8.1f%n", 
 44:                         s.toString(), s.getPerimeter()));
 45:         System.out.println();

 47:         // sort by roundness
 48:         shapes.sort((one, other) 
 49:                 -> Double.compare(one.getRoundness(), other.getRoundness()));
 50:         System.out.println("Sorted by roundness:");
 51:         shapes.forEach(s -> 
 52:                 System.out.printf("%30s: %8.3f%n", 
 53:                         s.toString(), s.getRoundness()));
 54:         System.out.println();
 55:     }

 57: }