Category: Toss a coin program in java

Toss a coin program in java

For this assignment you will create a game program using the coin class from the coin toss simulator exercise. The program should have three three instances of the coin class: one representing a quarter, one representing a dime, and one representing a nickel. During each round of the game, the program will toss the simulated coins. When a coin is tossed, the value of the coin is added to your balance if it lands heads-up. For example, if the quarter lands heads-up, 25 cents is added to your balance.

Nothing is added to your balance for coins that lands tails-up. The game is over when your balance reaches one dollar or more. If your balances is exactly one dollar, you win the game. You will lose if your balance exceeds one dollar.

The problem For this assignment you will create a game program using the coin class from the coin toss simulator exercise. Fix the problem by checking if the parameter is valid There cooking methods worksheets activities a repeatable pattern with coin, what is it?

Coin toss simulator

How can you refactor the code to make the code easier to read? Related examples Read java program user input Testing in java with Junit Format dollar amount in java.Here are the instructions: Write a program that tosses a coin times and prints the number of heads and tails.

Then declare your variables. You'll only need variables for the heads and the tails and the Random. Inside your "flips" loop, you need to use a random number generator which will return either 0 or 1. Decide ahead of time which value will be heads and which is tails. If you want 1 to indicate heads, then test your random number: if it's 1, increment heads, otherwise increment tails.

When the loop is finished, you will have the number of times it came up heads or tails. This is simple java homework. Do it yourself. If you get stuck after spending an hour on it, then ask for help. This code could be written in 5 mins tops. Hint: use Math. Answer Save. This is a simple little exercise to do. First, import the Random class. Random; Then start your program as you did. Charlie M. By the way, I don't think you need the variable coin.

Heads Or Tails Generator. Rae Lv 4. Still have questions? Get your answers by asking now.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time.

Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. You're not calling toss at the beginning of your loop. That's required to set a value to sideUp, and required to give sideUp to change every toss.

Java Program for Coin Change

The simple fix is put toss in your for loop before the if statement but I see a lot that can be done here. First I would add a constructor for the Coin class and add hcount and tcount to the class variables and make heads and tails constants:. Now add the toss and checkCoin method to the for loop before the if statement.

It should look like this:. Learn more. Asked 6 years, 1 month ago.

Can someone help with a Java Coin Toss simulator?

Active 4 years, 1 month ago. Viewed 13k times. Keppil Shouldn't you call toss in that for cycle? Other than that I don't see a problem. Active Oldest Votes.

Please accept the answer if it helped solve the problem. Dom Dom 1, 4 4 gold badges 24 24 silver badges 31 31 bronze badges.I am really lost. The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.

Forum: Beginning Java. Ok so I am a newbie to java coding and I have an assignment to do a coin flip program. No problem there, except he also wants the program to skew the results. So, in the coding we have to add a ratio or some coding to improve the chance of either heads or tails???

Jeanne Boyarsky. I like Michah, A ratio or percentage is a good way to think of it. I feel slightly dumber than my IQ but I am not really sure, I have tried changing numbers around within the coding and it has not changed anything, thanks ever so much for the help!

Scanner; import java. I tried changing your. See this ideone example. Got it!!! Created a whole new class and bingo I haven't a foggy clue what was really wrong with the other just now but creating a whole new class and creating from the beginning did it!! Thanks ever so much, you confirmed that my code was likely right but something else was a miss so I went digging!

Do you think you might be able to help, I would sure appreciate it in bunches!

toss a coin program in java

I swear my IQ is just fine but this stuff has me just a little batty trying to get used to it and learning it! Ganesh Patekar. Welcome to CodeRanch!

Tossing coin, rolling dice and choosing a card in Java

Glad to hear you got the solutioncan you please post the solved code so It may also be helpful to others who perhaps have same problem. Of course you can create a separate topic for your new problem, volunteers who knows the answer will definitely try to help you. Ok so he isn't preferential of the boolean method what other way is there to represent 50 : 50 or 60 : 40 or etc??? Knute Snortum. What "boolean method"? I am really lost Me too!

