Results 1 to 14 of 14
Thread: Trouble with increments
- 02-12-2013, 10:44 PM #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?
Your time is much appreciated,
Steve
-
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.
- 02-12-2013, 10:50 PM #3
Member
- Join Date
- Dec 2012
- Location
- Des Moines, IA
- Posts
- 33
- 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(); }
- 02-13-2013, 01:31 AM #4
Member
- Join Date
- Feb 2013
- Posts
- 5
- Rep Power
- 0
- 02-13-2013, 01:34 AM #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.
- 02-13-2013, 10:19 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
As for your problem.
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.Please do not ask for code as refusal often offends.
- 02-13-2013, 12:26 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-13-2013, 02:40 PM #8
Member
- Join Date
- Jan 2013
- Location
- Kolkata,India
- Posts
- 59
- Rep Power
- 0
Re: Trouble with increments
Is it necessary to take nested loops?
IF not I have modified the code a bit.
Check if it works correctly for you?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--; } } }
**And sorry to the senior members and staffs who thins i am feeding him with code.
- 02-13-2013, 03:00 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
- 02-13-2013, 03:05 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
- 02-13-2013, 03:26 PM #11
Member
- Join Date
- Jan 2013
- Location
- Kolkata,India
- Posts
- 59
- Rep Power
- 0
Re: Trouble with increments
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 03:40 PM.
- 02-13-2013, 03:27 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
- 02-13-2013, 03:57 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
Re: Trouble with increments
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-13-2013, 04:02 PM #14
Member
- Join Date
- Jan 2013
- Location
- Kolkata,India
- Posts
- 59
- Rep Power
- 0
Similar Threads
-
While Program prints 0, I want it to print pi in 4 digit increments.
By super in forum New To JavaReplies: 5Last Post: 10-29-2012, 02:51 AM -
set increments in data plot
By lamorak24 in forum Advanced JavaReplies: 1Last Post: 12-19-2011, 07:01 AM -
Im having trouble with.......
By Java Noobie in forum New To JavaReplies: 1Last Post: 03-12-2011, 05:06 PM -
++/--increments for (C/JAVA) explained!!
By _ShivamKapoOr_ in forum New To JavaReplies: 5Last Post: 09-24-2010, 03:02 PM -
Pre- and post-increments
By SirFalcon in forum New To JavaReplies: 6Last Post: 10-29-2009, 03:29 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks