Reputation: 89
I have to print a table of values which consists of 7 rows and 6 columns. Please run the programs on your computer so that you can see what it outputs. How would you fix the code so all the values will fit neatly into the table?
public class Catapult {
Catapult() {
}
public double[][] calcTrajectory(int ROWS, int COLS, int[] MPH, int[] degrees, double[][] trajectories) {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
trajectories[row][col] = Math.pow(MPH[row], 2) * Math.sin(2 * degrees[col]) / 9.8;
}
}
return trajectories;
}
public static void printScores(double[][] trajectories, int ROWS, int COLS, int[] MPH) {
System.out.println(" Projectile Distance (feet)");
System.out.println(" MPH 25 deg 30 deg 35 deg 35 deg 40 deg 45 deg");
System.out.println("=====================================================================");
for (int row = 0; row < ROWS; row++) {
System.out.printf("%5d", MPH[row]);
for (int col = 0; col < COLS; col++) {
System.out.printf("%13.2f", trajectories[row][col]);
}
System.out.println();
}
}
}
public class CatapultTester
{
static final int ROWS = 7;
static final int COLS = 6;
public static void main(String[] args)
{
Catapult object = new Catapult();
int degrees[] = { 25, 30, 35, 40, 45, 50 };
int MPH [] = { 20, 25, 30, 35, 40, 45, 50};
double[][] trajectories = new double[ROWS][COLS];
trajectories = object.calcTrajectory(ROWS, COLS, MPH, degrees, trajectories);
object.printScores(trajectories, ROWS, COLS, MPH);
}
}
this is the output:
Projectile Distance (feet)
MPH 25 deg 30 deg 35 deg 35 deg 40 deg 45 deg
=============================================================================
20
25
30
35
40
45
50
-10.71 -12.44 31.59 -40.57 36.49 -20.67
-16.73 -19.44 49.36 -63.39 57.02 -32.29
-24.10 -27.99 71.07 -91.28 82.10 -46.50
-32.80 -38.10 96.74 -124.24 111.75 -63.30
-42.84 -49.76 126.35 -162.27 145.96 -82.67
-54.22 -62.98 159.91 -205.37 184.73 -104.63
-66.93 -77.76 197.42 -253.54 228.06 -129.17
Upvotes: 2
Views: 1595
Reputation: 1947
public static void printScores(double[][] trajectories, int ROWS, int COLS, int[] MPH){
System.out.println(" Projectile Distance (feet)");
System.out.println(" MPH 25 deg 30 deg 35 deg 35 deg 40 deg 45 deg");
System.out.println("==============================================================================");
for(int row = 0; row < ROWS; row++){
System.out.printf("%5d", MPH[row]);
for(int col = 0; col < COLS; col++){
System.out.printf("%13.2f", trajectories[row][col]);
}
System.out.println();
}
}
Out put
MPH 25 deg 30 deg 35 deg 35 deg 40 deg 45 deg
==============================================================================
20 -10.71 -12.44 31.59 -40.57 36.49 -20.67
25 -16.73 -19.44 49.36 -63.39 57.02 -32.29
30 -24.10 -27.99 71.07 -91.28 82.10 -46.50
35 -32.80 -38.10 96.74 -124.24 111.75 -63.30
40 -42.84 -49.76 126.35 -162.27 145.96 -82.67
45 -54.22 -62.98 159.91 -205.37 184.73 -104.63
50 -66.93 -77.76 197.42 -253.54 228.06 -129.17
Upvotes: 1
Reputation: 4805
change this :
for (int row = 0; row < ROWS; row++) {
System.out.printf("%5d\n", MPH[row]);
}
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
System.out.printf("%13.2f", trajectories[row][col]);
}
System.out.println();
}
to this :
for (int row = 0; row < ROWS; row++) {
System.out.printf("%5d", MPH[row]);
for (int col = 0; col < COLS; col++) {
System.out.printf("%13.2f", trajectories[row][col]);
}
System.out.println();
}
here is the output:
Projectile Distance (feet)
MPH 25 deg 30 deg 35 deg 35 deg 40 deg 45 deg
=====================================================================
20 -10.71 -12.44 31.59 -40.57 36.49 -20.67
25 -16.73 -19.44 49.36 -63.39 57.02 -32.29
30 -24.10 -27.99 71.07 -91.28 82.10 -46.50
35 -32.80 -38.10 96.74 -124.24 111.75 -63.30
40 -42.84 -49.76 126.35 -162.27 145.96 -82.67
45 -54.22 -62.98 159.91 -205.37 184.73 -104.63
50 -66.93 -77.76 197.42 -253.54 228.06 -129.17
your problem is from your \n
at System.out.printf("%5d\n", MPH[row]);
Upvotes: 2