Question

1. Game simulation, Java programming. In the game show Let’s Make a Deal, a contestant is...

1. Game simulation, Java programming. In the game show Let’s Make a Deal, a contestant is presented with three doors. Behind one of them is a valuable prize. After the contestant chooses a door, the host opens one of the other two doors (never revealing the prize, of course). The contestant is then given the opportunity to switch to the other unopened door. Should the contestant do so? Intuitively, it might seem that the contestant’s initial choice door and the other unopened door are equally likely to contain the prize, so there would be no incentive to switch.

Write a program MonteHall to test this intuition by simulation. Your program should take a command-line argument n, play the game n times using each of the two strategies (switch or do not switch), and print the chance of success for each of the two strategies.

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

If you have any problem with the code feel free to comment.

Program

import java.text.DecimalFormat;
import java.util.Arrays;

class MonteHall {
   // arraay represent gates
   private String[] gate = new String[3];
   private int priceGate;

   private void init() {
       // fill array with loss
       Arrays.fill(gate, "Loss");
       priceGate = (int) (Math.random() * 3);// get the win gate
       gate[priceGate] = "Win";// put the win gate in the array
   }

   // when no switching gate happens
   public boolean noSwitch() {
       init();
       // get user choice
       int userChoice = (int) (Math.random() * 3);
       // since opening the bluff gate doesn't impact user choice
       // thats why haven't used that method here
       if (gate[userChoice].equals("Win"))
           return true;
       else
           return false;
   }

   // when switching gate occurs
   public boolean switchGate() {
       init();
       int userChoice = (int) (Math.random() * 3);
       int openIndex = openGate(userChoice);

       // switcing the gate
       int sw = (int) (Math.random() * 2);
       if (openIndex == 0)
           userChoice = (sw == 0) ? 1 : 2;
       else if (openIndex == 1)
           userChoice = (sw == 0) ? 0 : 2;
       else
           userChoice = (sw == 0) ? 0 : 1;

       if (gate[userChoice].equals("Win"))
           return true;
       else
           return false;

   }

   private int openGate(int userChoice) {
       // opening the bluff gate
       if (priceGate == 0) {
           return (userChoice == 0 || userChoice == 2) ? 1 : 2;
       } else if (priceGate == 1) {
           return (userChoice == 1 || userChoice == 2) ? 0 : 2;
       } else
           return (userChoice == 0 || userChoice == 2) ? 1 : 0;
   }

}

public class Test {// driver class
   public static void main(String[] args) {

       // if you want to use commandline input use the commented line given below
       int n = 1000;// Integer.parseInt(args[0])

       float sum = 0, percentage;

       DecimalFormat df = new DecimalFormat("##.##");// for formating percentage to 2 decimal places

       MonteHall obj1 = new MonteHall();
       MonteHall obj2 = new MonteHall();

       // no switch
       for (int i = 0; i < n; i++) {
           if (obj1.noSwitch())
               sum++;
       }
       percentage = (sum / n) * 100;
       System.out.println("Sucess rate of no switch: " + df.format(percentage) + "%");

       sum = 0;

       // switching
       for (int i = 0; i < n; i++) {
           if (obj2.switchGate())
               sum++;
       }
       percentage = (sum / n) * 100;
       System.out.println("Sucess rate of switching gate: " + df.format(percentage) + "%");

   }
}

Output

Add a comment
Know the answer?
Add Answer to:
1. Game simulation, Java programming. In the game show Let’s Make a Deal, a contestant is...
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
  • A debate recently erupted about the optimal strategy for playing a game on the TV show...

    A debate recently erupted about the optimal strategy for playing a game on the TV show called "Let's Make a Deal." In one of the games on this show, the contestant would be given the choice of prizes behind three closed doors. A valuable prize was behind one door and worthless prizes were behind the other two doors. After the contestant selected a door, the host would open one of the two remaining doors to reveal one of the worthless...

  • please help in java Monty Hall was a television game show host years ago. The contestant...

    please help in java Monty Hall was a television game show host years ago. The contestant would have 3 doors to choose from. There was always a prize behind one door and goats behind the other two doors. The contestant would choose a door. Monty Hall would then open a door following these rules: Not a door the contestant chose and not the winning door. Monty ALWAYS showed a goat. He'd then ask the contestant if they wanted to change...

  • Monty Hall Problem - Suppose you are going to be on a game show, and you...

    Monty Hall Problem - Suppose you are going to be on a game show, and you will be given the choice of three doors: Behind one door is a car; behind the other two doors are goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your...

  • 5. Consider the Monty Hall Problem. A game show host shows you three doors, and indicates...

    5. Consider the Monty Hall Problem. A game show host shows you three doors, and indicates that one of them has a car behind it, while the other two have goats. You win a car if you end up choosing a door with a car behind it. The game is conducted as follows: • You pick an initial door out of the three available. • The game show hosts then opens a door (out of the remaining two doors) with...

  • Consider the Monty Hall problem. Let’s label the door with the car behind it a and...

    Consider the Monty Hall problem. Let’s label the door with the car behind it a and the other two doors b and c. In the game the contestant chooses a door and then Monty chooses a door, so we can label each outcome as ‘contestant followed by Monty’, for example ab means the contestant chose a and Monty chose b. (a) Make a 3 × 3 probability table showing probabilities for all possible outcomes. (3 marks) (b) Make a probability...

  • You have to solve the 7th question Exercises p one of the two doors not selected...

    You have to solve the 7th question Exercises p one of the two doors not selected by the contestant. In opening up a oor, a rule of the show is that Monty is prohibited from opening the door ith the good prize. After Monty opens a door, the contestant is then given he opportunity to continue with the door originally selected or switch to he other unopened door. After the contestant's decision, the remaining two doors are opened. a. Write...

  • A contestant on a game show is offered a choice of 4 boxes. one of the...

    A contestant on a game show is offered a choice of 4 boxes. one of the boxes contains a diamond and the other three boxes each contain a pebble. tThe contestant cannot see into the boxes and so does not know which box contains the diamond. First the ontestant selexts a box. Host opens it ait to reveal a pebble. ( The host know the boxes and choose normaly between boxes containing pebble.). The host then ask the contestant if...

  • Probability puzzle 2: The Game Show Paradox Discussions List View Topic Settini Probability Puzzle 2: The...

    Probability puzzle 2: The Game Show Paradox Discussions List View Topic Settini Probability Puzzle 2: The Game Show Paradox Subscribe Let's say you are a contestant on a game show. The host of the show presents you with a choice of three doors, which we will call doors 1. 2. nd 3. You do not know what is behind each door, but you do know that behind two of the doors are beat up 1987 Hyundai Excels, and behind one...

  • 1.3 Cars and goats: the Monty Hall dilemma On Sunday September 9, 1990, the following question...

    1.3 Cars and goats: the Monty Hall dilemma On Sunday September 9, 1990, the following question appeared in the "Ask Marilyn" column in Parade, a Sunday supplement to many newspapers across the United States: Suppose you're on a game show, and you're given the choice of three doors; behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3,...

  • Question 1: Consider the following Monty Hall problem. Suppose you are on a game show, and...

    Question 1: Consider the following Monty Hall problem. Suppose you are on a game show, and you are given the choice of three doors. Behind one door is a car, behind the others, goats. You pick a door, say #1, and the host, who knows what is behind the doors, opens another door, say #3, which has a goat. Here we assume that the host cannot open the door to expose the car and when he can open either of...

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