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 doors, GameshowHost monty) {
    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 doors;
  protected GameshowHost monty;
  protected int selection;

  public Player(List doors, GameshowHost monty) {
    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 doorList;

  public GameshowHost(List doorList) {
    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

1 Expert Approved Answer
Step: 1 Unlock

Answer Heres how we can write JUnit tests for the SwitchingPlayer class import orgjunitTest import s... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!