Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help with code dealing with parallel arrays.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with code dealing with parallel arrays.

    Hey I need some help with the code! specifically with the perimeter and area methods.

    I am not sure how to implement them or what code i need o fill in.
    For the perimeter I think I need to go from each point in the array to calculate the perimeter
    but i am not sure how to do that.
    I am thinking I need a loop so that it will add each point in the array.

    For the area there is a formula for the area of a polygon. Correct me if I am wrong.
    I am not sure what to do for the area method.

    Any help would be greatly appreciated.

    /**
     * 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() );
     
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with code dealing with parallel arrays.

    Is this an algorithm problem
    or a coding problem (you have the algorithm and need help coding it)?

    For algorithm help have you tried google?

    need to go from each point in the array
    Do you know how to index through an array? Or is there some other problem.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with code dealing with parallel arrays.

    I think I just need to go from point to point. x[1] to x[2] using a loop i think. I don't know how to code it.
    If there is an algorithm could you show that as well?

    Thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with code dealing with parallel arrays.

    You could use the index of a for loop to go from x[0] to x[1] etc
    Go to this site and Find 'for statement': Trail: Learning the Java Language: Table of Contents (The Java™ Tutorials)

    What is the problem you need an algorithm for?

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with code dealing with parallel arrays.

    I know how to do the for loop but there are both x and y values so I am a little
    confused on how to code it. Could you provide an example?

    by x and y i mean two separate arrays.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with code dealing with parallel arrays.

    Using parallel arrays means the data in all the arrays at a certain index is related.
    For your case of points on a 2Dim graph, one array has the x value and one array has the y value.
    For the nth point say at index n, the point's x value is xVal[n] and its y value is yVal[n].

Similar Threads

  1. parallel array newbie
    By NewAtJava in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 1st, 2011, 09:21 PM
  2. My program keeps lagging when dealing with large arrays.
    By chrynelson in forum Java Theory & Questions
    Replies: 4
    Last Post: October 21st, 2011, 04:57 PM
  3. Retrieving arrays of java class inside C++ code
    By sattu in forum Java Native Interface
    Replies: 2
    Last Post: May 20th, 2011, 02:15 AM
  4. Need help implenting arrays and method call into existing code
    By hoven in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 31st, 2011, 01:36 AM
  5. theory about serial / parallel port & java
    By wolfgar in forum Java Theory & Questions
    Replies: 5
    Last Post: January 4th, 2010, 10:08 PM