Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can anyone help me fix this code with these directions: In this case, we want to answer the question Which strategy is better, switching or

Can anyone help me fix this code with these directions:

In this case, we want to answer the question "Which strategy is better, switching or not switching". So, you could run the program twice, one with switching and one with not switching. Compare the probability of winning and determine which one is better. In Monte Hall paradox, the probability of winning for switching is around 67%, and the probability of winning for not switching is around 33%.

Can someone help me fix my output so it says winning for switching is: around 67% and winning without switching is around 33%

3 4 5 import java.util.*; 6 7 class Door { 8 boolean open; 9 boolean hasGrandPrize; 10 boolean chosenByContestant; 11 } 12 13 class Game { 14 Door door1 = new Door(), door2 = new Door(), door3 = new Door(); 15 16 static Random r = new Random(); 17 18 19 void setUpGame() { 20 int grandPrizeDoor = r.nextInt(3); 21 switch (grandPrizeDoor) { 22 case 0: 23 door1.hasGrandPrize = true; break; 24 case 1: 25 door2.hasGrandPrize = true; break; 26 case 2: 27 door3.hasGrandPrize = true; break; 28 } 29 } 30 31 void contestantChooseDoor() { 32 int contestantDoor = r.nextInt(3); 33 switch (contestantDoor) { 34 case 0: 35 door1.chosenByContestant = true; 36 door2.chosenByContestant = false; 37 door3.chosenByContestant = false; 38 break; 39 case 1: 40 door1.chosenByContestant = false; 41 door2.chosenByContestant = true; 42 door3.chosenByContestant = false; 43 break; 44 case 2: 45 door1.chosenByContestant = false; 46 door2.chosenByContestant = false; 47 door3.chosenByContestant = true; 48 break; 49 } 50 } 51 52 53 boolean ifUserWon() { 54 if ((door1.hasGrandPrize && door1.chosenByContestant) || (door2.hasGrandPrize && door2.chosenByContestant) 55 || (door3.hasGrandPrize && door3.chosenByContestant)) { 56 return true; 57 } 58 return false; 59 } 60 61 62 void printStateOfDoors() { 63 System.out.println("Door 1 is " + (door1.open ? " open, " : "not open, ") 64 + (door1.hasGrandPrize ? "is the grand prize, and " : "is not the grand prize, and ") 65 + (door1.chosenByContestant ? "is chosen." : "is not chosen.")); 66 System.out.println("Door 2 is " + (door2.open ? " open, " : "not open, ") 67 + (door2.hasGrandPrize ? "is the grand prize, and " : "is not the grand prize, and ") 68 + (door2.chosenByContestant ? "is chosen." : "is not chosen.")); 69 System.out.println("Door 3 is " + (door3.open ? " open, " : "not open, ") 70 + (door3.hasGrandPrize ? "is the grand prize, and " : "is not the grand prize, and ") 71 + (door3.chosenByContestant ? "is chosen." : "is not chosen. ")); 72 } 73 } 74 75 public class PlayManyGames { 76 77 public static void main(String[] args) { 78 int winner = 0; 79 80 boolean alwaysSwitch = false; 81 82 Scanner sc = new Scanner(System.in); 83 System.out.print("Would you like to always switch? 1.Yes or 2.No: "); 84 int decision = sc.nextInt(); 85 sc.close(); 86 87 Game theGame; 88 89 for (int i = 0; i < 10000; i++) { 90 theGame = new Game(); 91 theGame.setUpGame(); 92 93 if (decision == 1) 94 alwaysSwitch = true; 95 96 theGame.contestantChooseDoor(); 97 if (alwaysSwitch) 98 theGame.contestantChooseDoor(); 99 100 if (theGame.ifUserWon()) 101 winner++; 102 } 103 104 System.out.println("Total time won: " + winner); 105 } 106 }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Fundamentals Study Guide

Authors: Dr. Sergio Pisano

1st Edition

B09K1WW84J, 979-8985115307

More Books

Students also viewed these Databases questions

Question

6.3 Explain the importance of application forms.

Answered: 1 week ago