Background information The Monty Hall problem has a long and interesting history
ID: 3664750 • Letter: B
Question
Background information The Monty Hall problem has a long and interesting history. The version we are going to use is as follows: we have three doors. Behind one of these doors is a prize, and there is nothing behind the other two doors. The player selects one of the doors and walks up to it. At this point, the game host, who knows where the prize is, stops the player and opens one of two doors that were not chosen by the player. The prize is not behind the open door. We now have two closed doors: the one selected by the player and one other one. The game host gives the player the opportunity to chose the second closed door instead of the door that was initially selected. The question is the following: Should the player switch door? It should be noted that the game host’s routine is part of the show, it is done every time regardless of the initial choice of door by the player. In addition, the host knows which door has the prize, and thus can always safely open another (empty) door. In other words, the host routine is not an indication that the player chose the good or the bad door. Problem For this assignment, you will simulate playing the Monty Hall game. Your simulation will be a fair representation of the actual game, with the probabilities of each situation respected. You will simulate both strategies: player changing door and player sticking to the original choice. By simulating a large number of games, you will see which of the two strategies is more interesting. Implementation The assignment has three classes, Door, MontyHall and Statistics. The class MontyHall will use the class Door and simulate a game. It will use the class Statistics to keep track of the results as it iterates through several games. As a first step, you will simply simulate one game when executing your program. You only need the classes Door and MontyHall for now. One object of the class Door stores the following information about one of the door: 1 • Does it have the prize behind it? • Is it open or closed? • Was it selected by the player? A detailed description of the class can be found here: • JavaDoc Documentation • Door.java The class MontyHall implements the game’s logic. It has a main method which simulates one Monty Hall game and prints the result. Here are two sample runs: > java MontyHall The prize was behind door C The player selected door C The host opened door B Switching strategy would have lost > java MontyHall The prize was behind door A The player selected door C The host opened door B Switching strategy would have won A detailed description of the class can be found here: • JavaDoc Documentation • MontyHall.java Implement both classes and make sure that your program works correctly.
Explanation / Answer
Door.java
package com.chegg.test;
/**
* Class to implement MontyHall game that holds door properties
*
* @author your Name
*
*/
public class Door {
private boolean doorOpened;
private boolean priceExist;
private boolean selectedByPlayer;
Door(boolean price, boolean open, boolean selected) {
this.doorOpened = open;
this.priceExist = price;
this.selectedByPlayer = selected;
}
/**
* @return the doorOpened
*/
public boolean isDoorOpened() {
return doorOpened;
}
/**
* @param doorOpened
* the doorOpened to set
*/
public void setDoorOpened(boolean doorOpened) {
this.doorOpened = doorOpened;
}
/**
* @return the priceExist
*/
public boolean isPriceExist() {
return priceExist;
}
/**
* @param priceExist
* the priceExist to set
*/
public void setPriceExist(boolean priceExist) {
this.priceExist = priceExist;
}
/**
* @return the selectedByPlayer
*/
public boolean isSelectedByPlayer() {
return selectedByPlayer;
}
/**
* @param selectedByPlayer
* the selectedByPlayer to set
*/
public void setSelectedByPlayer(boolean selectedByPlayer) {
this.selectedByPlayer = selectedByPlayer;
}
}
MontyHall.java
package com.chegg.test;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* Class to implement MontyHall game
*
* @author yourname
*
*/
public class MontyHall {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// As mentioned host nows the info about the doors.
Door doorA = new Door(true, false, false);
Door doorB = new Door(false, false, false);
Door doorC = new Door(false, false, false);
System.out.println("1.Door A 2.Door B 3.Door C");
System.out.println("Please select the Door:");
try {
char choice = sc.next().charAt(0);
char secondChoice = 0;
switch (choice) {
case 'A':
case 'a':
if (doorA.isPriceExist()) // The prize is behind this door
{
// The prize was behind door A The player selected door A
// The host opened door B Switching strategy would have lost
System.out.println("Should the player switch door?");
secondChoice = sc.next().charAt(0);
doorB.setDoorOpened(true);
if (secondChoice == 'y' || secondChoice == 'Y')
System.out.println("You Lost:");
break;
}
case 'B':
case 'b':
// The player selects the second door
if (!doorB.isPriceExist())// The prize is not behind the open
// door
{
// opens one of two doors that were not chosen by the player
doorC.setDoorOpened(true);
// Selected by the player
doorB.setSelectedByPlayer(true);
System.out.println("Should the player switch door?");
secondChoice = sc.next().charAt(0);
if (secondChoice == 'y' || secondChoice == 'Y')
System.out.println("You Won:");
break;
}
case 'C':
case 'c':
// The player selects the third door
if (!doorC.isPriceExist())// The prize is not behind the open
// door
{
// opens one of two doors that were not chosen by the player
doorB.setDoorOpened(true);
// Selected by the player
doorC.setSelectedByPlayer(true);
System.out.println("Should the player switch door?");
secondChoice = sc.next().charAt(0);
if (secondChoice == 'y' || secondChoice == 'Y')
System.out.println("You Won:");
break;
}
default:
System.out.println("Please enter the right choice");
}
sc.close();// Closing the scanner
} catch (InputMismatchException ime) {
System.out.println("Enter choice in numeric format only.");
sc.close();// Closing the scanner
}
}
}
Output:
Scenario:The prize was behind door A The player selected door C The host opened door B Switching strategy would have won
1.Door A
2.Door B
3.Door C
Please select the Door:
c
Should the player switch door?
y
You Won:
Scenario 2:The prize was behind door A The player selected door A The host opened door B Switching strategy would have lost
1.Door A
2.Door B
3.Door C
Please select the Door:
A
Should the player switch door?
y
You Lost:
Note:Please comment if you have any doubts.Please feel free to ask.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.