Question about MontyHall in Java. I\'ve done the main part of the program alread
ID: 3664971 • Letter: Q
Question
Question about MontyHall in Java. I've done the main part of the program already, but I dont know what should I do in the statistics class at all.The requirement that how statistics track the function as following:* Number of games played: 5
* Staying strategy won 2 games (40%)
* Switching strategy won 3 games (60%)
* Selected doors:
* door 1: 1 (20%)
* door 2: 3 (60%)
* door 3: 1 (20%)
* Winning doors:
* door 1: 1 (20%)
* door 2: 1 (20%)
* door 3: 3 (60%)
* Open doors:
* door 1: 2 (40%)
* door 2: 2 (40%)
* door 3: 1 (20%)
*
here are my door class and MontyHall class:
/**
* Created by Micheal on 2016/1/31.
*/
/**
* Created by Micheal on 2016/1/31.
*/
import java.util.Random;
/**
* The class <b>Door</b> stores the information about one of the door:
* does it have the prize behind it? Is it open or closed? Was it
* selected by the player?
*
* It provides other objects access to these information through some
* <b>setters</b> and <b>getters</b>.
*
* @author gvj (gvj@eecs.uottawa.ca)
*
*/
public class Door {
private String name;
private boolean isOpen;
private boolean hasPrize;
private boolean isChoosen;
//Door[] doors = new Door[3];
// ADD YOUR INSTANCE VARIABLES HERE
/**
* Creates an instance of the Door object.
* Initially, the door is closed, doesn't have a prize behind it
* and has not been chosen by the player.
*
* @param name identifier for that door
*/
public Door(String name) {
this.name = name;
isOpen = false;
hasPrize = false;
isChoosen = false;
}
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
/**
* Resets the door to its initial state: closed, without a prize behind it
* and not chosen by the player.
*/
public void reset() {
isOpen = false;
hasPrize = false;
isChoosen = false;
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* Sets this door open.
*/
public void open() {
isOpen = true;
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* Checks if the door is open.
*
* @return true if the door is open
*/
public boolean isOpen() {
if (isOpen == true) {
return true;
} else return false;
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* Puts the prize behind this door.
*/
public void setPrize() {
/* Random rand = new Random();
int value = rand.nextInt(3);
doors[value].hasPrize = true;
*/
hasPrize = true;
}
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATIO
/**
* Checks if the door has the prize.
*
* @return true if the door has the prize
*/
public boolean hasPrize() {
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
if (hasPrize == true) {
return true;
} else return false;
}
/**
* Sets this door as selected by the player.
*/
public void choose() {
isChoosen = true;
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* Checks if the door is selected by the player.
*
* @return true if the door is selected by the player
*/
public boolean isChosen() {
if (isChoosen == true) {
return true;
} else return false;
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* @return the door's identifier
*/
public String getName() {
return name;
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
}
/**
* Created by Micheal on 2016/1/31.
*/
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
* The class <b>MontyHall</b> simulates one or several games. Is uses three <b>Door</b> objects
* to simulate the three doors. One game consists of the following steps
* <ol>
* <li>Resets all three doors</li>
* <li>Simulates the selection of one of the doors by the player</li>
* <li>Simulates opening of an empty door by the host</li>
* <li> provide the outcome for switching and not switching door</li>
* </ol>
* @author gvj (gvj@eecs.uottawa.ca)
*
*/
public class MontyHall {
Door[] door=new Door[3];
// ADD YOUR INSTANCE VARIABLES HERE
/**
* Initializes the three doors.
*/
public MontyHall() {
door[0] = new Door("A");
door[1] = new Door("B");
door[2] = new Door("C");
}
public void runGames(int numberOfGames, boolean commandLine){
Statistics stats = new Statistics();
int j=1;
while(j<= numberOfGames){
oneGame();
j++
if(commandLine) {
System.out.println(stats.toString());
}
else {
JOptionPane.showMessageDialog (null,stats.toString(), "Results", JOptionPane.INFORMATION_MESSAGE);
}
}
/**
* Simulates one Monty Hall game.
* <ol>
* <li>Resets all three doors</li>
* <li>Simulates the selection of one of the doors by the player</li>
* <li>Simulates opening of an empty door by the host</li>
* <li>prints the outcome for switching and not switching door to standard output</li>
* </ol>
*/
public void oneGame(){
for(int i = 0; i<door.length;i++){door[i].reset();}
//set prize
Random random=new Random();
int value=random.nextInt(3);
door[value].setPrize();
System.out.println("The prize was behind"+door[value].getName());
//player choose
System.out.println("Please enter a number, 1, 2, or 3: ");
Scanner scan = new Scanner(System.in);
int playerSelection = scan.nextInt()-1;
System.out.println("The player selected"+door[playerSelection].getName());
door[playerSelection].choose();
// host open
for(int i=0;i<door.length;i++){
if(!door[i].hasPrize() && !door[i].isChosen()) {
door[i].open();
System.out.println("The host open door" + door[i].getName());
break;
}
}
if ((door[0].hasPrize() != door[0].isChosen()) || (door[1].hasPrize() != door[1].isChosen()) || (door[2].hasPrize() != door[2].isChosen())) {
System.out.println("Switching strategy would have won");
} else {
System.out.println("Switching strategy would have lost");
}
}
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* Simulates a random selection of one of the three doors.
* @return the door randomly selected
*/
private Door pickADoor(){
return null
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* Simulates the opening of one of the other doors once the player selected one.
* It should open a door chosen randomly among the ones that don't have the prize and
* that are not selected by the player
*
* @param prizeDoor the door that hides the prize
* @param selectedDoor the door that was selected by the player
* @return the door opened
*/
private Door openOtherDoor(Door prizeDoor, Door selectedDoor){
return null
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* The main method of this program. Examples of the execution of the program
* from the command line:
* <pre>
* > java MontyHall 5
*
* Number of games played: 5
* Staying strategy won 2 games (40%)
* Switching strategy won 3 games (60%)
* Selected doors:
* door 1: 1 (20%)
* door 2: 3 (60%)
* door 3: 1 (20%)
* Winning doors:
* door 1: 1 (20%)
* door 2: 1 (20%)
* door 3: 3 (60%)
* Open doors:
* door 1: 2 (40%)
* door 2: 2 (40%)
* door 3: 1 (20%)
*
* @param args (optional) the number of games to simulate
*/
public static void main(String[] args) {
MontyHall montyHall;
int numberOfGames;
boolean commandLine = false;
StudentInfo.display();
if (args.length == 1) {
numberOfGames = Integer.parseInt(args[0]);
commandLine = true;
} else {
numberOfGames = Integer.parseInt(JOptionPane.showInputDialog("Input the number of games to play", "1000"));
}
montyHall = new MontyHall();
montyHall.runGames(numberOfGames, commandLine);
}
}
so What should i do with the statistics class?
**
* Created by Micheal on 2016/1/31.
*/
public class Statistics {
/**
* Initializes the statistics.
*/
public Statistics(){
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* Updates statistics after one game.
* @param door1 the first door in the game
* @param door2 the second door in the game
* @param door3 the third door in the game
*/
public void updateStatistics(Door door1, Door door2, Door door3){
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* Updates statistics for one single door.
* @param door the door for which statistics are updated
* @param index index of that door (0, 1 or 2)
*/
private void oneDoor(Door door, int index){
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
/**
* @return Returns the complete statistics information
*/
public String toString(){
// REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION
}
}
Thank you.I was posting with picture and one guys suggest me to post with text, but i dont know how to repost that question, so i start a new one
Explanation / Answer
This is far better way. Check if helpfull.
import java.io.*;
class Door{
boolean grandPrize;
boolean opened;
void openDoor(){
opened = true;
if(grandPrize){
System.out.println("Grand prize! ");
}
else{
System.out.println("Booby prize. ");
}
}
}
class MakeADeal{
Door[] doorNum;
int doorQty;
int prizeDoor;
int playersDoor;
MakeADeal(int doors){
//make an Array of Doors...
doorQty = doors;
doorNum = new Door[doorQty];
for(int i=0; i<doorQty; i++){
doorNum[i] = new Door();
}
//pick one of the Doors to be the prizeDoor...
prizeDoor = (int)(Math.random() * doorQty);
doorNum[prizeDoor].grandPrize = true;
}
void showBooby(){
//open a Door that isn't already open, is not the player's choice,
//and is not the prizeDoor...
int pick = prizeDoor;
while( (doorNum[pick].opened == true) ||
(pick == (playersDoor-1)) ||
(pick == prizeDoor) ){
pick = (int)(Math.random() * doorQty);
}
System.out.print(" >> Behind door number " + (pick+1) + " is: ");
doorNum[pick].openDoor();
}
}
class Monty{
MakeADeal deal;
Monty(){
int input = 3;
//System.out.println("How many doors are there?");
//input = Integer.parseInt(readInput());
deal = new MakeADeal(input);
montyGo();
}
//utility method to return command line input as String...
String readInput(){
String inputLine = "";
try{
InputStreamReader inStream = new InputStreamReader(System.in);
BufferedReader inBuffer = new BufferedReader(inStream);
inputLine = inBuffer.readLine();
}
catch(IOException e){ System.out.println(e); }
return inputLine;
}
//utility method to println...
void p(String s){
System.out.println(s);
}
//method to ensure a valid Door is selected by user...
boolean validDoor(String s){
int i;
try{
i = Integer.parseInt(s);
if( (i<0) || (i>deal.doorQty) || (deal.doorNum[i-1].opened) ){
throw new Exception();
}
else{
deal.playersDoor = i;
return true;
}
}
catch(Exception e){
//input was not valid (not an int, out of range, or already open)...
p("MONTY: Sorry, you need to pick a closed door, 1 through " + deal.doorQty + ".");
return false;
}
}
//dramatic effect...
void callPause(){
class Pause extends Thread{
public void run(){
try{ sleep(1700); }
catch(InterruptedException e){}
}
}
new Pause().run();
}
//monty's dialogue with user...
void montyGo(){
p(" MONTY: Welcome to Let's Make a Deal!");
callPause();
p("MONTY: Pick a door, 1 through " + deal.doorQty + ".");
System.out.print("YOU: ");
while(!validDoor(readInput())){
System.out.print("YOU: ");
}
p("MONTY: You've picked door number " + deal.playersDoor + ".");
callPause();
p("MONTY: Whatever's behind that door is yours.");
callPause();
p("MONTY: So first let's see what's behind a door you DIDN'T pick...");
callPause();
deal.showBooby();
callPause();
p("MONTY: Now, you own whatever's behind door number " + deal.playersDoor + ".");
callPause();
p("MONTY: And you've just seen what's behind one of the other doors...");
callPause();
p("MONTY: So would you like to change doors now?");
System.out.print("YOU: ");
char c = readInput().charAt(0);
if( (c=='Y') || (c=='y') || (c=='1') ){
p("MONTY: Which door would you like instead?");
System.out.print("YOU: ");
while(!validDoor(readInput())){
System.out.print("YOU: ");
}
}
p("MONTY: Okay, you've got door number " + deal.playersDoor + ".");
callPause();
p("MONTY: Let's see what you've won...");
callPause();
System.out.print(" >> Behind door number " + deal.playersDoor + " is: ");
deal.doorNum[deal.playersDoor-1].openDoor();
callPause();
if(deal.doorNum[deal.playersDoor-1].grandPrize){
p("MONTY: Congratulations!!!");
}
else{
p("MONTY: Oh, I'm so sorry.");
}
}
}
public class MontyHall{
public static void main(String[] args){
new Monty();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.