Projectile flight. Suppose a cannonball is propelled vertically into the air with a starting velocity v0. Any calculus book will tell us that the position of the ball after t seconds is s(t) = −0.5 · g · t2 + v0 · t, where g = 9.81 m/sec2 is the gravitational force of the earth. No calculus book ever mentions why someone would want to carry out such an obviously dangerous experiment, so we will do it in the safety of the computer.

In fact, we will confirm the theorem from calculus by a simulation. In our simulation, we will consider how the ball moves in very short time intervals Δt. In a short time interval the velocity v is nearly constant, and we can compute the distance the ball moves as Δs = v · Δt. In our program, we will simply set

double deltaT = 0.01;

and update the position by

s = s + v * deltaT;

The velocity changes constantly—in fact, it is reduced by the gravitational force of the earth. In a short time interval, v decreases by g · Δt, and we must keep the velocity updated as

v = v − g * deltaT;

In the next iteration the new velocity is used to update the distance.

Now run the simulation until the cannonball falls back to the earth. Get the initial velocity as an input (100 m/sec is a good value). Update the position and velocity 100 times per second, but only print out the position every full second. Also print out the values from the exact formula s(t) = −0.5 · g · t2 + v0 · t for comparison. Use a class Cannonball.

What is the benefit of this kind of simulation when an exact formula is available? Well, the formula from the calculus book is not exact. Actually, the gravitational force diminishes the farther the cannonball is away from the surface of the earth. This complicates the algebra sufficiently that it is not possible to give an exact formula for the actual motion, but the computer simulation can simply be extended to apply a variable gravitational force. For cannonballs, the calculus-book formula is actually good enough, but computers are necessary to compute accurate trajectories for higher-flying objects such as ballistic missiles.

Complete the following files:

/** This class simulates a cannonball fired up in the air. */ public class Cannonball { // private implementation . . . /** Creates a Cannonball object at position 0. @param ivel the inital velocity */ public Cannonball(double ivel) { . . . } /** Updates the position and velocity of this cannon ball after a given time interval. @param deltaT the time interval */ public void move(double deltaT) { . . . } /** Gets the velocity of this cannon ball. @return the velocity */ public double getVelocity() { . . . } /** Gets the position of this cannon ball. @return the (vertical) position */ public double getPosition() { . . . } }

Use the following file:

public class CannonballTester
   public static void main(String[] args)
      Cannonball ball = new Cannonball(100); // 100 m/sec
      ball.move(1); // move by one second

      System.out.println("Expected: 100");      
      System.out.println("Expected: " + (100 - 9.81));