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