The main issue Im having with my code right now is that I cant generate the outp
ID: 3640389 • Letter: T
Question
The main issue Im having with my code right now is that I cant generate the output that Im being asked.this is the exercise
Write a program that stimulates a bouncing ball by computing its height in feet at each second as time passes on a simulated clock. At time zero, the ball begins at height zero and has an initial velocity supplied by the user. (An initial velocity of at least 100 feet per second is a good choice.) After each second, change the height by adding the current velocity; then subtract 32 from the velocity. If the new height is less than zero, multiply both the height and the velocity by -0.5 to simulate the bounce. Stop at the fifth bounce. The output from your program should have the following form:
Enter the initial velocity of the ball: 100
Time: 0 Height: 0.0
Time: 1 Height 100.0
Time: 2 Height 168.0
Time: 3 Height 204.0
Time: 4 Height 208.0
Time: 5 Height 180.0
Time: 6 Height 120.0
Time: 7 Height 28.0
Bounce !
Time: 8 Height 48.0
This is my code as of right now and I would like to have some insight into how I can fix this issue. please be as explanatory as possible.
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
double height = 0;
int bounce = 0;
int time = 0;
System.out.println("Enter the initial velocity of the ball:");
int velocity = keyboard.nextInt();
while ( bounce < 5) {
// displaying time and height
System.out.println("time:" + time + " height:" + height);
//updating time
time++;
// calculate new height
height = height + velocity;
// decrease velocity
velocity = velocity - 32;
if (height < 0)
{
// change velocity
velocity = (int)-0.5 * velocity;
// recalculate height
height = -0.5*height;
System.out.println("Bounce!");
bounce++;
}
THIS IS MY CURRENT OUTPUT FROM MY CODE
Bounce!
time:13 height:4.0
time:14 height:4.0
Bounce!
time:15 height:14.0
time:16 height:14.0
Bounce!
Explanation / Answer
The problem is how you are doing the change in velocity and height at the time of bounce. By casting -.5 to an integer it will change it to -1, since an integer datatype cannot represent anything past the decimal, it rounds it. Replace the lines you calculate the velocity and height with the following: velocity *= -.5; height *= -.5; If you aren't familiar with the *= syntax, it works like the following. The next two lines function identically. X = X * Y; X *= Y; I've tested it with these changes and it gives the following output: Enter the initial velocity of the ball: 100 time:0 height:0.0 time:1 height:100.0 time:2 height:168.0 time:3 height:204.0 time:4 height:208.0 time:5 height:180.0 time:6 height:120.0 time:7 height:28.0 Bounce! time:8 height:48.0 time:9 height:126.0 time:10 height:172.0 time:11 height:186.0 time:12 height:168.0 time:13 height:118.0 time:14 height:36.0 Bounce!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.