Java Reference
In-Depth Information
know that the graph of the projectile will be symmetrical. The projectile will go
upward until its y -velocity reaches 0, and then it will follow a similar path back down
that takes an equal amount of time. Thus, the total time involved in seconds can be
computed as follows:
double totalTime = -2.0 * yVelocity / ACCELERATION;
Now, how do we compute the values of x , y , and elapsed time to include in our
table? It is relatively simple to compute two of these. We want steady time incre-
ments for each entry in the table, so we can compute the time increment by dividing
the total time by the number of steps we want to include in our table:
double timeIncrement = totalTime / steps;
As noted earlier, the x -velocity does not change, so for each of these time incre-
ments, we move the same distance in the x -direction:
double xIncrement = xVelocity * timeIncrement;
The tricky value to compute here is the y -position. Because of acceleration due to
gravity, the y -velocity changes over time. But from physics, we have the following
general formula for computing the displacement of an object given the velocity v ,
time t , and acceleration a :
1
2
at 2
displacement = vt +
In our case, the velocity we want is the y -velocity and the acceleration is from the
Earth's gravity constant. Here, then, is a pseudocode description of how to create the
table:
set all of x, y, and t to 0.
for (given number of steps) {
add timeIncrement to t.
add xIncrement to x.
reset y to yVelocity * t + 0.5 * ACCELERATION * t * t.
report step #, x, y, t.
}
We are fairly close to having real Java code here, but we have to think about how
to report the values of x , y , and t in a table. They will all be of type double , which
means they are likely to produce a large number of digits after the decimal point. But
we aren't interested in seeing all those digits, because they aren't particularly relevant
and because our computations aren't that accurate.
Before we try to complete the code for the table, let's think about the problem of
displaying only some of the digits of a number. The idea is to truncate the digits so
Search WWH ::




Custom Search