Question
please use JUnit test for switching player class and explain SwitchPlayer.class public class SwitchingPlayer extends Player { public SwitchingPlayer(List doors, GameshowHost monty) {
please use JUnit test for switching player class and explain
SwitchPlayer.class
public class SwitchingPlayer extends Player {
public SwitchingPlayer(List
super(doors, monty);
}
@Override
public boolean makeSecondSelection() {
for (int i = 0; i < doors.size(); i++) {
Door d = doors.get(i);
if (!d.isOpen() && i != selection) {
selection = i;
return monty.hearSecondChoice(selection);
}
} return false;
}
}
Player.class
abstract public class Player {
protected List
protected GameshowHost monty;
protected int selection;
public Player(List
this.doors = doors;
this.monty = monty;
}
public void makeFirstSelection() {
Random r = new Random();
selection = r.nextInt(3);
monty.hearFirstChoice(selection);
}
// concrete method
// Template Method Pattern
public boolean play() {
makeFirstSelection();
boolean bDidIWin = makeSecondSelection();
return bDidIWin;
}
protected abstract boolean makeSecondSelection();
}
GameshowHost.class
public class GameshowHost {
List
public GameshowHost(List
this.doorList = doorList;
for (Door d: doorList)
d.close();
Collections.shuffle(doorList);
}
public void hearFirstChoice(int selection) {
Random r = new Random();
int reveal;
do {
reveal = r.nextInt(3);
} while (doorList.get(reveal).peek() != Prize.GOAT || reveal == selection);
doorList.get(reveal).open();
}
public boolean hearSecondChoice(int selection) {
if (selection >= 3)
throw new IllegalArgumentException("Player choice must be between 0 and 2");
Door theDoor = doorList.get(selection);
theDoor.open();
return theDoor.look() == Prize.CAR;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Answer Heres how we can write JUnit tests for the SwitchingPlayer class import orgjunitTest import s...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started