12.
CODE:
package packages_example;
import java.util.Random;
// creating class coin
public class Coin {
private String sideUp;
// constructor
public Coin(){
// generating a random number between 0 and 1
// and assigning side up using it
Random rand = new Random();
rand.setSeed(System.currentTimeMillis());
int choice = rand.nextInt(2);
if (choice == 0) sideUp = "heads";
else sideUp = "tails";
}
public void toss() {
// generating a random number between 0 and 1
// and assigning side up using it
Random rand = new Random();
int choice = rand.nextInt(2);
if (choice == 0) sideUp = "heads";
else sideUp = "tails";
}
// return side up as string
public String getSideUp(){
return sideUp;
}
// main function
public static void main(String[] args) {
Coin c = new Coin();
System.out.println("Coin created with " + c.getSideUp() + " as side up.");
for (int i = 1; i <= 20; i++) {
c.toss();
System.out.printf("Toss #%d, Side up : %s.\n",i,c.getSideUp());
}
}
}
OUTPUT:

13.
CODE:
package packages_example;
public class CoinTossGame {
public static void main(String[] args) {
// Coin variables
Coin quarter = new Coin();
Coin dime = new Coin();
Coin nickel = new Coin();
float balance = 0;
int counter = 0;
while (true) {
System.out.println("Round #" + ++counter + ":");
// if balance is lese than 1 then toss the coins
if (balance < 1) {
quarter.toss();
dime.toss();
nickel.toss();
// check if the coins are heads up, then add value to balance
if (quarter.getSideUp().equals("heads"))
balance += 0.25;
if (dime.getSideUp().equals("heads"))
balance += 0.10;
if (nickel.getSideUp().equals("heads"))
balance += 0.05;
System.out.println("Quarter : \t\t" + quarter.getSideUp());
System.out.println("Dime : \t\t\t" + dime.getSideUp());
System.out.println("Nickel : \t\t" + nickel.getSideUp());
System.out.println("Balance : \t\t" + balance);
System.out.println();
}
// if balance is 1 then you've won the game
else if (balance == 1) {
System.out.println("You win the game");
System.exit(1);
}
// if balance has exceeded 1 then you've lost the game
else {
System.out.println("You have lost the game, your balance has exceeded $1.00.");
System.exit(1);
}
}
}
}
OUTPUT:

12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field ...
c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in a single program (Gaddis, p812, 9E). Scans of these problems are included below. Your program should have two sections that correspond to Problems 12 and 13: 1) As described in Problem 12, implement a Coin class with the specified characteristics. Run a simulation with 20 coin tosses as described and report the information requested in the problem. 2) For the second part of your...
implement coinclass and driver program within one source file,
i.e. do not separate class specifications with implementation
Its a c++ problem
12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following member variable: • A string named sideUp. The sideUp member variable will hold either "heads" or "tails" indicating the side of the coin that is facing up. The Coin class should have the following member functions: • A default constructor that randomly determines...
# JAVA Problem Toss Simulator Create a coin toss simulation program. The simulation program should toss coin randomly and track the count of heads or tails. You need to write a program that can perform following operations: a. Toss a coin randomly. b. Track the count of heads or tails. c. Display the results. Design and Test Let's decide what classes, methods and variables will be required in this task and their significance: Write a class called Coin. The Coin...
Please write this C# code- Your main program will use a class named Coin, which has the following field: • A private 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 has 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...
Using c++ Write a function named coinToss that simulates tossing
a coin. When you call the function it should generate and return a
random number in the range 1 through 2. 1 represents heads and
2 represents tails.
Exercise 1 (40 points) Write a function named coinToss that simulates tossing a coin. When you call the function it should generate and return a random number in the range 1 through 2.1 represents heads and 2 represents tails Use the function...
Write a C++ program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. The program should toss a coin 100 times. Count the number of times each side of the coin appears and print the results at the end of the 100 tosses. The program should have the following functions as a minimum: void toss() - called from main() and will randomly toss the coin and set a variable equal to the...
Java programming language! 1. Tossing Coins for a Dollar For this assignment you will create a game program using the Coin class from Programming Challenge 12. The program should have three instances of the Coin class: one representing a quarter, one representing a dime, and one representing a nickel. When the game begins, your starting balance is $0. During each round of the game, the program will toss the simulated coins. When a coin is tossed, the value of the...
Write a program that simulates the toss of a coin. Whenever a coin is tossed the result will be either a head or tail. Prompt the user as shown by entering an ‘H’ for heads or ‘T’ for tails. Use a loop for input validation of an ‘h’ or ‘t’. Make sure that both an upper case and lower case will be accepted. Have the computer generate a random number. Assign a char variable a (‘h’ ^ ’t’) head or...
Tosssing Coins for a Dollar (C++) For this assigment, you will create a game program using the Coin class from Programming Challenge 12 (Coin Toss Simuator). The prgram should have three instances of the Coin class: one representing a quarter, one representing a dime, and one representing a nickel. When the game begins, your starting balance is $0. During each round of the game, the program will toss the simulated coins. When a coin is tossed, the value of the...
In C++ program Fishing Game Simulation For this assignment, you will write a program that simulates a fishing game. In this game, a six-sided die is rolled to determine what the user has caught. Each possible item is worth a certain number of fishing points. The points will not be displayed until the user has finished fishing, and then a message is displayed congratulating the user depending on the number of fishing points gained. Here are some suggestions for the...