Java Reference
In-Depth Information
23 System.out.print("number of steps to display? ");
24
int
steps = console.nextInt();
25 System.out.println();
26
27
double
xVelocity = velocity * Math.cos(angle);
28
double
yVelocity = velocity * Math.sin(angle);
29
double
totalTime = -2.0 * yVelocity / ACCELERATION;
30
double
timeIncrement = totalTime / steps;
31
double
xIncrement = xVelocity * timeIncrement;
32
33
double
x = 0.0;
34
double
y = 0.0;
35
double
t = 0.0;
36 System.out.println("step\tx\ty\ttime");
37 System.out.println("0\t0.0\t0.0\t0.0");
38
for
(
int
i = 1; i <= steps; i++) {
39 t += timeIncrement;
40 x += xIncrement;
41 y = yVelocity * t + 0.5 * ACCELERATION * t * t;
42 System.out.println(i + "\t" + round2(x) + "\t" +
43 round2(y) + "\t" + round2(t));
44 }
45 }
46
47
public static double
round2(
double
n) {
48
return
Math.round(n * 100.0) / 100.0;
49 }
50 }
The following is a sample execution of the program:
This program computes the
trajectory of a projectile given
its initial velocity and its
angle relative to the
horizontal.
velocity (meters/second)?
30
angle (degrees)?
50
number of steps to display?
10
step x
y
time
0
0.0
0.0
0.0
1
9.03
9.69
0.47
Search WWH ::
Custom Search