• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not so good with boolean and loops  RSS feed

 
Christina Bremmerman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it looks like I'll be stopping in here once a week for a few more weeks...Java is killing me...aaaaaaanyway you guys are awesome and I really appreciate the help that I get here
I think I've narrowed down my problem to be in this part of my code code
basically giveFlat is supposed to give a 1% chance of the vehicle getting a flat tire. This I'm not really sure if I'm doing right with the random generator, but technically with the random number getting over 99 with 0-100 us a 1% chance right? Or should it be 101?
That isn't my main issue however...I can't get it to print true, I've been fooling around with the numbers and I took my random out and set nextInt to 100 and I still get false...also I managed to get true once but everything was still coming up true when I tweaked the random generator to make the odds in my favor...and that was too long ago for me to remember how I got it to show true...

 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are missing one thing here.. you are printing the value of hasFlat that is initialized as false by default.

instead of hasFlat use the method giveFlat that will give you the value of hasFlat according to logic. because this is the method giveFlat the is setting the value of hasFlat and returning accordingly.

It will be great if you can paste the small code with class and main method.
 
Christina Bremmerman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that is my problem maybe there's something wrong with the rest of my code... I have to use giveFlat as part of goOneHour in Car Class
Actually looking at this again I think there is something wrong with the class that I put it in...we are working on inheritance and the like to I have a parent class that is Vehicle(Which doesn't touch the flat tire issue at all) the child class is wheeledVehicles and Car is the grandchild, if my terminology is right.





 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are testing booleans use either if (hasFlat) if you're testing if it's true or if (!hasFlat) if you're testing if it's false.
That way you avoid writing
if (hasFlat = true)
instead of
if (hasFlat == true)
which is what you've done in a couple of places.

Edit: Note that this applies to conditions in while loops as well
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:If you are testing booleans use either if (hasFlat) if you're testing if it's true or if (!hasFlat) if you're testing if it's false.
That way you avoid writing
if (hasFlat = true)
instead of
if (hasFlat == true)
which is what you've done in a couple of places.

Edit: Note that this applies to conditions in while loops as well


In fact there's no reason to ever use == or != with true or false.


 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's perfect Jeff . Joanne Neal advised to OP about his post == at OP's last statement. [I think ]
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:That's perfect Jeff . Joanne Neal advised to OP about his post == at OP's last statement. [I think ]


Yeah, I know. I was just expanding on it to a general rule.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's Excellent Jeff
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not related to your question, but some things can be written shorter, for instance:
as
 
Christina Bremmerman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OP about HER post....Just saying...It's just like the surgeon brain teaser, everyone just assumes everyone is male...

I had no idea about the booleans, now I know and I am grateful because that was indeed my problem(I think anyway, I am still combing through it to make sure).
My instructor has assumed we learned a lot of stuff in the previous class that I wasn't taught and it isn't helping that he doesn't use a book he just give us code examples and powerpoint printouts, and none of them have used or even explained about booleans so thanks Jeff for expanding on that, it makes sense now
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back to another part of your original question:


This sort of thing happens so often that it has a name, and even its own Wikipedia page: Off-by-one error.
 
Christina Bremmerman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Back to another part of your original question:


This sort of thing happens so often that it has a name, and even its own Wikipedia page: Off-by-one error.


Ah I know of this...and I guess I missed it because of my other issues

I can either change the 100 to 101 or change the 99 to 98? Would it work as well to change >99 to ==99?
 
Ryan Sykes
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christina Bremmerman wrote:
Ah I know of this...and I guess I missed it because of my other issues

I can either change the 100 to 101 or change the 99 to 98? Would it work as well to change >99 to ==99?

You could do any of those...they would all work. == 99 might be the clearest from a clarity perspective (at least to me).
 
Christina Bremmerman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for your help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!