Re: 2D Array Print Trouble
Quote:
I am having trouble moving the text at the bottom to the top.
Towards the end of the tester main() method you say:
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:
Code:
//System.out.printf("%5d", speed[i]);
//System.out.println();
System.out.printf("%5d%n", speed[i]);
Re: 2D Array Print Trouble
here are the code versions of the two classes if you need them.
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
}
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();
}
Re: 2D Array Print Trouble
Did you understand what I meant by suggesting that you try having printDistance() rather than main() print the speeds?
Re: 2D Array Print Trouble
I am sorry I did not receive your suggestion, could you please explain it again :)
Re: 2D Array Print Trouble
you mean like this?
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
}
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();
}
Re: 2D Array Print Trouble
Close...
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)
Re: 2D Array Print Trouble
thank you i will take a look at it right now :)
Re: 2D Array Print Trouble
Yes this does work, Thank you :)
Re: 2D Array Print Trouble