Java Reference
In-Depth Information
6 // constant for Earth acceleration in meters/second^2
7
public static final double
ACCELERATION =
9.81;
8
9
public static void
main(String[] args) {
10 Scanner console =
new
Scanner(System.in);
11 giveIntro();
12
13 System.out.print("velocity (meters/second)? ");
14
double
velocity = console.nextDouble();
15 System.out.print("angle (degrees)? ");
16
double
angle = Math.toRadians(console.nextDouble());
17 System.out.print("number of steps to display? ");
18
int
steps = console.nextInt();
19 System.out.println();
20
21 printTable(velocity, angle, steps);
22 }
23
24 // prints a table showing the trajectory of an object given
25 // its initial velocity and angle and including the given
26 // number of steps in the table
27
public static void
printTable(
double
velocity,
28
double
angle,
int
steps) {
29
double
xVelocity = velocity * Math.cos(angle);
30
double
yVelocity = velocity * Math.sin(angle);
31
double
totalTime =
2.0 * yVelocity / ACCELERATION;
32
double
timeIncrement = totalTime / steps;
33
double
xIncrement = xVelocity * timeIncrement;
34
35
double
x = 0.0;
36
double
y = 0.0;
37
double
t = 0.0;
38 System.out.println("step\tx\ty\ttime");
39 System.out.println("0\t0.0\t0.0\t0.0");
40
for
(
int
i = 1; i <= steps; i++) {
41 t += timeIncrement;
42 x += xIncrement;
43 y = displacement(yVelocity, t, ACCELERATION);
44 System.out.println(i + "\t" + round2(x) + "\t" +
45
round2(y) + "\t" + round2(t));
46 }
47 }
48
49 // gives a brief introduction to the user
Search WWH ::
Custom Search