public class MeasuringStuff
1: // MeasuringStuff.java
2: // week09 code
3: // an example of using a simple interface
5: public class MeasuringStuff {
7: public static void main(String[] args) {
8: // create the shapes
9: Circle c1 = new Circle(10.0);
10: Circle c2 = new Circle(24.7);
11: Rectangle r1 = new Rectangle(10.0, 20.0);
12: Rectangle r2 = new Rectangle(0.4, 1000.0);
14: // print out their basic information
15: printCircleInfo("c1", c1);
16: printCircleInfo("c2", c2);
17: printRectangleInfo("r1", r1);
18: printRectangleInfo("r2", r2);
20: // print out their measurable information
21: printMeasurableInfo("c1", c1);
22: printMeasurableInfo("c2", c2);
23: printMeasurableInfo("r1", r1);
24: printMeasurableInfo("r2", r2);
26: // print out their roundnesses
27: System.out.println("The roundness of c1 is " + roundness(c1));
28: System.out.println("The roundness of c2 is " + roundness(c2));
29: System.out.println("The roundness of r1 is " + roundness(r1));
30: System.out.println("The roundness of r2 is " + roundness(r2));
31: }
33: // this method accepts Circles, but not Rectangles
34: public static void printCircleInfo(String name, Circle c) {
35: System.out.println(name + " is a circle of radius " + c.getRadius());
36: }
38: // this method accepts Rectangles, but not Circles
39: public static void printRectangleInfo(String name, Rectangle r) {
40: System.out.println(name + " is a " + r.getLength() + "x" + r.getWidth()
41: + " rectangle");
42: }
44: // this method accepts any object that implements the Measurable interface
45: // which includes both Circles and Rectangles
46: public static void printMeasurableInfo(String name, Measurable m) {
47: System.out.println("Measures of " + name + ":");
48: System.out.println("\tArea: " + m.getArea());
49: System.out.println("\tPerimeter: " + m.getPerimeter());
50: }
52: // this method likewise accepts any Measurable object
53: public static double roundness(Measurable m) {
54: return 4 * Math.PI * m.getArea() / Math.pow(m.getPerimeter(), 2);
55: }
57: }