Question

12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field .A String named sideUp. The

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

C:\Program Files\Java\jdk-11.0.1lbin\java.exe Coin created with tails as side up. Toss #1, Side up heads. Toss #2, Side up

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:

C:\Program Files Java\jdk-11.0.1binljava.exe-j Round #1: Quarter: Dime : Nickel: Balance: heads tails heads 0.3 Round #2: Q

Add a comment
Know the answer?
Add Answer to:
12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ Program 2: Coin Toss Simulator For this program, please implement Problems 12 and 13 in...

    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...

    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...

    # 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...

  • Using c++ Write a function named coinToss that simulates tossing a coin. When you call the...

    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...

    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...

    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...

    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...

  • 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. Eac...

    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...

  • import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25);...

    import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25); Coin dime = new Coin(10); Coin nickel = new Coin(5);    Scanner keyboard = new Scanner(System.in);    int i = 0; int total = 0;    while(true){    i++; System.out.println("Round " + i + ": "); quarter.toss(); System.out.println("Quarter is " + quarter.getSideUp()); if(quarter.getSideUp() == "HEADS") total = total + quarter.getValue();    dime.toss(); System.out.println("Dime is " + dime.getSideUp()); if(dime.getSideUp() == "HEADS") total = total +...

  • Problem: Write a function named coinToss that simulates the tossing of a coin. When you call the function, it should ge...

    Problem: Write a function named coinToss that simulates the tossing of a coin. When you call the function, it should generate a random number in the range of 1 through 2. If the random number is 1, the function should display “heads.” If the random number is 2, the function should display “tails.” Demonstrate the function in a program that asks the user how many times the coin should be tossed and then simulates the tossing of the coin that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT