Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

16. Coin Toss Simulator Write a class named coin. The coin class should have the

ID: 3606800 • Letter: 1

Question

16. Coin Toss Simulator Write a class named coin. The coin class should have the following field: either" heads" or “tails" indicating A string named sideup. The sideUp field will hold the side of the coin that is facing up. . The coin classhould bave 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 tos 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

/*********************************Coin.java******************************************/

import java.util.Random;

/**

* The Class Coin.

*/

public class Coin {

/** The side up. */

private String sideUp;

/**

* Instantiates a new coin.

*/

public Coin() {

this.sideUp = "heads";

}

/**

* Toss.

* we are generating a random number between 0 and 1

* 0 means tail

* 1 means head

*/

public void toss() {

int min = 0;

int max = 2;

Random r = new Random();

int result = min + r.nextInt(max);

if (result == 0) {

this.sideUp = "tails";

} else if (result == 1) {

this.sideUp = "heads";

}

}

/**

* Gets the side up.

*

* @return the side up

*/

public String getSideUp() {

return this.sideUp;

}

/**

* The main method.

*

* @param args the arguments

*/

public static void main(String[] args) {

Coin c = new Coin();

int headCount = 0;

int tailCount = 0;

System.out.println("Initially Coin Facing " + c.sideUp);

for (int i = 0; i < 20; i++) {

c.toss();

String value = c.getSideUp();

System.out.println("Facing: " + value);

if ("heads".equals(value)) {

headCount++;

} else if ("tails".equals(value)) {

tailCount++;

}

}

System.out.println("Total Head Count: " + headCount);

System.out.println("Total Tail COunt: " + tailCount);

}

}

/*************************************output********************************************/

Initially Coin Facing heads
Facing: heads
Facing: heads
Facing: tails
Facing: tails
Facing: heads
Facing: heads
Facing: tails
Facing: heads
Facing: heads
Facing: heads
Facing: heads
Facing: heads
Facing: tails
Facing: tails
Facing: tails
Facing: heads
Facing: heads
Facing: heads
Facing: heads
Facing: tails
Total Head Count: 13
Total Tail COunt: 7

Thanks a lot. Please let me know if you have any doubt.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote