/** * Polygon1 models a polygon with parallel arrays. */ public class Polygon1 { private double[] x; private double[] y; // construct the polygon public Polygon1( double[] x, double[] y ) { this.x = new double[ x.length ]; this.y = new double[ y.length ]; // assumes x.length == y.length, could be a problem for( int i = 0 ; i < x.length; i++ ) { this.x[i] = x[i]; this.y[i] = y[i]; } } /** * calculates the distance between point at index i * and the point at index j. * * [ Why is this method private? ] * * @return distance between points */ private double distance( int i, int j ) { return Math.hypot( x[i] - x[j], y[i] - y[j] ); } public double perimeter() { } public double area() { } public static void main( String[] args ) { double sq_x[] = { 0.0, 1.0, 1.0, 0.0 }; double sq_y[] = { 0.0, 0.0, 1.0, 1.0 }; Polygon1 sq = new Polygon1( sq_x, sq_y ); System.out.println( "square perimeter: " + sq.perimeter() ); System.out.println( "square area: " + sq.area() ); System.out.println(); double rect_x[] = { 0.0, 2.0, 2.0, 0.0 }; double rect_y[] = { 0.0, 0.0, 1.0, 1.0 }; Polygon1 rect = new Polygon1( rect_x, rect_y ); System.out.println( "rectangle perimeter: " + rect.perimeter() ); System.out.println( "rectangle area: " + rect.area() ); System.out.println(); // same rectangle as about, but with extra points double rect1_x[] = { 0.0, 1.0, 2.0, 2.0, 1.0, 0.0 }; double rect1_y[] = { 0.0, 0.0, 0.0, 1.0, 1.0, 1.0 }; Polygon1 rect1 = new Polygon1( rect1_x, rect1_y ); System.out.println( "rectangle perimeter: " + rect1.perimeter() ); System.out.println( "rectangle area: " + rect1.area() ); System.out.println(); // lshape polygon double lshape_x[] = { 0.0, 2.0, 2.0, 1.0, 1.0, 0.0 }; double lshape_y[] = { 0.0, 0.0, 1.0, 1.0, 2.0, 2.0 }; Polygon1 lshape = new Polygon1( lshape_x, lshape_y ); System.out.println( "lshape perimeter: " + lshape.perimeter() ); System.out.println( "lshape area: " + lshape.area() ); System.out.println(); double rt_x[] = { 0.0, 1.0, 0.0 }; double rt_y[] = { 0.0, 0.0, 1.0 }; Polygon1 rt = new Polygon1( rt_x, rt_y ); System.out.println( "rt perimeter: " + rt.perimeter() ); System.out.println( "rt area: " + rt.area() ); System.out.println(); // regular hexagon final double a = 0.5; final double b = Math.sqrt( 1.0 - (0.5*0.5) ); double hex_x[] = { 1.0, a, -a, -1.0, -a, a}; double hex_y[] = { 0.0, b, b, 0.0, -b, -b}; Polygon1 hex = new Polygon1( hex_x, hex_y ); System.out.println( "hex perimeter: " + hex.perimeter() ); System.out.println( "hex area: " + hex.area() ); } }