Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class BouncyBall
- {
- public static void main(String [] args){
- FakeGravity earth = new FakeGravity("Earth", 32, -0.5);
- FakeGravity discWorld = new FakeGravity("DiscWorld",6, -3);
- FakeGravity krypton = new FakeGravity("Krypton",74,-0.22);
- // intance of FakeGravity
- //call menu method to display menu to user
- menu();
- Scanner input = new Scanner(System.in);
- //local variable option assigned to values entered by user
- int option;
- double velocity;
- double height;
- // Prompts user to enter number choice from menu
- System.out.println("Enter your option: ");
- option = input.nextInt(); // reads number from user
- //error checking
- // 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.
- while(option < 0 || option > 3){
- menu();
- System.out.println("ERROR, please enter a valid number from the choices above: ");
- option = input.nextInt();
- if (option == 0){
- System.out.println("The program has ended");
- System.exit(0);
- }
- }
- while (option == 1 || option == 2 || option ==3)
- {
- // If user enter option 1, user is prompted to enter velocity, which is then stored in local variable called velocity.
- while (option == 1){
- System.out.println("You have selected Earth");
- System.out.println("Initial Velocity: ");
- velocity = input.nextDouble();
- // 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
- while (velocity < 0){
- System.out.println("ERROR you have entered a negative number, Please enter a poitive velocity: ");
- menu();
- velocity = input.nextDouble();
- }
- compute(earth, velocity); // pass earth and velocity as an argument to compute method
- earth.getVelocityDecay(); // retrieve value of earths velocity decay for use in calculation
- menu(); // calls menu method againt
- // once calculations are returned to user, prompt user again
- while (option == 1){
- System.out.println("Enter your option: ");
- option = input.nextInt();
- if (option ==1){
- System.out.println("You have selected Earth");
- }
- if (option == 0){
- System.out.println("The program has ended");
- System.exit(0);
- }
- System.out.println("Initial Velocity: ");
- velocity = input.nextDouble();
- while (velocity < 0){
- System.out.println("ERROR, you have entered a negative number, Please enter a positive velocity: ");
- menu();
- velocity = input.nextDouble();
- }
- compute(earth, velocity);
- earth.getVelocityDecay();
- menu();
- }
- }
- // if user chooses option 2, user is informed of planet choice and asked to enter initial velocity
- while (option == 2){
- System.out.println("You have selected Disc World");
- System.out.println("Initial Velocity: ");
- velocity = input.nextDouble();
- while (velocity < 0){
- System.out.println("ERROR, you have entered a negative number, Please enter a positive velocity: ");
- menu();
- velocity = input.nextDouble();
- }
- compute(discWorld, velocity);
- discWorld.getVelocityDecay();
- menu();
- if (option == 2){
- System.out.println("Enter your option: ");
- option = input.nextInt();
- System.out.println("You have selected DiscWorld");
- if (option == 0){
- System.out.println("The program has ended");
- System.exit(0);
- }
- System.out.println("Initial Velocity: ");
- velocity = input.nextDouble();
- while (velocity < 0){
- System.out.println("ERROR, you have entered a negative number, Please enter a positive velocity: ");
- velocity = input.nextDouble();
- }
- compute(discWorld, velocity);
- discWorld.getVelocityDecay();
- menu();
- // if user enters a negative velocity, user is returned to method menu which displays meny again and prompts user to enter choices again.
- }
- }
- // if user selects option 3, user is informed of planet name and prompted to enter intial velocity
- while (option == 3){
- System.out.println("You have selected Krypton, please enter an initial velocity: ");
- velocity = input.nextDouble();
- while (velocity < 0){
- System.out.println("ERROR, you have entered a negative number, Please enter a positive velocity: ");
- menu();
- velocity = input.nextDouble();
- }
- compute(krypton, velocity);
- krypton.getVelocityDecay();
- menu();
- if (option == 3){
- System.out.println("Enter your option: ");
- option = input.nextInt();
- while (option == 0){
- System.out.println("The program has ended");
- System.exit(0);
- }
- System.out.println("Initial Velocity: ");
- velocity = input.nextDouble();
- // anytime user enters negative velocity, user is returned to method menu and asked to enter value
- compute(krypton, velocity);
- krypton.getVelocityDecay();
- menu();
- }
- }
- }
- }
- // Static method menu
- // This menu simply prints out the menu so the user can see the choices
- public static void menu(){
- System.out.println("*** Gravity Menu ***");
- System.out.println("1.) Earth");
- System.out.println("2.) Disc World");
- System.out.println("3.) Krypton ");
- System.out.println("0.) Quit");
- System.out.println("**********************");
- }
- // Static method compute
- public static void compute(FakeGravity planet, double velocity){
- Scanner input = new Scanner(System.in);
- double height;
- int time;
- int bounceCounter;
- height = velocity; // initial height is zero
- time = 0; // inital time is zero
- double newHeight; // new height variable to remember new height
- bounceCounter = 0;
- while (bounceCounter <= 5)
- {
- while (time <= 20)
- {
- newHeight = height;
- height = (height + velocity) - planet.getVelocityDecay();
- velocity -= planet.getVelocityDecay();
- time = time +1;
- // newHeight is equal to the height
- if (newHeight < 0){ // everytime the new height is less than 0, BOUNCE! is printed
- velocity = (velocity * planet.getBouncinessFactor());
- height = (height * planet.getBouncinessFactor());
- System.out.println(" " +"BOUNCE!"+ " ");
- bounceCounter = bounceCounter + 1;
- }
- if (newHeight > 0){
- System.out.println("time: "+time +" " + " "+"Height: "+newHeight + " "+ "");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement