COP3337 Section U05 Spring 2017 A Program for Hw #2 =================== I. The Circle Class: -------------------- public class Circle implements Figure { // the radius private double radius; // construct a circle with a given radius public Circle(double r) { radius = r; } // @return the radius public double getRadius() { return radius; } // @return the perimeter public double getPerimeter() { // Here I use the Math.PI constant return 2 * Math.PI * radius; } // @return the area public double getArea() { return Math.PI * radius * radius; } } II. The Driver: --------------- public class Driver { //test some figures public static void main(String[] args) { System.out.println("TESTING FIGURES"); System.out.println("===============\n\n\n"); // form an array of figures System.out.println("We form an array of 6 figures"); Figure[] set1 = new Figure[6]; set1[0] = new Circle(1); set1[1] = new Triangle(3, 4, 5); try { set1[2] = new Triangle(3, 6, 2); } catch (IllegalArgumentException e) { System.out.println("We try to form an illegal triangle"); } set1[2] = new Parallelogram(1,2,Math.PI / 3); set1[3] = new Square(1); set1[4] = new Triangle(5, 12, 13); set1[5] = new Circle(5); System.out.println("The array is "); printArray(set1); // find the figures with the largest area, largest perimeter, // smallest area, smallest perimeter Figure smallArea = getSmallestArea(set1); Figure bigArea = getLargestArea(set1); Figure smallPerimeter = getSmallestPerimeter(set1); Figure bigPerimeter = getLargestPerimeter(set1); // print these figures System.out.print("\nThe figure with a largest perimeter is "); printFig(bigPerimeter); System.out.print("\nThe figure with a smallest perimeter is "); printFig(smallPerimeter); System.out.print("\nThe figure with a largest area is "); printFig(bigArea); System.out.print("\nThe figure with a smallest area is "); printFig(smallArea); } // print an array of figures // if the array is null or empty print the message "The array is empty" // otherwise print 2 lines // that displays the shape, the fields, the perimeter and the area // of each item in the array public static void printArray(Figure[] figs) { // check for null or empty array if (figs == null || figs.length == 0) { System.out.println("The array is empty.\n"); return; } // print the array for (int i = 0; i < figs.length; i++) { System.out.print("array [ " + i + " ] is "); printFig(figs[i]); } } // print the shape, the fields, the perimeter and the area // of fig // if fig = null, write null public static void printFig(Figure fig) { if (fig == null) { System.out.println(" null."); return; } if (fig instanceof Circle) { Circle c = (Circle) fig; // cast to circle System.out.println(" a circle of radius " + c.getRadius() + " ."); } else if (fig instanceof Square) { Square sq = (Square) fig; // cast to square System.out.println(" a square of side " + sq.getSide() + " ."); } else if (fig instanceof Triangle) { Triangle t = (Triangle) fig; // cast to triangle System.out.println(" a triangle with sides " + t.getSide1() + ", " + t.getSide2()+ ", and " + t.getSide3() + " ."); } else if (fig instanceof Parallelogram) { Parallelogram p = (Parallelogram) fig; System.out.println(" a parallelogram with sides " + p.getSide1() + " and " + p.getSide2() + " and angle " + p.getAngle() + " ."); } else // unknown figure { System.out.println(" unknown figure"); return; } // print the perimeter and the area of fig System.out.println("Its perimeter is " + fig.getPerimeter() + " and its area is " + fig.getArea() + " .\n"); } // return a reference to a figure with the largest perimeter // among all figures of arr // if arr is null or empty return null public static Figure getLargestPerimeter(Figure[] arr) { if (arr == null || arr.length == 0) return null; Figure bigPerimeter = null; for (Figure fig : arr) if (fig != null && (bigPerimeter == null || bigPerimeter.getPerimeter()< fig.getPerimeter() )) // update bigPerimeter bigPerimeter = fig; return bigPerimeter; } // return a reference to a figure with the smallest perimeter // among all figures of arr // if arr is null or empty return null public static Figure getSmallestPerimeter(Figure[] arr) { if (arr == null || arr.length == 0) return null; Figure smallPerimeter = null; for (Figure fig : arr) if (fig != null && (smallPerimeter == null || smallPerimeter.getPerimeter() > fig.getPerimeter() )) // update bigPerimeter smallPerimeter = fig; return smallPerimeter; } // return a reference to a figure with the largest area // among all figures of arr // if arr is null or empty return null public static Figure getLargestArea(Figure[] arr) { if (arr == null || arr.length == 0) return null; Figure bigArea = null; for (Figure fig : arr) if (fig != null && (bigArea == null || bigArea.getArea()< fig.getArea() )) // update bigPerimeter bigArea = fig; return bigArea; } // return a reference to a figure with the smallest are // among all figures of arr // if arr is null or empty return null public static Figure getSmallestArea(Figure[] arr) { if (arr == null || arr.length == 0) return null; Figure smallArea = null; for (Figure fig : arr) if (fig != null && ( smallArea == null || smallArea.getArea() > fig.getArea() )) // update bigPerimeter smallArea = fig; return smallArea; } } // end class III. The Interface Figure: -------------------------- public interface Figure { double getPerimeter(); // the perimeter double getArea(); // the area } IV. The Parallelogram Class: ---------------------------- public class Parallelogram implements Figure { private double a, b; // the 2 sides private double angle; // the angle between and b in radians // form a parallelogram with 2 sides and the angle between them // @param s1 and s2 are the 2 sides // @param ang is the angle public Parallelogram(double s1, double s2, double ang ) { a = s1; b = s2; angle = ang; } // methods that retrieve the fields public double getSide1() { return a; } public double getSide2() { return b; } public double getAngle() { return angle; } // @return the perimeter public double getPerimeter() { return 2.0 * (a + b); } // @return the area public double getArea() { return a * b * Math.sin(angle); } } V. The Square Class: -------------------- public class Square implements Figure { //the field private double side; // the side // the constructor // build a square with side s public Square (double s) { side = s; } // @return the side public double getSide() { return side; } // @return the area public double getPerimeter() { return 4 * side; } // @return the area public double getArea() { return side * side; } } VI. The Triangle Class: ----------------------- public class Triangle implements Figure { // the fields private double a, b, c; // the 3 fields // the constructor // form a triangle with sides s1,s2,s3 // if s1,s2,s3 do not form a triangle, throw an // IllegalArgumentException public Triangle(double s1, double s2, double s3) { if ( s1 + s2 <= s3 || s2 + s3 <= s1 || s3 + s1 <= s2) throw new IllegalArgumentException("The numbers do not form " + "a triangle"); // set the sides a = s1; b = s2; c = s3; } // methods that return the 3 sides public double getSide1() { return a; } public double getSide2() { return b; } public double getSide3() { return c; } // @ return the perimeter public double getPerimeter() { return a + b + c; } // @return the area public double getArea() { double p = (a + b+ c) / 2.0; return Math.sqrt( p * (p-a) * (p-b) * (p-c)); } } // end class VII. The output: ---------------- run: TESTING FIGURES =============== We form an array of 6 figures We try to form an illegal triangle The array is array [ 0 ] is a circle of radius 1.0 . Its perimeter is 6.283185307179586 and its area is 3.141592653589793 . array [ 1 ] is a triangle with sides 3.0, 4.0, and 5.0 . Its perimeter is 12.0 and its area is 6.0 . array [ 2 ] is a parallelogram with sides 1.0 and 2.0 and angle 1.0471975511965976 . Its perimeter is 6.0 and its area is 1.7320508075688772 . array [ 3 ] is a square of side 1.0 . Its perimeter is 4.0 and its area is 1.0 . array [ 4 ] is a triangle with sides 5.0, 12.0, and 13.0 . Its perimeter is 30.0 and its area is 30.0 . array [ 5 ] is a circle of radius 5.0 . Its perimeter is 31.41592653589793 and its area is 78.53981633974483 . The figure with a largest perimeter is a circle of radius 5.0 . Its perimeter is 31.41592653589793 and its area is 78.53981633974483 . The figure with a smallest perimeter is a square of side 1.0 . Its perimeter is 4.0 and its area is 1.0 . The figure with a largest area is a circle of radius 5.0 . Its perimeter is 31.41592653589793 and its area is 78.53981633974483 . The figure with a smallest area is a square of side 1.0 . Its perimeter is 4.0 and its area is 1.0 . BUILD SUCCESSFUL (total time: 0 seconds)