Results 1 to 10 of 10
  1. #1
    mp3_1 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default 2D Array Print Trouble

    This is a program that calculates the trajectory of a projectile based on launch angles and launch velocities.
    I am having trouble moving the text at the bottom to the top.







    Present Outcome:



    Desired Outcome:



    Thank you all and I hope you are having a fantastic day :)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: 2D Array Print Trouble

    I am having trouble moving the text at the bottom to the top.
    Towards the end of the tester main() method you say:

    Java Code:
    for(int i = 0; i < speed.length; i)
    {
        System.out.printf("%5d", speed[i]);
        System.out.println();
    }
    
    cat.printDistance();
    The for loop prints the speeds on the left (each one followed by a newline) and only after that do you call printDistance() to print the body of the table. So the body of the table ends up after the row labels have been printed.

    The table must be printed row by row, because that's how the various print*() methods work. One way of doing this would be if printDistance() itself was given the responsibility of printing the speeds.

    -----

    When you post code use text rather than images. Put [code] at the start of the code and [/code] at the end to preserve formatting. Ie what you post is:

    [code]
    // your code here
    [/code]

    Another small point is that printf() allows you to include newlines as part of the format string:

    Java Code:
    //System.out.printf("%5d", speed[i]);
    //System.out.println();
    System.out.printf("%5d%n", speed[i]);

  3. #3
    mp3_1 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: 2D Array Print Trouble

    here are the code versions of the two classes if you need them.
    Java Code:
        private int[] mySpeed;
        private int[] myAngle;
        private double[][] myDistance;
    
        public Catapult(int[] speed, int[] angle) // constructor; initializes all private variables
        {
            myDistance = new double [speed.length][angle.length];
            mySpeed = speed;
            myAngle = angle; 
        }
    
        public void calcTrajectory() // mutator method to fill 2-D array
        {
            for (int row = 0; row < myDistance.length; row++)
            {
                for (int col = 0; col < myDistance[0].length; col++)
                {
                    myDistance[row][col] = ((int)Math.pow(mySpeed[row], 2) * Math.sin(2 * Math.toRadians(myAngle[col]))) / 9.8;
                }
            } // end outer loop
        }
    
        public void printDistance() // loops through 2-D array
        {        
            for (int row = 0; row < myDistance.length; row++)
            {        
                for (int col = 0; col < myDistance[0].length; col++)
                {
                    System.out.printf("%12.2f", myDistance[row][col]);
                }
                System.out.println();
            } // end outer loop 
        }
    Java Code:
        public static void main(String[] args)
        {
            int [] speed = {20, 25, 30, 35, 40, 45, 50};
            int [] angle = {25, 30, 35, 40, 45, 50};
            Catapult cat = new Catapult(speed, angle);
            cat.calcTrajectory();
            
            System.out.printf("%60s", "Projectile Distance (feet)");
            System.out.println();
            System.out.printf("%5s", "MPH");
                       
            for(int i = 0; i < angle.length; i++)
            {
                System.out.printf("%8d%s", angle[i], " deg ");
            }
            System.out.println();
            for(int i = 0; i < 82; i++)
            {
                System.out.print("=");
            }
            System.out.println();
            
            for(int i = 0; i < speed.length; i++)
            {
                System.out.printf("%5d", speed[i]);
                System.out.println();
            }
            
            cat.printDistance();
        }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: 2D Array Print Trouble

    Did you understand what I meant by suggesting that you try having printDistance() rather than main() print the speeds?

  5. #5
    mp3_1 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: 2D Array Print Trouble

    I am sorry I did not receive your suggestion, could you please explain it again :)

  6. #6
    mp3_1 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: 2D Array Print Trouble

    you mean like this?

    Java Code:
        private int[] mySpeed;
        private int[] myAngle;
        private double[][] myDistance;
    
        public Catapult(int[] speed, int[] angle) // constructor; initializes all private variables
        {
            myDistance = new double [speed.length][angle.length];
            mySpeed = speed;
            myAngle = angle; 
        }
    
        public void calcTrajectory() // mutator method to fill 2-D array
        {
            for (int row = 0; row < myDistance.length; row++)
            {
                for (int col = 0; col < myDistance[0].length; col++)
                {
                    myDistance[row][col] = ((int)Math.pow(mySpeed[row], 2) * Math.sin(2 * Math.toRadians(myAngle[col]))) / 9.8;
                }
            } // end outer loop
        }
    
        public void printDistance() // loops through 2-D array
        {        
            System.out.printf("%60s", "Projectile Distance (feet)");
            System.out.println();
            System.out.printf("%5s", "MPH");
                       
            for(int i = 0; i < myAngle.length; i++)
            {
                System.out.printf("%8d%s", myAngle[i], " deg ");
            }
            System.out.println();
            for(int i = 0; i < 82; i++)
            {
                System.out.print("=");
            }
            System.out.println();
            
            for(int i = 0; i < mySpeed.length; i++)
            {
                System.out.printf("%5d", mySpeed[i]);
                System.out.println();
            }
            
            for (int row = 0; row < myDistance.length; row++)
            {        
                for (int col = 0; col < myDistance[0].length; col++)
                {
                    System.out.printf("%12.2f", myDistance[row][col]);
                }
                System.out.println();
            } // end outer loop
        }

    Java Code:
        public static void main(String[] args)
        {
            int [] speed = {20, 25, 30, 35, 40, 45, 50};
            int [] angle = {25, 30, 35, 40, 45, 50};
            Catapult cat = new Catapult(speed, angle);
            cat.calcTrajectory();
    
            cat.printDistance();
        }
    Last edited by mp3_1; 12-04-2011 at 07:27 AM.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: 2D Array Print Trouble

    Close...

    Java Code:
        // not here...
    for(int i = 0; i < mySpeed.length; i++)
    {
        System.out.printf("%5d", mySpeed[i]);
        System.out.println();
    }
         
    for (int row = 0; row < myDistance.length; row++)
    {        
        // ... but here System.out.printf("%5d", mySpeed[row]);
        for (int col = 0; col < myDistance[0].length; col++)
        {
            System.out.printf("%12.2f", myDistance[row][col]);
        }
        System.out.println();
    } // end outer loop
    (untested, but it seems reasonable)

  8. #8
    mp3_1 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: 2D Array Print Trouble

    thank you i will take a look at it right now :)

  9. #9
    mp3_1 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: 2D Array Print Trouble

    Yes this does work, Thank you :)

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

Similar Threads

  1. Having some array trouble
    By beijct in forum New To Java
    Replies: 6
    Last Post: 11-10-2011, 04:44 AM
  2. Having trouble with 2d Array
    By geekchick in forum New To Java
    Replies: 2
    Last Post: 08-02-2010, 05:54 AM
  3. Print Formatting in Columns trouble
    By Inventor22 in forum New To Java
    Replies: 6
    Last Post: 07-09-2010, 11:23 AM
  4. Array trouble
    By hobo in forum New To Java
    Replies: 8
    Last Post: 11-17-2009, 01:04 PM
  5. Having trouble with array
    By ice22 in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 04:06 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
  •