/* 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
}

- 6,385
- 7
- 48
- 65
-
In what line does the exception occur? – dxdy Oct 01 '15 at 22:55
-
File: (no associated file) [line: (no source location)] Error: java.lang.ArrayIndexOutOfBoundsException: 51966 – Gabriel Vergen Oct 01 '15 at 22:55
-
Post the stack trace and identify the highest stack in the trace which points to a class that you wrote and that line number. – Kon Oct 01 '15 at 22:57
-
It would help a lot if you showed us the full stacktrace – Kevan Oct 01 '15 at 22:56
-
This is a screencap of 1/4th of the stacktrace. http://imgur.com/ZxH9Ye4 It repeates itself from there. – Gabriel Vergen Oct 01 '15 at 23:43
-
See http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Raedwald Oct 02 '15 at 07:04
2 Answers
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?

- 244
- 1
- 9
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

- 1
- 1

- 138
- 8