Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.10 KB | None | 0 0
  1. public class BouncyBall
  2. {
  3.  
  4.  
  5. public static void main(String [] args){
  6.  
  7. FakeGravity earth = new FakeGravity("Earth", 32, -0.5);
  8. FakeGravity discWorld = new FakeGravity("DiscWorld",6, -3);
  9. FakeGravity krypton = new FakeGravity("Krypton",74,-0.22);
  10. // intance of FakeGravity
  11.  
  12.  
  13.  
  14. //call menu method to display menu to user
  15. menu();
  16.  
  17.  
  18. Scanner input = new Scanner(System.in);
  19. //local variable option assigned to values entered by user
  20. int option;
  21. double velocity;
  22. double height;
  23.  
  24. // Prompts user to enter number choice from menu
  25. System.out.println("Enter your option: ");
  26. option = input.nextInt(); // reads number from user
  27.  
  28.  
  29. //error checking
  30. // If user selects an option other than 0-3 the user is taken back to the menu and prompted to re enter valid number choice.
  31. while(option < 0 || option > 3){
  32. menu();
  33. System.out.println("ERROR, please enter a valid number from the choices above: ");
  34. option = input.nextInt();
  35.  
  36. if (option == 0){
  37.  
  38. System.out.println("The program has ended");
  39. System.exit(0);
  40.  
  41. }
  42.  
  43. }
  44.  
  45. while (option == 1 || option == 2 || option ==3)
  46. {
  47.  
  48. // If user enter option 1, user is prompted to enter velocity, which is then stored in local variable called velocity.
  49. while (option == 1){
  50.  
  51. System.out.println("You have selected Earth");
  52. System.out.println("Initial Velocity: ");
  53. velocity = input.nextDouble();
  54. // Error check to see if velocity entered by user is negative. If it is negative the user will receive an error message and be prompted to enter a positive velocity
  55. while (velocity < 0){
  56.  
  57. System.out.println("ERROR you have entered a negative number, Please enter a poitive velocity: ");
  58.  
  59. menu();
  60.  
  61. velocity = input.nextDouble();
  62.  
  63. }
  64.  
  65. compute(earth, velocity); // pass earth and velocity as an argument to compute method
  66. earth.getVelocityDecay(); // retrieve value of earths velocity decay for use in calculation
  67. menu(); // calls menu method againt
  68.  
  69.  
  70.  
  71. // once calculations are returned to user, prompt user again
  72. while (option == 1){
  73. System.out.println("Enter your option: ");
  74. option = input.nextInt();
  75. if (option ==1){
  76. System.out.println("You have selected Earth");
  77.  
  78. }
  79. if (option == 0){
  80.  
  81. System.out.println("The program has ended");
  82. System.exit(0);
  83.  
  84. }
  85.  
  86. System.out.println("Initial Velocity: ");
  87. velocity = input.nextDouble();
  88.  
  89. while (velocity < 0){
  90.  
  91. System.out.println("ERROR, you have entered a negative number, Please enter a positive velocity: ");
  92. menu();
  93. velocity = input.nextDouble();
  94.  
  95.  
  96. }
  97.  
  98. compute(earth, velocity);
  99. earth.getVelocityDecay();
  100. menu();
  101.  
  102.  
  103.  
  104. }
  105.  
  106.  
  107. }
  108. // if user chooses option 2, user is informed of planet choice and asked to enter initial velocity
  109. while (option == 2){
  110.  
  111. System.out.println("You have selected Disc World");
  112. System.out.println("Initial Velocity: ");
  113. velocity = input.nextDouble();
  114.  
  115. while (velocity < 0){
  116.  
  117. System.out.println("ERROR, you have entered a negative number, Please enter a positive velocity: ");
  118. menu();
  119. velocity = input.nextDouble();
  120.  
  121. }
  122. compute(discWorld, velocity);
  123. discWorld.getVelocityDecay();
  124. menu();
  125.  
  126. if (option == 2){
  127.  
  128. System.out.println("Enter your option: ");
  129. option = input.nextInt();
  130. System.out.println("You have selected DiscWorld");
  131.  
  132. if (option == 0){
  133.  
  134. System.out.println("The program has ended");
  135. System.exit(0);
  136. }
  137.  
  138. System.out.println("Initial Velocity: ");
  139. velocity = input.nextDouble();
  140.  
  141. while (velocity < 0){
  142.  
  143. System.out.println("ERROR, you have entered a negative number, Please enter a positive velocity: ");
  144. velocity = input.nextDouble();
  145.  
  146. }
  147. compute(discWorld, velocity);
  148. discWorld.getVelocityDecay();
  149. menu();
  150. // if user enters a negative velocity, user is returned to method menu which displays meny again and prompts user to enter choices again.
  151.  
  152. }
  153. }
  154. // if user selects option 3, user is informed of planet name and prompted to enter intial velocity
  155. while (option == 3){
  156.  
  157. System.out.println("You have selected Krypton, please enter an initial velocity: ");
  158. velocity = input.nextDouble();
  159.  
  160. while (velocity < 0){
  161.  
  162. System.out.println("ERROR, you have entered a negative number, Please enter a positive velocity: ");
  163. menu();
  164. velocity = input.nextDouble();
  165.  
  166. }
  167. compute(krypton, velocity);
  168. krypton.getVelocityDecay();
  169. menu();
  170.  
  171. if (option == 3){
  172. System.out.println("Enter your option: ");
  173. option = input.nextInt();
  174.  
  175. while (option == 0){
  176.  
  177. System.out.println("The program has ended");
  178. System.exit(0);
  179.  
  180. }
  181. System.out.println("Initial Velocity: ");
  182. velocity = input.nextDouble();
  183.  
  184.  
  185. // anytime user enters negative velocity, user is returned to method menu and asked to enter value
  186. compute(krypton, velocity);
  187. krypton.getVelocityDecay();
  188. menu();
  189. }
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. }
  198. }
  199. }
  200. // Static method menu
  201. // This menu simply prints out the menu so the user can see the choices
  202. public static void menu(){
  203.  
  204. System.out.println("*** Gravity Menu ***");
  205. System.out.println("1.) Earth");
  206. System.out.println("2.) Disc World");
  207. System.out.println("3.) Krypton ");
  208. System.out.println("0.) Quit");
  209. System.out.println("**********************");
  210. }
  211.  
  212. // Static method compute
  213. public static void compute(FakeGravity planet, double velocity){
  214. Scanner input = new Scanner(System.in);
  215. double height;
  216. int time;
  217.  
  218. height = velocity; // initial height is zero
  219. time = 0; // inital time is zero
  220. double newHeight; // new height variable to remember new height
  221.  
  222. while (time <= 20)
  223. {
  224. newHeight = height;
  225.  
  226.  
  227.  
  228. height = (height + velocity) - planet.getVelocityDecay();
  229. velocity -= planet.getVelocityDecay();
  230. time = time +1;
  231. // newHeight is equal to the height
  232.  
  233. if (newHeight < 0){ // everytime the new height is less than 0, BOUNCE! is printed
  234. velocity = (velocity * planet.getBouncinessFactor());
  235. height = (height * planet.getBouncinessFactor());
  236. System.out.println(" " +"BOUNCE!"+ " ");
  237.  
  238. }
  239.  
  240. if (newHeight > 0){
  241.  
  242. System.out.println("time: "+time +" " + " "+"Height: "+newHeight + " "+ "");
  243.  
  244. }
  245.  
  246.  
  247. }
  248.  
  249.  
  250.  
  251.  
  252. }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement