// CSE 142, Spring 2010, Marty Stepp // This program simulates the dropping of two balls from various heights. // It uses the displacement method we wrote in the MathFun class. import java.awt.*; public class Balls { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(600, 600); Graphics g = panel.getGraphics(); // initial positions and velocities of each ball int ball1x = 100; int ball1y = 0; int v01 = 25; int ball2x = 200; int ball2y = 100; int v02 = 15; // acceleration due to gravity = 9.81 m/s^2 double a = 9.81; // draw the balls at each time increment for (int t = 1; t <= 10; t++) { double d1 = displacement(v01, a, t); g.fillOval(ball1x, ball1y + (int) d1, 20, 20); double d2 = displacement(v02, a, t); g.fillOval(ball2x, ball2y + (int) d2, 20, 20); panel.sleep(200); } } // Computes and returns the change in position (displacement) of a moving // body with the given initial velocity v0 in m/s, the given // acceleration a in m/s^2, over the given interval of time t in s. public static double displacement(double v0, double a, double t) { // v0t + (1/2)at^2 double answer = (v0 * t) + 0.5 * a * Math.pow(t, 2); return answer; } }