Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you make a class to pass these tests. Its unit test you have to create a class to pass these tests import org.hamcrest.Matchers; import

Can you make a class to pass these tests.

Its unit test you have to create a class to pass these tests

import org.hamcrest.Matchers; import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.*;

class ComputerPlayerTest { @Test void badConstructor() { badConstructor(null, null, "nm cannot be null"); badConstructor("", null, "nm cannot be blank"); badConstructor(" ", null, "nm cannot be blank"); badConstructor("\t", null, "nm cannot be blank"); badConstructor(" ", null, "nm cannot be blank"); badConstructor(" ", null, "nm cannot be blank"); badConstructor("x", null, "g cannot be null"); badConstructor("X", null, "g cannot be null"); }

private void badConstructor(final String name, final NumberGuesser guesser, final String expectedMessage) { Throwable ex;

ex = assertThrows(IllegalArgumentException.class, () -> { new ComputerPlayer(name, guesser); } ); assertThat(ex.getMessage(), Matchers.equalTo(expectedMessage)); }

@Test void makeSmallestGuess() { final ComputerPlayer player1; final ComputerPlayer player2;

player1 = new ComputerPlayer("P1", new SmallestNumberGuesser()); player2 = new ComputerPlayer("P2", new SmallestNumberGuesser());

assertThat(player1.getName(), equalTo("P1")); assertThat(player2.getName(), equalTo("P2"));

for(int i = 0; i < 100; i++) { assertThat(player1.takeTurn(i, 100), equalTo(i)); assertThat(player2.takeTurn(i + 1, 100), equalTo(i + 1)); } }

@Test void makeLargestGuess() { final ComputerPlayer player1; final ComputerPlayer player2;

player1 = new ComputerPlayer("P1", new LargestNumberGuesser()); player2 = new ComputerPlayer("P2", new LargestNumberGuesser());

assertThat(player1.getName(), equalTo("P1")); assertThat(player2.getName(), equalTo("P2"));

for(int i = 100; i > 0; i--) { assertThat(player1.takeTurn(0, i), equalTo(i)); assertThat(player2.takeTurn(0, i - 1), equalTo(i - 1)); } }

@Test void makeBestGuess() { final ComputerPlayer player1; final ComputerPlayer player2;

player1 = new ComputerPlayer("P1", new BestNumberGuesser()); player2 = new ComputerPlayer("P2", new BestNumberGuesser());

assertThat(player1.getName(), equalTo("P1")); assertThat(player2.getName(), equalTo("P2"));

assertThat(player1.takeTurn(0, 10), equalTo(5)); assertThat(player2.takeTurn(1, 10), equalTo(5)); assertThat(player1.takeTurn(2, 100), equalTo(51)); assertThat(player2.takeTurn(2, 96), equalTo(49)); } }

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

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions

Question

How can you use instant messaging safely on the job?

Answered: 1 week ago

Question

LO10.2 List the conditions required for purely competitive markets.

Answered: 1 week ago