Results 1 to 4 of 4
  1. #1
    felixsamma is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Polygon construct, area and perimeter with parallel arrays

    This is what I have done so far, and the test in main was provided to me. I have a basic idea what to do but I would not be able to do so with arrays. Help! :| Sadly I am dreadful at Java.

    Java Code:
    /**
     * Polygon1 models a polygon with parallel arrays.
     */
    public class Polygon1 {
        private double[] x;
        private double[] y;
        private int[] points;
    
        // construct the polygon
        public Polygon1( double[] x, double[] y, int[] points ) {
            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.
    
         * @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() );
    
        }
    }
    Last edited by Fubarable; 11-13-2011 at 11:11 PM. Reason: code tags added and question approved

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Polygon construct, area and perimeter with parallel arrays

    Does your code compile? If not, are you getting any errors, and if so, you'd better post them here?

  3. #3
    felixsamma is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: Polygon construct, area and perimeter with parallel arrays

    Polygon1.java:46: cannot find symbol
    symbol : constructor Polygon1(double[],double[])
    location: class Polygon1
    Polygon1 sq = new Polygon1( sq_x, sq_y );
    ^
    Polygon1.java:54: cannot find symbol
    symbol : constructor Polygon1(double[],double[])
    location: class Polygon1
    Polygon1 rect = new Polygon1( rect_x, rect_y );
    ^
    Polygon1.java:63: cannot find symbol
    symbol : constructor Polygon1(double[],double[])
    location: class Polygon1
    Polygon1 rect1 = new Polygon1( rect1_x, rect1_y );
    ^
    Polygon1.java:72: cannot find symbol
    symbol : constructor Polygon1(double[],double[])
    location: class Polygon1
    Polygon1 lshape = new Polygon1( lshape_x, lshape_y );
    ^
    Polygon1.java:80: cannot find symbol
    symbol : constructor Polygon1(double[],double[])
    location: class Polygon1
    Polygon1 rt = new Polygon1( rt_x, rt_y );
    ^
    Polygon1.java:91: cannot find symbol
    symbol : constructor Polygon1(double[],double[])
    location: class Polygon1
    Polygon1 hex = new Polygon1( hex_x, hex_y );


    This is what I am getting soo far, my problem is that I am skittish of starting the area and perimeter before actually having a constructed polygon :$

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Polygon construct, area and perimeter with parallel arrays

    A general rule of thumb to follow: always fix all compilation errors before adding any new code. Right now the error messages are telling you exactly what needs to be fixed.

    For instance, this:

    Java Code:
    Polygon1.java:46: cannot find symbol
    symbol : constructor Polygon1(double[],double[])
    location: class Polygon1
    Polygon1 sq = new Polygon1( sq_x, sq_y );
    ^
    Is probably referring to this line:
    Java Code:
    Polygon1 sq = new Polygon1( sq_x, sq_y );
    and if you check your line numbers you'll see if I'm right, if that line is "Polygon1.java:46" or line 46 of the Polygon1.java file. The error is stating that that line is trying to call a Polygon1 constructor that accepts two double arrays and only two double arrays. Do any of your Polygon1 constructors match this? Do any accept only two double arrays as parameters? If not you'd best create one.

    Here's your constructor:

    Java Code:
     public Polygon1( double[] x, double[] y, int[] points ) {
       // ... etc...
     }
    What does this constructor take for parameters? two arrays of double and an array of int. So a solution is to get rid of that array of int in the constructor parameter list, and then fix any other errors that might arise.
    Last edited by Fubarable; 11-13-2011 at 11:24 PM.

Similar Threads

  1. parallel arrays
    By belfast09 in forum New To Java
    Replies: 6
    Last Post: 06-13-2011, 11:48 AM
  2. parallel arrays
    By the beginner in forum New To Java
    Replies: 9
    Last Post: 02-22-2011, 05:35 AM
  3. Replies: 3
    Last Post: 09-26-2010, 04:21 AM
  4. two parallel arrays
    By Adomini in forum New To Java
    Replies: 12
    Last Post: 09-07-2010, 01:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •