aspose file tools*
The moose likes Beginning Java and the fly likes Random Card Problem Check Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random Card Problem Check" Watch "Random Card Problem Check" New topic
Author

Random Card Problem Check

Hai-Tan Le
Greenhorn

Joined: May 27, 2011
Posts: 6
Hey all, I have a program that repeatedly flips a coin (Heads or Tails) until 3 consecutive Heads occur, at which point the program will display how many flips it took. I am new to Java programming and was hoping if someone could look over my program. To me, the program looks a bit messy, so if there is a different way that you would have written it more cleanly, please let me know. Thanks

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18996
    
  40


1. I think it is a good idea to always use equals() to compare strings. In this case, it works because you are only using string literals -- and the string pool is helping, but this will quickly break if you do any string operation.

2. Instead of having three embedded conditionals, can you get it to work with another counter? Heads would increment it, and tails will set it back to zero. And of course, you would need a check to exit the loop when the count goes to three.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

You shouldn't write for loops like that. To most programmers it looks like an infinite loop, because i is almost always smaller than i +1.

Make a more descriptive sounding while loop, like: while (streak < 3).

Your trueFalse() method is named badly. You would expect it to return a String that reads "true" or "false".

You should also probably put all your fields at the top of your class. I had to look around for a bit to find rgen.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

I don't like the idea of using Strings to represent "Heads" and "Tails". I'd do one of two things:

- Just use a boolean - e.g. true for heads, false for tails

- If you want it to be more readable (so you don't need to remember which is which), define an enum with HEADS and TAILS as the two values

If you were going to continue using Strings, at least define a couple of constants for them. As things are you're risking mispelling it somewhere and creating a very hard to find error.
Hai-Tan Le
Greenhorn

Joined: May 27, 2011
Posts: 6
Thanks everyone, I really appreciate it.
Suletea Dorin
Greenhorn

Joined: Jun 13, 2011
Posts: 9


I didn't test or compile this but it should work (and it's not the best implementation)

1) Try using enumerations instead of String or Integers or whatever when you have a limited number of possibilities
2) Try avoiding infinite loops , even if you use a break statement in there (good habit)
3) If you must use an infinite loop (who knows) go with while(true) if you don't have statements after the loop or you will get an "unreachable code compile error".
4) Be afraid of nested "if's" they are evil. In big applications it can be a pain in the you know where.
5) Don't use the run() method if you are not working with threads it's confusing (good practice again) does program class extend thread?


I hope that helps.
 
wood burning stoves
 
subject: Random Card Problem Check