Jan 02

DropBall.java Uses a while loop to determine how long it takes a ball to fall from the top of the Washington Monument to the ground

DropBall.java Uses a while loop to determine how long it takes a ball to fall from the top of the Washington Monument to the ground
************************************************************
/** Simulating dropping a ball from the top of the Washington
 *  Monument. The program outputs the height of the ball each
 *  second until the ball hits the ground.
 *
 *****************************************

public class DropBall {
  public static void main(String[] args) {
    int time = 0;
    double start = 550.0, drop = 0.0;
    double height = start;
    while (height > 0) {
      System.out.println("After " + time + 
                   (time==1 ? " second, " : " seconds,") + 
                   "the ball is at " + height + " feet.");
      time++;                   
      drop = freeFall(time);
      height = start - drop;
    }
    System.out.println("Before " + time + " seconds could " +
                       "expire, the ball hit the ground!");
  }
  
  /** Calculate the distance in feet for an object in 
   *  free fall. 
   */

  public static double freeFall (float time) {
    // Gravitational constant is 32 feet per second squared
    return(16.0 * time * time); // 1/2 gt^2
  }
}
@@@@@@@@@@@@@@@@@@@@

Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017

Skip to toolbar