* I\'m using Eclipse Objectives: Using for loops, nested for loops and parameter
ID: 3555276 • Letter: #
Question
* I'm using Eclipse
Objectives: Using for loops, nested for loops and parameterized methods.
a. Write a program using a for loop to calculate how high a ball bounces on each of its first 20 bounces. The ball starts at 10 meters above the bounce surface. Each bounce brings the ball to 4/5 of its previous height. Thus, the first bounce it will reach 8 meters. Print the 20 heights, labeled such as: (Ignore the strange precision of some output values.)
After bounce 1 the ball reaches 8.0 meters
After bounce 2 the ball reaches 6.4 meters
After bounce 3 the ball reaches 5.120000000000001 meters
...
HERE IS WHAT I HAVE:
public class BallBounces {
public static void main(String[] args) {
for ( int i = 1; i <= 20; i++ ){
double j = 10;
System.out.print("After bounce " + i + " the ball reaches ");
System.out.println(j*4/5 + " meters");
}
}
}
Explanation / Answer
http://ideone.com/QalIjC
class BallBounces {
public static void main(String[] args) {
double j = 10;
for ( int i = 1; i <= 20; i++ ){
System.out.print("After bounce " + i + " the ball reaches ");
System.out.println(j + " meters");
j = 4*j/5;
// you have to update the value of j after every loop
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.