1. Member
Join Date
Feb 2013
Posts
5
Rep Power
0

## Trouble with increments

Hello,

I was wondering if anyone could give me a hand with one of homework assignments. The object of the program is:

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
After bounce 4 the ball reaches 4.096000000000001 meters

This is as far as I have been able to get:

public class Bouncy_Ball {

public static void main(String[] args) {

bounce();

}
public static void bounce() {
for (int i=1; i<=20; ++i){
System.out.print("After bounce " + i + " the ball reaches ");
for (double j=10; j>0; j--){
System.out.println(j-j*.2+ "meters.");
}
}

}
}

Example of my output:

After bounce 1 the ball reaches 8.0meters.
7.2meters.
6.4meters.
5.6meters.
4.8meters.
4.0meters.
3.2meters.
2.4meters.
1.6meters.
0.8meters.
After bounce 2 the ball reaches 8.0meters.
7.2meters.
6.4meters.
5.6meters.
4.8meters.
4.0meters.
3.2meters.
2.4meters.
1.6meters.
0.8meters.
After bounce 3 the ball reaches 8.0meters.
7.2meters.
6.4meters..... and so on to bounce number 20.

Is this an easy fix or am I way off the mark here? I have spent quite a bit of time on this problem and I am not making any progress. Can someone let me know what I am doing wrong here?

Steve

2. ## Re: Trouble with increments

You never replied to answers given to your question posted on 2/7/13. Consider replying to the kind folks who took the time from their day to try to help you out. It often motivates others to help you with current and future posts.

3. Member
Join Date
Dec 2012
Location
Des Moines, IA
Posts
35
Rep Power
0

## Re: Trouble with increments

I'll say it before someone else does. You need to use the code tags. It really does here read your code.

Java Code:
```
private static void chars(String input) {
charCount += input.replaceAll("\\s", "").length();
}```

4. Member
Join Date
Feb 2013
Posts
5
Rep Power
0

## Re: Trouble with increments

Originally Posted by Fubarable
You never replied to answers given to your question posted on 2/7/13. Consider replying to the kind folks who took the time from their day to try to help you out. It often motivates others to help you with current and future posts.
I went and thanked them. I had just forgot and I don't take the help and time people dedicate on these types of forums for granted.

5. Member
Join Date
Feb 2013
Posts
5
Rep Power
0

## Re: Trouble with increments

hmm still not quite sure what you mean by code tags. The professor wants to see us use nested for loops and parameterized methods.

6. Moderator
Join Date
Apr 2009
Posts
13,075
Rep Power
23

## Re: Trouble with increments

Wrap your code in [code] tags [/code] so it retains its formatting.
That's what's meant by code tags.
Unformatted code is hard to read and follow.

You seem to have a loop within a loop.
You also don't seem to have any sort of starting height variable.

Describe how you would do this by hand, because it looks like you dove into the code without coming up with a strategy.

7. ## Re: Trouble with increments

Your 'simulation' assumes a linear relationship between the number of bounces and the height of the ball after that many bounces, say, 'n'; your simulation does 0.8*n where n goes down 10, 9, 8 ... but that's not what the assignment text states; it states after every bounce: newHeight= oldHeight*0.8; and the oldHeight starts at 10. That is not a linear relationship.

kind regards,

Jos

8. Member
Join Date
Jan 2013
Location
Kolkata,India
Posts
90
Rep Power
0

## Re: Trouble with increments

Is it necessary to take nested loops?
IF not I have modified the code a bit.
Java Code:
```public class Bouncy_Ball {

public static void main(String[] args) {
bounce();
}

public static void bounce() {
int j=10;
for (int i=1; i<=10; ++i){
System.out.print("After bounce " + i + " the ball reaches ");
System.out.println(j-j*.2+ "meters.");
j--;

}

}
}```
Check if it works correctly for you?
**And sorry to the senior members and staffs who thins i am feeding him with code.

9. Moderator
Join Date
Apr 2009
Posts
13,075
Rep Power
23

## Re: Trouble with increments

Originally Posted by harshit shah
**And sorry to the senior members and staffs who thins i am feeding him with code.
You obviously feel this is a solution, and realise posting full solutions is frowned upon, so why do it?

10. ## Re: Trouble with increments

Originally Posted by harshit shah
Check if it works correctly for you?
Have you checked it yourself? (spoiler: it is utterly incorrect)

kind regards,

Jos

11. Member
Join Date
Jan 2013
Location
Kolkata,India
Posts
90
Rep Power
0

## Re: Trouble with increments

Originally Posted by JosAH
Have you checked it yourself? (spoiler: it is utterly incorrect)

kind regards,

Jos
okay just rempve the j-- from my code and it shall work fine
Java Code:
```public class Bouncy_Ball {

public static void main(String[] args) {
bounce();
}

public static void bounce() {
double j=10;
for (int i=1; i<=10; ++i){
System.out.print("After bounce " + i + " the ball reaches ");
j=j-(j*.2);
System.out.println(j+ "meters.");

}

}
}```
Last edited by harshit shah; 02-13-2013 at 04:40 PM.

12. Member
Join Date
Feb 2013
Posts
3
Rep Power
0

## Re: Trouble with increments

Stephen, apart from the missing code tags, there are a number of problems in your program.
- you most probably did not want to count down from 10 to 1 for every single bounce, did you?
- as Tolls mentioned, the bouncing height does not decrease in a linear fashion. You should use a height variable and modify it to the new jumping height for each bounce in the way your homework asks

Possible output for a correct solution:
Start: 10.0m
1: 8.0m
2: 6.4m
3: 5.12m
4: 4.096m
5: 3.2768m
6: 2.62144m
7: 2.097152m
8: 1.6777216m
9: 1.34217728m
10: 1.073741824m
11: 0.8589934592m
12: 0.68719476736m
13: 0.549755813888m
14: 0.43980465111m
15: 0.351843720888m
16: 0.281474976711m
17: 0.225179981369m
18: 0.180143985095m
19: 0.144115188076m
20: 0.115292150461m

13. ## Re: Trouble with increments

Originally Posted by harshit shah
okay just rempve the j-- from my code and it shall work fine
That is not just 'removing the j--'; but I noticed you figured out why your previous spoonfeeding was utterly incorrect. Better test your code before posting it and even better than that is not to spoonfeed at all.

kind regards,

Jos

14. Member
Join Date
Jan 2013
Location
Kolkata,India
Posts
90
Rep Power
0

## Re: Trouble with increments

Originally Posted by JosAH
That is not just 'removing the j--'; but I noticed you figured out why your previous spoonfeeding was utterly incorrect. Better test your code before posting it and even better than that is not to spoonfeed at all.

kind regards,

Jos
yup,mate.it was my bad was in a hurry but I do not call it a spoon feeding.he may get knowledge from my code and learn not to do unnecessary nested for loops because the make code complex

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•