Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Coin Toss Simulator in Java NetBeans Write a class named Coin. The Coin class should have the following field: A String named sideUp. The sideUp

Coin Toss Simulator in Java NetBeans

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-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 demonstrate 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 fails is facing up and display those values after the loop finishes.

Notes:

  • You will need to randomly toss the coin. It is recommended that you use the Random class which is described beginning on Page 249 of the text. The best approach is to randomly generate a 0 or a 1 and select the 0 to represent heads and the 1 to represent tails. You can also use Math.random() but the Random class is a bit more convenient to use in this case.

Example Output

image text in transcribed

CIT111 Spring 2019 Assignment 6 Coin Toss Simulator programmed by: Loop D. Loop The coin is being tossed 20 times Toss 1: heads Toss 2: tai1s Toss 3: heads 1033 4: heads Toss 6: heads Toss 7: tai1s Toss 9: heads To 33 10: heads Toss 11: tai1s T033 12: heads T033 13: heads Toss 14: tai1s To 33 15: heads To 33 16: heads To 33 17: heads Toss 18: tai1s T033 19: heads T033 20: heads The results of the tossing Heads came up 13 times Thank you for tossing the coin 20 times Good bye

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Master The Art Of Data Storytelling With Visualizations

Authors: Alexander N Donovan

1st Edition

B0CNMD9QRD, 979-8867864248

More Books

Students also viewed these Databases questions

Question

a. Describe the encounter. What made it intercultural?

Answered: 1 week ago