Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

Answer Heres how we can write JUnit tests for the SwitchingPlayer class import orgjunitTest import s... 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_2

Step: 3

blur-text-image_3

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

Introduction To Java Programming And Data Structures Comprehensive Version

Authors: Y. Daniel Liang

12th Edition

0136520235, 978-0136520238

More Books

Students also viewed these Programming questions

Question

Differentiate the function. f(x) = ln(x 2 + 10)

Answered: 1 week ago

Question

=+q a management consulting firm r a breakfast cereal company

Answered: 1 week ago