Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package projectileProject;
- import javax.swing.JOptionPane;
- public class Projectile {
- public static void main(String[] args) {
- String task, task1, title;
- final int GRAVITATION = 32;
- final double distanceToTarget;
- double initialVelocity;
- double launchAngle; //changing variable
- double radian; //changing variable
- double flightTime;
- double highestPoint;
- double distanceTraveled;
- double error;
- double minError;
- String trajectory;
- task = "Enter distance to target in feet";
- title = "input";
- distanceToTarget = Double.parseDouble(JOptionPane.showInputDialog(null, task, title,
- JOptionPane.CANCEL_OPTION));
- task = "Enter initial velocity in feet/sec: ";
- initialVelocity = Double.parseDouble(JOptionPane.showInputDialog(null, task, title,
- JOptionPane.CANCEL_OPTION));
- task = "Enter the launch angle in degrees. \n" +
- "The initial choice must be a 45 degree angle: ";
- launchAngle = Double.parseDouble(JOptionPane.showInputDialog(null, task, title,
- JOptionPane.QUESTION_MESSAGE));
- radian = (launchAngle * Math.PI / 180);
- flightTime = (2 * initialVelocity * Math.sin(radian))/GRAVITATION;
- highestPoint = (initialVelocity * Math.sin(radian))*(flightTime/2) -
- 0.5 * GRAVITATION * Math.pow(flightTime/2, 2);
- distanceTraveled = (initialVelocity * Math.cos(radian) * flightTime);
- error = (distanceTraveled - distanceToTarget);
- minError = Math.abs(error);
- trajectory = String.format("initial velocity: %.2f feet/seconds\n" +
- "launch angle: %.2f degrees\n" +
- "flight time: %.2f seconds\n" +
- "maximum height: %.2f feet\n" +
- "distance traveled: %.2f feet\n" +
- "target missed by: %.2f feet\n",
- initialVelocity, launchAngle, flightTime,
- highestPoint, distanceTraveled, error);
- JOptionPane.showMessageDialog(null, trajectory);
- if (error < 0) {
- System.out.printf("Target missed by %f.2! Adjust launch angle up and try again", error);
- }
- else if (error > 1) {
- System.out.printf("Target missed by %f.2! Adjust launch angle down and try again", error);
- }
- double launchAngleTwo;
- double radianTwo;
- String trajectoryTwo;
- task = "Enter the launch angle in degrees. \n";
- launchAngleTwo = Double.parseDouble(JOptionPane.showInputDialog(null, task, title,
- JOptionPane.QUESTION_MESSAGE));
- radianTwo = launchAngleTwo * Math.PI / 180;
- flightTime = (2 * initialVelocity * Math.sin(radianTwo))/GRAVITATION;
- highestPoint = (initialVelocity * Math.sin(radianTwo))*(flightTime/2) -
- 0.5 * GRAVITATION * Math.pow(flightTime/2, 2);
- distanceTraveled = (initialVelocity * Math.cos(radianTwo) * flightTime);
- error = distanceTraveled - distanceToTarget;
- trajectoryTwo = String.format("initial velocity: %.2f feet/seconds\n" +
- "launch angle: %.2f degrees\n" +
- "flight time: %.2f seconds\n" +
- "maximum height: %.2f feet\n" +
- "distance traveled: %.2f feet\n" +
- "target missed by: %.2f feet\n",
- initialVelocity, launchAngleTwo, flightTime,
- highestPoint, distanceTraveled, Math.abs(error));
- JOptionPane.showMessageDialog(null, trajectoryTwo);
- System.out.println("\n");
- if (Math.abs(error) < 0) {
- System.out.printf("Target missed by %f.2! Adjust launch angle up and try again", Math.abs(error));
- }
- else if (Math.abs(error) > 0) {
- System.out.printf("Target missed by %f.2! Adjust launch angle down and try again", Math.abs(error));
- }
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement