logo CodeStepByStep logo

projectile2

Related Links:

Write a program that calculates the trajectory that a projectile will follow. (This is an enhanced version of the Projectile program from the Parameters category.) You will prompt the user for the projectile's initial velocity, its initial angle relative to the horizontal, and the number of time increments to display. Here is an example output log from your program:

This program computes the trajectory of a projectile given
its initial velocity and angle relative to the horizontal.

Velocity  (m/s)? 30
Angle (degrees)? 50
Number of steps? 10

step    x       y       vy      time
------------------------------------
0       0.00    0.00    22.98   0.00
1       9.03    9.69    18.39   0.47
2       18.07   17.23   13.79   0.94
3       27.10   22.61   9.19    1.41
4       36.14   25.84   4.60    1.87
5       45.17   26.92   -0.00   2.34
6       54.21   25.84   -4.60   2.81
7       63.24   22.61   -9.19   3.28
8       72.28   17.23   -13.79  3.75
9       81.31   9.69    -18.39  4.22
10      90.35   0.00    -22.98  4.69

You can compute the x and y components of velocity using the cos and sin of the initial angle, respectively. The projectile will be pulled downward by gravity with a force of 9.81 m/s2. Recall that you can compute the displacement of a body in motion using the following formula:

displacement = vt + ½at2

The following is a pseudocode description of the process:

x, y, t = 0.
for (the given number of steps):
    add time increment to t.
    add x increment to x.
    reset y to y-velocity * t + 0.5 * -9.81 * t * t.
    report step #, x, y, and t.

Your program should also display graphical output onto a DrawingPanel. The window size should be 420 x 220 and the background color should be cyan (Color.CYAN). Draw the projectile as a black circle, 10 pixels in diameter, at a scale of 400. For example, the log above should be accompanied by the following graphical display:

Java DrawingPanel close maximize minimize
expected output
 

You should break down your program into several functions, each of which helps solve the overall problem.

The output columns should align into 8-space-wide columns, left-aligned.

Complete program: Write an entire program that you could put into a file and run outside of CodeStepByStep.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.