Could you post your code with a comment on the line s that are the problem? Junilu Lacar. If so, consider this: Currently, you have range constant, skewpoint movable: Piet Souris. That prof has a strange sense of humor in that case. This is exactly what you do in your flip coin code.Sign in to post your reply or Sign up for a free account. By using this site, you agree to our updated Privacy Policy and our Terms of Use.

Manage your Cookies Settings. Join Now login. Ask Question. I have to make a program that would ask a user for their guess heads or tails and tells them if its correct or not then it has to ask the user if they want to play again. I figured out how to do this but the part that i am stuck on is how to print a summary at the end of this program tracking how many wins and how many losses. Enter heads or tails. Enter anything else if you wish to stop. Dec 8 ' Post Reply. Share this Question. Expert 2.

Outside of the loop, you can define variables such as totalNumOfGames, totalWon, totalLost, etc and set them all to 0.

toss a coin program in java

Inside your loop, increment totalNumOfGames, and if they win, increment totalWon, etc. Once your loops exits, you will have all the necessary information to make a summary. That code is also hard to follow, because it is all over the place consider using the code tags to sperated different pieces of cod Expand Select Wrap Line Numbers. Dec 9 ' Similar topics How much does a Draped Bust coin cost? Question stats viewed: replies: 5 date asked: Dec 8 '06 Follow this discussion.

Follow us to get the Latest Bytes Updates. Help with Coin Program P: Ganon11 Outside of the loop, you can define variables such as totalNumOfGames, totalWon, totalLost, etc and set them all to 0. Dec 8 '06 reply. P: Cancel Changes.I have to write a class named Coin, which needs to have a String named sideUp which will hold either "heads" or "tails" indicating the side of the coin that is facing up.

When the toss method is called it randomly determines the side of the coin that is facing up and sets the sideUp field accordingly. The program demonstrates the Coin class. It should create am instance of the class and display the side that is initially facing up.

Then use a loop to toss the coin 20 times. Each time the coin is tossed, display the side that is facing up. I could post the code that I have so far, I'm just having compiling issues because something is off in my code, but I'm just confused as to what to do next. First, since you only have two possible states, you might as well do a Random Boolean generator.

Consider using what's called a "ternary if-else" operator. It looks more confusing but makes for quicker and cleaner code most times. On random numbers, the Random constructor creates a random number generator, and if you call it's nextBoolean method you'll get your boolean value.

Every time the toss method is called you can call your pre-constructed Random instance to get a new value for the coin. I'd recommend making another method for the toss counter. Create variables headsCount and tailsCount before the loop, run your toss method, then get the current side up, then with an if-else function check the state of the isHeads boolean and add to the proper count.

If you've been given this assignment I assume you've been taught how to print output, but just in case look up System. If you need to check any documentation on this, try using the sources below. Simply searching "java function " also tends to work. For one, your no-arg constructor is off. Second, your main method is trying to use a Coin which should properly be a BlackmanCoin again that it hasn't instantiated yet. Once you set up the no-arg constructor as above, change the main method to include something like:.

Here you're setting an object, Coin, to an int. This is not only not wise, but also not, I think, what you intended to do. My recommendation is as follows:. Edit the if function this is required!

This is because you currently have it checking sideUp, but while it may be forgiving this time with the main method inside your coin class, this is very bad programming practice.

toss a coin program in java

Most methods checking for the sideUp value will NOT have access to the sideUp declared in the coin's instance. And actually, I don't think it will work as intended even here.And no one ever goes to them and gives them an award. Forum: Beginning Java. I need help writing the main method, import java. I like Okay, one step at a time. Create a Scanner object.

Day 3 of Code: If-Statements! (+ A Coin Toss Game)

How would you do this? Kurt Van Etten. 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. As Kurt saidyou are almost there In the second last instructioninstructor is asking you to use toString method to display the result of the coin flip.

Just check that part. In the last instructionyou 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. Here is a hint for you. Now after flipping a coin calling flip methodif you call this toString method on the coin you have flippedyou will get a string value which represents the side of the coin.

You just need to print that value. Now try it Passing a Coin variable to System. 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. Make a loop and find a way to break out of the loop when the game is over e. 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. At least three problems: 1. You're not asking the question everytime you lose.

About Author



Leave a Reply

Your email address will not be published. Required fields are marked *