Advertisement
Guest User

Untitled

a guest
Sep 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. package projectileProject;
  2. import javax.swing.JOptionPane;
  3.  
  4. public class Projectile {
  5.  
  6.  
  7.  
  8. public static void main(String[] args) {
  9. String task, task1, title;
  10. final int GRAVITATION = 32;
  11. final double distanceToTarget;
  12. double initialVelocity;
  13. double launchAngle; //changing variable
  14. double radian; //changing variable
  15. double flightTime;
  16. double highestPoint;
  17. double distanceTraveled;
  18. double error;
  19. double minError;
  20.  
  21.  
  22.  
  23. String trajectory;
  24. task = "Enter distance to target in feet";
  25. title = "input";
  26.  
  27. distanceToTarget = Double.parseDouble(JOptionPane.showInputDialog(null, task, title,
  28. JOptionPane.CANCEL_OPTION));
  29.  
  30. task = "Enter initial velocity in feet/sec: ";
  31.  
  32. initialVelocity = Double.parseDouble(JOptionPane.showInputDialog(null, task, title,
  33. JOptionPane.CANCEL_OPTION));
  34.  
  35.  
  36. task = "Enter the launch angle in degrees. \n" +
  37. "The initial choice must be a 45 degree angle: ";
  38. launchAngle = Double.parseDouble(JOptionPane.showInputDialog(null, task, title,
  39. JOptionPane.QUESTION_MESSAGE));
  40.  
  41.  
  42. radian = (launchAngle * Math.PI / 180);
  43. flightTime = (2 * initialVelocity * Math.sin(radian))/GRAVITATION;
  44. highestPoint = (initialVelocity * Math.sin(radian))*(flightTime/2) -
  45. 0.5 * GRAVITATION * Math.pow(flightTime/2, 2);
  46. distanceTraveled = (initialVelocity * Math.cos(radian) * flightTime);
  47. error = (distanceTraveled - distanceToTarget);
  48. minError = Math.abs(error);
  49.  
  50. trajectory = String.format("initial velocity: %.2f feet/seconds\n" +
  51. "launch angle: %.2f degrees\n" +
  52. "flight time: %.2f seconds\n" +
  53. "maximum height: %.2f feet\n" +
  54. "distance traveled: %.2f feet\n" +
  55. "target missed by: %.2f feet\n",
  56. initialVelocity, launchAngle, flightTime,
  57. highestPoint, distanceTraveled, error);
  58.  
  59. JOptionPane.showMessageDialog(null, trajectory);
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. if (error < 0) {
  69. System.out.printf("Target missed by %f.2! Adjust launch angle up and try again", error);
  70. }
  71. else if (error > 1) {
  72. System.out.printf("Target missed by %f.2! Adjust launch angle down and try again", error);
  73.  
  74. }
  75.  
  76. double launchAngleTwo;
  77. double radianTwo;
  78. String trajectoryTwo;
  79.  
  80.  
  81.  
  82. task = "Enter the launch angle in degrees. \n";
  83.  
  84. launchAngleTwo = Double.parseDouble(JOptionPane.showInputDialog(null, task, title,
  85. JOptionPane.QUESTION_MESSAGE));
  86.  
  87.  
  88. radianTwo = launchAngleTwo * Math.PI / 180;
  89. flightTime = (2 * initialVelocity * Math.sin(radianTwo))/GRAVITATION;
  90. highestPoint = (initialVelocity * Math.sin(radianTwo))*(flightTime/2) -
  91. 0.5 * GRAVITATION * Math.pow(flightTime/2, 2);
  92. distanceTraveled = (initialVelocity * Math.cos(radianTwo) * flightTime);
  93. error = distanceTraveled - distanceToTarget;
  94.  
  95. trajectoryTwo = String.format("initial velocity: %.2f feet/seconds\n" +
  96. "launch angle: %.2f degrees\n" +
  97. "flight time: %.2f seconds\n" +
  98. "maximum height: %.2f feet\n" +
  99. "distance traveled: %.2f feet\n" +
  100. "target missed by: %.2f feet\n",
  101. initialVelocity, launchAngleTwo, flightTime,
  102. highestPoint, distanceTraveled, Math.abs(error));
  103.  
  104. JOptionPane.showMessageDialog(null, trajectoryTwo);
  105.  
  106. System.out.println("\n");
  107.  
  108. if (Math.abs(error) < 0) {
  109. System.out.printf("Target missed by %f.2! Adjust launch angle up and try again", Math.abs(error));
  110. }
  111. else if (Math.abs(error) > 0) {
  112. System.out.printf("Target missed by %f.2! Adjust launch angle down and try again", Math.abs(error));
  113.  
  114. }
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. System.exit(0);
  132.  
  133.  
  134. }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement