Solved

# Bouncing Ball

Posted on 1997-08-27

Hi,

I'm going through the book "Java 1.1 Programming in 24 Hours," and I'm stuck on an exercise. I've entered the code for a bouncing ball applet from the book, and for additional practice we've been told to "Change the...applet so the ball loses 10% of its bouncing strength each time it hits the ground. You can do this by changing the height variable at a certain tile within the run() method."

Here's the run() method:

public void run() {

while (true) {

repaint();

current += (float) 0.1;

if (current > 3)

current = (float) 0;

xPosition += xMove;

if (xPosition > (getSize().width - 111))

xMove *= -1;

if (xPosition < 1)

xMove *= -1;

double bounce = Math.sin(current) * height;

yPosition = (int) (height - bounce);

try { Thread.sleep(200); }

catch (InterruptedException e){ }

}

}

I've tried at least a dozen different solutions, most of them involving reducing the height variable by 10% at various points in the run() method, but nothing's worked yet. I even wrote another program to allow me to trace the values of the bounce height, but nothing's worked. I'm not so sure I see the way in which the sin curve defines the movement of the ball. Can someone point me in the right direction?

Thanks!