d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Java Coin Flipper > I Need Some Help With A Java Program
Add Reply New Topic New Poll
Member
Posts: 1
Joined: Apr 2 2020
Gold: 0.00
Apr 2 2020 01:44pm
Below is the assignment requirements. I'm running into issues with the my code printing items from a file.

Coin-Toss Simulator

Write a class named Coin. The Coin class should have the following field: 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-argument constructor that calls the toss method, described next…
• A void method named toss that simulates tossing the coin. When the toss method is called, it
generates a random number in the range of 0 through 1 (remember we covered how to
generate random numbers in the lectures). If the random number is 0, then it sets the
sideUp field to “heads”. If the random number is 1, then it sets the sideUp field to “tails”.
• A method named getSideUp that returns the value of the sideUp field
• A method named showPastRun that opens the file named myoutput.txt (explained in next
section) and displays on the screen its content
Write a driver program that demonstrates the Coin class:
• The program should start by asking the user if the past program run needs to be displayed. If
yes, display the past program run using the showPastRun method
• Next, the program should display the following message: “New Coin-Toss Simulator Run:”
• Next, 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 a head is facing up, and the number of
times a tail is facing up, then display those values after the loop finishes.
• The program must also store all its output in a text file (named myoutput.txt) as shown in
class.
o Write a header message on the text file before each program run with the current date
and time. For example: *** Program Output of 2019-02-08 at 13:44 *** (check our
our textbook or https://www.w3schools.com/java/java_date.asp for details)
o The new output must overwrite the older one in the file
Run your program several times to test it.
Member
Posts: 30,945
Joined: Apr 13 2008
Gold: 11,996.69
Apr 3 2020 05:59am
What error message or result do you get when you try to write the results to a file?
Go Back To Homework Help Topic List
Add Reply New Topic New Poll