COP 3337 Section U05 Spring 2017 Homework #2 =========== Due: February 13, 2017 Complete the missing methods in the driver, Circle, Parallelogram, Square, Triangle. The comments tell you what to do. 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() { // you write it } // @return the area public double getArea() { // you write it } } II. The driver public class Driver { //testsome 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 4 figures"); Figure[] set1 = new Figure[4]; set1[0] = new Circle(10); set1[1] = new Triangle(10, 6, 8); try { set1[2] = new Triangle(5, 12, 7); } catch (IllegalArgumentException e) { System.out.println("We try to form an illegal triangle"); } set1[2] = new Parallelogram(10, 20, Math.PI / 3); set1[3] = new Square(6); 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) { // you write it } // print the shape, the fields, the perimeter and the area // of fig // if fig = null, write null public static void printFig(Figure fig) { // you write it } // 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) { // you write it } // 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) { // you write it } // 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) { // you write it } // 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) { // you write it } } III. The interface Figure public interface Figure { double getPerimeter(); double getArea(); } IV. The class Parallelogram 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() { // you write it } // @return the area public double getArea() { // you write it } } V. The class Square 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() { // you write it } // @return the area public double getArea() { // you write it } } VI. The class Triangle 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) { // you write it } // 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() { // you write it } // @return the area public double getArea() { // you write it } } VII. Here is the output of the driver: TESTING FIGURES =============== We form an array of 4 figures We try to form an illegal triangle The array is array[0] = a circle of radius 10.0 The perimeter is 62.83185307179586 and the area is 314.1592653589793 array[1] = a triangle with sides 10.0, 6.0, 8.0 The perimeter is 24.0 and the area is 24.0 array[2] = a parallelogram with sides 10.0 and 20.0 and angle 1.0471975511965976 The perimeter is 60.0 and the area is 173.20508075688772 array[3] = a square of side 6.0 The perimeter is 24.0 and the area is 36.0 The figure with a largest perimeter is a circle of radius 10.0 The perimeter is 62.83185307179586 and the area is 314.1592653589793 The figure with a smallest perimeter is a triangle with sides 10.0, 6.0, 8.0 The perimeter is 24.0 and the area is 24.0 The figure with a largest area is a circle of radius 10.0 The perimeter is 62.83185307179586 and the area is 314.1592653589793 The figure with a smallest area is a triangle with sides 10.0, 6.0, 8.0 The perimeter is 24.0 and the area is 24.0 BUILD SUCCESSFUL (total time: 3 seconds) VII. The rules for Hw 1 concerning lateness, style, how to hand in the homework, apply here as well.