// This program computes the trajectory of a projectile. It's modified // to prompt the user for the parameters, and further modified to do this // a given number of times. import java.awt.*; import java.util.*; public class Projectile3 { // constant for Earth's gravity acceleration in meters/second^2 public static final double ACCELERATION = -9.81; public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("Projectile calculator here!"); System.out.print("Please enter the total number of projectiles: "); int count = console.nextInt(); DrawingPanel panel = new DrawingPanel(420, 220); Graphics g = panel.getGraphics(); for (int i=1; i<=count; i++) { oneProjectile(g, console); } System.out.println("Projectile calculator is finished ... please close the drawing panel to exit"); } public static void oneProjectile(Graphics g, Scanner console) { System.out.print("Please enter the initial velocity: "); double v0 = console.nextDouble(); System.out.print("Please enter the initial angle in degrees: "); double angle = console.nextDouble(); System.out.print("Please enter the number of steps: "); int steps = console.nextInt(); table(g, v0, angle, steps); } // prints a table showing the trajectory of an object given // its initial velocity v and angle and number of steps public static void table(Graphics g, double v0, double angle, int steps) { double radians = Math.toRadians(angle); double v0x = v0 * Math.cos(radians); double v0y = v0 * Math.sin(radians); double totalTime = -2.0 * v0y / ACCELERATION; double dt = totalTime / steps; System.out.println(" step x y time"); for (int i = 0; i <= steps; i++) { double time = i * dt; double x = displacement(v0x, time, 0.0); double y = displacement(v0y, time, ACCELERATION); System.out.printf("%8d%8.2f%8.2f%8.2f\n", i, x, y, time); g.fillOval((int) x, (int) (220 - y), 5, 5); } } // returns the horizontal displacement for a body public static double displacement(double v, double t, double a) { return v * t + 0.5 * a * t * t; } }