Advertisement
Guest User

Untitled

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