I have a question on how to use printf. In my case, I don't know how to use printf to make my headings of my table (two dimensional array) match the table's alignment. I want it so that the headings numbers will be in line with the numbers inside the table. How can I fix this? Any help will be greatly appreciated. Below is my output:
run:
Projectile Distance (Feet)
MPH 572.95780 630.25357 687.54935 744.84513 802.14091 2291.83118
____________________________________________________________________________________
45 42.3390045 44.8135645 38.8776945 25.6454145 7.6001045 44.20434
67 63.0380667 66.7224167 57.8845667 38.1831767 11.3157167 65.81535
77 72.4467377 76.6809877 66.5240577 43.8821577 13.0046277 75.63853
89 83.7371389 88.6312689 76.8914489 50.7209389 15.0313289 87.42636
90 84.6779990 89.6271290 77.7553890 51.2908390 15.2002190 88.40867
56 52.6885356 55.7679956 48.3811356 31.9142956 9.4579156 55.00984
99 93.1457999 98.5898399 85.5309299 56.4199199 16.7202399 97.24954
BUILD SUCCESSFUL (total time: 0 seconds)
Below is my code for the two files:
public class CatapultTester {
static int velocity[] = {45, 67, 77, 89, 90, 56, 99};
static double angle[] = {Math.toDegrees(10), Math.toDegrees(11), Math.toDegrees(12),
Math.toDegrees(13), Math.toDegrees(14), Math.toDegrees(40)};
public static void main(String args[]){
System.out.printf("%55s", "Projectile Distance (Feet)");
System.out.println("\n");
System.out.printf("%1s", "MPH");
for(int k = 0; k < angle.length; k++){
System.out.printf("%12.5f", angle[k]);
}
System.out.println();
System.out.println("___________________________________________________"
+ "_________________________________");
System.out.println();
Catapult.display(angle, velocity);
}
}
_
public class Catapult {
public static void display(double angle[], int velocity[]){
double[][] result = new double[velocity.length][angle.length];
for(int i = 0; i < velocity.length; i++){
for(int j = 0; j < angle.length; j++){
result[i][j] = velocity[i] * Math.sin(angle[j] / 9.8);
System.out.printf("%1d%12.5f", velocity[i], result[i][j]);
}
System.out.println();
}
}
}