// Helene Martin, CSE 142 // Simulates two balls falling. import java.awt.*; import java.util.*; public class BallSimulation { public static final double ACC_GRAVITY = 9.81; public static void main(String[] args) { Scanner console = new Scanner(System.in); int ball1x = 20; int ball1y = 20; System.out.print("Ball 1 v0? "); double ball1V0 = console.nextDouble(); int ball2x = 100; int ball2y = 10; System.out.print("Ball 2 v0? "); double ball2V0 = console.nextDouble(); DrawingPanel panel = new DrawingPanel(400, 400); Graphics g = panel.getGraphics(); for (double t = 0; t < 10; t += 0.1) { panel.clear(); g.setColor(Color.RED); double disp1 = displacement(ball1V0, ACC_GRAVITY, t); g.fillOval(ball1x, ball1y + (int) disp1, 40, 40); g.setColor(Color.BLUE); double disp2 = displacement(ball2V0, ACC_GRAVITY, t); g.fillOval(ball2x, ball2y + (int) disp2, 40, 40); panel.sleep(100); } //System.out.println(displacement(3.0, 4.0, 5.0)); } // given an object's initial velocity, acceleration and the current time, // calculate and return the displacement. public static double displacement(double v0, double a, double t) { // v0t + 1/2(at^2) return v0 * t + a * Math.pow(t, 2) / 2.0; } }