public class BallTossExercise3 { public static void main (String[] argv) { for (double initVelocity=50; initVelocity<500; initVelocity+=50) { // Try this initial velocity: double heightReached = getMaxHeight (initVelocity); if (heightReached > 1000) { // If we've exceeded 1000, then stop. System.out.println ("initV=" + initVelocity + " height=" + heightReached); break; } } } static double getMaxHeight (double initVelocity) { // Make an instance of the simulator. BallTossSimulator2 sim = new BallTossSimulator2 (); // Find the height reached at t=1 and t=2 double stopTime = 1; double prevY = sim.run (stopTime, initVelocity); stopTime = 2; double nextY = sim.run (stopTime, initVelocity); while (nextY > prevY) { // As long as y(t+1) > y(t), repeat. prevY = sim.run (stopTime, initVelocity); stopTime = stopTime + 1; nextY = sim.run (stopTime, initVelocity); } // Now we're sure that y(t+1) < y(t) System.out.println ("initV=" + initVelocity + " stopTime=" + stopTime + " prevY=" + prevY + " nextY=" + nextY); return prevY; } }