• Post Reply Bookmark Topic Watch Topic
  • New Topic

coin flip assignment help  RSS feed

 
jamie keene
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help writing the main method,



here is the outline for the main method

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jamie keene wrote:I need help writing the main method...

here is the outline for the main method

Okay, one step at a time. Create a Scanner object. How would you do this?
 
jamie keene
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've been working on it, and this is what i have so far, but i think i have it wrong
 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a good start! You've got a little logic error in your code to display the results of the coin tosses, though: if the coins are equal, are they necessarily both tails? However, since the instructor suggested just using the toString() method for this section of code, you might want to do something simpler than what you've got right now.
 
prem pillai
Ranch Hand
Posts: 87
Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Kurt said , you are almost there ... think a bit more ... you will get it right.

In the second last instruction , instructor is asking you to use toString() method to display the result of the coin flip. Just check that part.
In the last instruction , you have been asked to use equals()method and keep in mind that you have a local variable called "choice" with you holding the user input.
 
jamie keene
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm completely lost though, its like i'm treading water, can anyone give me an example or anything?
 
prem pillai
Ranch Hand
Posts: 87
Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a hint for you.



This is the toString() method in your Coin class. It returns a string "Heads" if the boolean variable "sideShowing" is TRUE. If sideShowing is FALSE , it returns a string "Tails". Now after flipping a coin (calling flip() method) , if you call this toString() method on the coin you have flipped , you will get a string value which represents the side of the coin. You just need to print that value.

Now try it...
 
jamie keene
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't know how to use toString though
 
jamie keene
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


so how do i make the coin1 == coin2 same and coin1 != coin2 different match the input from the user?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Passing a Coin variable to System.out.println will call the toString() method of that coin. (println method API)
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so how do i make the coin1 == coin2 same and coin1 != coin2 different match the input from the user?

You're near the end. You compared if both coins are the same, or are different. Then what do you need ? To compare if the user input is "Same" or "Different". How do you do that ? There are many ways. One could be :
1. If coins are same and user input is "Same", then win
2. Else if coins are different and user input is "Different", then win
3. Else lose
 
jamie keene
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

does that work? or will that be an error?
I don't know how to get the user input to read since it is a string, so by doing coice = same does that mean that if the user inputs same it will work?
 
jamie keene
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

is there a way to get the computer play another round without having to rerun the program?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(coin1.equals(coin2))&& choice = same

You compare Strings the same way you compared coins.

is there a way to get the computer play another round without having to rerun the program?

Make a loop and find a way to break out of the loop when the game is over (e.g. by asking the user if he wants to continue)
 
jamie keene
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

but i get the error of an odd repeat.. and how can i count the wins/loses to total at the end?

this is what the error looks like
----jGRASP exec: java Game
Enter your choice (Same or Different):
same
Computer flipped Tails and Heads
YOU LOSE
Would you like to try again (yes or no):
yes
Enter your choice (Same or Different):
Computer flipped Tails and Tails
YOU LOSE
Enter your choice (Same or Different):
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing first : there is a method called equalsIgnoreCase in the String class. Check it out and see if you can improve your code a bit.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At least three problems:
1. You're not asking the question everytime you lose.
2. You're not breaking out from the loop when you win
3. Why aren't you using readLine() like you did first ?
 
sreeram koneru
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jamie keene wrote:
but i get the error of an odd repeat.. and how can i count the wins/loses to total at the end?

this is what the error looks like
----jGRASP exec: java Game
Enter your choice (Same or Different):
same
Computer flipped Tails and Heads
YOU LOSE
Would you like to try again (yes or no):
yes
Enter your choice (Same or Different):
Computer flipped Tails and Tails
YOU LOSE
Enter your choice (Same or Different):


Just like you are asking for repeating the experiment again, you should probably check if the user entered "Same" or "Different" correctly or not every time and if they didn't entered the right answer for the choice question, you should probably ask them again to answer the question.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!