• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ball Bounces  RSS feed

 
ashkan ahmadi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A particular ball bounces to 80% of its drop height. Write a program which will determine
how many bounces it takes for the bounce height to be less than 30% of the initial height.
Sample output:
Original height = 100
Lower bounce height = 30.0
Bounce 1 height = 80.0
Bounce 2 height = 64.0
Bounce 3 height = 51.2
Bounce 4 height = 41.0
Bounce 5 height = 32.8
Bounce 6 height = 26.2
It takes 6 bounces to go below 30%.
Design your program so that it would work with any bounce height, bounce ratio, and lower
limi

This is my code. i don't know why it does not go to loop.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code does not have a valid main() method for the java program to use to start its execution.

How do you try to execute the program?

Are there any errors?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also...you would do well to learn now not to put much code in the main method...we have a KB on that found here
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not convinced about the loop condition in line 7 and the formula for Math#pow in line 9 looks positively wrong. Your code most certainly enters the loop, as you will find if you write this as line 9½:-
System.out.println(newh);
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I was mistaken. If you enter the value of newh in line 7, you will find out you are correct and you never enter the loop.
 
Jerry Ye
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I agree with Norman Raddar's response. There's no valid main fonction.
Besides, I would like to suggest you to give your variables some meaningful names, like lowLimi, bounceHei, bounceRatio. It really helps.
And I don't think you've given newh a value that would make it into the loop.
Hope it helps.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Ye wrote:Besides, I would like to suggest you to give your variables some meaningful names, like lowLimi, bounceHei, bounceRatio. It really helps.

Well, you're right about meaningful names but the examples you gave are not very good. Why abbreviate the names like that? What's a "limi"? What's a "hei"? Nothing is gained by losing a few letters at the end of those words; truncating them like that just makes the names less readable.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And of course there is the integer division problem:  (4/5) gives 0
 
Jerry Ye
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Jerry Ye wrote:Besides, I would like to suggest you to give your variables some meaningful names, like lowLimi, bounceHei, bounceRatio. It really helps.

Well, you're right about meaningful names but the examples you gave are not very good. Why abbreviate the names like that? What's a "limi"? What's a "hei"? Nothing is gained by losing a few letters at the end of those words; truncating them like that just makes the names less readable.
Well, I think you are right. Please forgive me, I'm just someone weak at thinking of a new name
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Ye wrote:Please forgive me, I'm just someone weak at thinking of a new name

No need to apologize. We're all here to learn. Thank you for acknowledging that I'm right though

If you want to be a better programmer, it's essential that you are always able to come up with good, meaningful names. You have the right idea though so just keep practicing. The book Clean Code by Robert Martin has a whole chapter about choosing good variable names. That link also takes you to a list of related articles, including that chapter I just mentioned. I suggest you go and read some of that stuff. It's very educational.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Ye wrote:. . . I'm just someone weak at thinking of a new name
You are trying too hard. Don't think about it. If somebody comes to you and asks, “What does ysm mean?”  you will say, “That's the bounce height.” Then you change ysm to bounceHeight. Let the names come naturally.

And welcome to the Ranch
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!