-1
/* This program simulates a bouncing ball by computing its height
 * in feet each second as time passes on a simulated clock.
 * Stop at the fifth bounce.
 *
 *
 * Compile: javac GabeVergen_Ball.java
 * Run GabeVergen_Ball
 */

// Import scanner

    import java.util.Scanner;

// Identify main class

    public class GabeVergenBall
    {

// Identify method

    public static void main (String args[])
    {
// Create the scanner

    Scanner keyboard = new Scanner(System.in);

// User prompt

    System.out.println("Enter the velocity of the ball: ");

//Identify velocity

    double velocity;

//Input is read

    double velocity = keyboard.nextDouble();

// Identify variables

    int time = 0;
    int bounce = 0;
    double height = 0;

//If bounce is less than 5, execute

    while(bounce < 5)
    {
//Time and height are displayed

     System.out.println("Time: " + time);
     System.out.println("Height:" + height);

//Update variable time

     time++;

//Uptade variables velocity and height

     height += velocity;
     velocity -= 32;

//If height is less than 0, execute

         if (height < 0)
         {
//In order to simulate the bounce, multiply height and velocity by -0.5

          height *= -0.5;
          velocity *= -0.5;

      //Display bounce

          System.out.println("Bounce!");

      //Bounce count
          bounce++;
     } //end of if statement
    }//end of while statement
    //Print end statement when ball stops

        System.out.println("Stop");

   }//End of method
 }
aimme
  • 6,385
  • 7
  • 48
  • 65

2 Answers2

0

The source code you provided has no errors other than the double declaration of velocity. (to fix simply remove the first declaration).

Here is the output I get when set velocity = 0.0 (I didn't want to get input from keyboard).

C:\temp>java bounce Enter the velocity of the ball: Time: 0 Height:0.0 Time: 1 Height:0.0 Bounce! Time: 2 Height:16.0 Time: 3 Height:48.0 Time: 4 Height:48.0 Time: 5 Height:16.0 Bounce! Time: 6 Height:24.0 Time: 7 Height:72.0 Time: 8 Height:88.0 Time: 9 Height:72.0 Time: 10 Height:24.0 Bounce! Time: 11 Height:28.0 Time: 12 Height:84.0 Time: 13 Height:108.0 Time: 14 Height:100.0 Time: 15 Height:60.0 Bounce! Time: 16 Height:6.0 Time: 17 Height:58.0 Time: 18 Height:78.0 Time: 19 Height:66.0 Time: 20 Height:22.0 Bounce!

I wonder if the problem might be something other than the source code you are providing. Can you get a simple "Hello World" program to compile and execute?

Owen Ivory
  • 244
  • 1
  • 9
0

The problem:

The problem is the folder in which you are storing the file

The solution

Save the file in a Folder with the same name that the class

Community
  • 1
  • 1