Question

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 coin is added to your balance if it lands heads-up. For example, if the quarter lands heads-up, 25 cents is added to your balance. Nothing is added to your balance for coins that land tails-up. The game is over when your balance reaches one dollar or more. If your balance is exactly one dollar, you win the game. You lose if your balance exceeds one dollar.  

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

Here is the completed code for Coin.java and Simulation.java. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Coin.java (since you did not provide it, I created one myself)

// representing a coin with two sides - heads and tails

public class Coin {

    private String sideUp;

    public Coin() {

        toss();

    }

   

    //method to toss the coin and update sideUp

    public void toss() {

        int randNum = (int) (Math.random() * 2);

        if (randNum == 1) {

             sideUp = "heads";

        } else if (randNum == 0) {

             sideUp = "tails";

        }

    }

   

    //returns the sideUp

    public String getSideUp() {

        return sideUp;

    }

}

// Simulation.java

public class Simulation {

    public static void main(String[] args) {

        // winning score

        double winningScore = 1.00;

        // three coins for representing a quarter, dime and nickel

        Coin quarter = new Coin();

        Coin dime = new Coin();

        Coin nickel = new Coin();

        // starting balance

        double balance = 0;

        // round number

        int round = 1;

        // looping until balance is not less than winningScore

        while (balance < winningScore) {

             // tossing coins

            quarter.toss();

             dime.toss();

             nickel.toss();

             // displaying coins and their sideUp

             System.out.println("Quarter: " + quarter.getSideUp() + ", Dime: "

                     + dime.getSideUp() + ", Nickel: " + nickel.getSideUp());

             // if quarter is "heads", adding 0.25 to balance

             if (quarter.getSideUp().equalsIgnoreCase("heads")) {

                 balance += 0.25;

             }

             // if dime is "heads", adding 0.10 to balance

             if (dime.getSideUp().equalsIgnoreCase("heads")) {

                 balance += 0.10;

             }

             // if nickel is "heads", adding 0.05 to balance

             if (nickel.getSideUp().equalsIgnoreCase("heads")) {

                 balance += 0.05;

             }

             // displaying balance after current round

             System.out.printf("Balance after round#" + round + ": $%.2f\n",

                     balance);

             // updating round

             round++;

        }

        // after the loop, if balance is equal to winningScore, player won, else

        // lost

        if (balance == winningScore) {

             System.out.println("You won!");

        } else {

             System.out.println("You lost!");

        }

    }

}

/*OUTPUT1*/

Quarter: heads, Dime: heads, Nickel: heads

Balance after round#1: $0.40

Quarter: heads, Dime: tails, Nickel: heads

Balance after round#2: $0.70

Quarter: heads, Dime: tails, Nickel: heads

Balance after round#3: $1.00

You won!

/*OUTPUT2*/

Quarter: tails, Dime: heads, Nickel: heads

Balance after round#1: $0.15

Quarter: heads, Dime: tails, Nickel: heads

Balance after round#2: $0.45

Quarter: tails, Dime: heads, Nickel: tails

Balance after round#3: $0.55

Quarter: tails, Dime: tails, Nickel: tails

Balance after round#4: $0.55

Quarter: tails, Dime: tails, Nickel: tails

Balance after round#5: $0.55

Quarter: heads, Dime: heads, Nickel: heads

Balance after round#6: $0.95

Quarter: heads, Dime: heads, Nickel: heads

Balance after round#7: $1.35

You lost!

Add a comment
Know the answer?
Add Answer to:
Java programming language! 1. Tossing Coins for a Dollar For this assignment you will create a...
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
  • 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...

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

  • 12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field ...

    12. 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-arg constructor that randomly determines the side of the coin that is facing up (heads" or "tails") and initializes the aideUp field accordingly .A void method named toss that simulates the...

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

  • A box contains five coins. For each coin there is a different probability that a head...

    A box contains five coins. For each coin there is a different probability that a head will be obtained when the coin is tossed. (Some of the coins are not fair coins!) Let pi denote the probability of a head when the i th coin is tossed (i = 1, . . . , 5), and suppose that p1 = 0, p2 =1/4, p3 =1/2, p4 =3/4, p5 =1. The experiment we are interested in consists in selecting at random...

  • Suppose you have a six sided die. One face is printed with the number 1.  Two faces...

    Suppose you have a six sided die. One face is printed with the number 1.  Two faces are printed with the number 2. Three faces are printed with the number 3. You also have 3 coins:  C_1, C_2, and C_3. C_1 will land Heads with probability 1/5. C_2 will land Heads with probability 1/3. C_3 will land Heads with probability 1/2. You roll the die. If the die lands with a 1 face up, flip coin C_1 If the die lands with...

  • 3. Suppose my friend and I are tossing a biased coin (the chance of the coin...

    3. Suppose my friend and I are tossing a biased coin (the chance of the coin landing heads is 0.48). I get one dollar each time the coin lands heads, and I have to pay one dollar to my friend each time it lands tails. I will stop playing if my net gain is three dollars. (a)What is the chance that I will stop after exactly three tosses? (b) What is the chance that I will stop after exactly four...

  • Suppose you have a six sided die. One face is printed with the number 1. Two...

    Suppose you have a six sided die. One face is printed with the number 1. Two faces are printed with the number 2. Three faces are printed with the number 3. You also have 3 coins: C_1, C_2, and C_3. C_1 will land Heads with probability 1/3. C_2 will land Heads with probability 1/5. C_3 will land Heads with probability 1/4. You roll the die. If the die lands with a 1 face up, flip coin C_1 If the die...

  • Suppose you have a six sided die. One face is printed with the number 1. Two...

    Suppose you have a six sided die. One face is printed with the number 1. Two faces are printed with the number 2. Three faces are printed with the number 3. You also have 3 coins: C_1, C_2, and C_3. C_1 will land Heads with probability 1/5. C_2 will land Heads with probability 1/3. C_3 will land Heads with probability 1/2. You roll the die. If the die lands with a 1 face up, flip coin C_1 If the die...

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