Write a class named Coin. A Coin class should have the following fields: A Strin
ID: 3537906 • Letter: W
Question
Write a class named Coin. A Coin class should have the following fields:
A String named sideUp. The sideUp field will hold either "heads" or "tails" indicating the side of the coin that is facing up
The Coin class should have the following methods:
A no-arg constructor that randomly determines the side of the coin that is facing up ("heads" or "tails") and initializes the sideUp field accordingly
A void method named toss that simulates the tossing of the coin. When the toss method is called, it randomly determines the side of the coin that is facing up ("heads: or "tails") and sets the sideUp field accordingly
A method named getSideUp that returns the value of the sideUp field
Write a program that demonstrates the Coin class. The program should create an 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. The program should keep count of the number of times heads is facing up and the number of times tails is facing up, and display those values after the loop finishes.
Explanation / Answer
First, since you only have two possible states, you might as well do a Random Boolean generator. Basically do something like if isHeads, sideUp = "heads" else "tails" situation. 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 20-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.out.print() and System.out.println() if you need to. For one, your no-arg constructor is off. Constructors have no return type, and must EXACTLY MATCH the class name. In your case, change void Coin(){...} to public BlackmanCoin(){...} and you should be set there. 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: coinToss coin = new BlackmanCoin(); Then you can change the references from Coin (such as Coin.toss();) to coin (coin.toss();). Finally, your do block is set up so that you have it doing Coin = heads or Coin = tails. 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!) to coin.getFace(). 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. 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. Change it from Coin = heads; or Coin = tails; to simply heads++; or tails++; Keep an eye on what your print lines are doing, I'm not certain it's what you expect right now. Your big problems definitely seem to be the non-instantiated coin, the lack of a no-arg constructor, and the structure of the if function. You're very much on the right track, but the syntax of how it all needs to be done has gotten you a little mixed up. Don't worry, it happens to all of us! And, more importantly, you'll get it down with time. i think i can get you source code or binar class file.. let me know if you need any further clarifications and please dont forget to rate my answer :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.