Write Junit test for the following class below:
public class Player {
public int turnScore;
public int roundScore;
public int lastTurnScore;
public String name;
public int chipPile;
public Player (String name) {
this.name = name;
this.turnScore = 0;
this.chipPile = 50;
}
public int getChip() {
return chipPile;
}
public void addChip(int chips) {
chipPile += chips;
}
public int getRoundScore() {
return roundScore;
}
public void setRoundScore(int points) {
roundScore += points;
}
public int getTurnScore() {
return turnScore;
}
public void setTurnScore(int turnScore) {
this.turnScore = turnScore;
}
public boolean checkHundred () {
if (getTurnScore() >= 100)
{
return
true;
}
return false;
}
}
I changed addChips method slightly to check if chips are not negative
public void addChip(int chips) {
if(chips < 0) throw new
IllegalArgumentException();
else
chipPile += chips;
}

import static org.junit.Assert.*;
import org.junit.Assert;
import org.junit.Test;
public class PlayerTest {
@Test
public void testGetChip() {
Player player = new
Player("Jon");
player.addChip(50);
System.out.println(player.getChip());
Assert.assertEquals(100,
player.getChip());
}
@Test
public void testAddChip() {
Player player = new
Player("Jon");
player.addChip(10);
//
System.out.println(player.getChip());
Assert.assertEquals(60,
player.getChip());
}
@Test(expected = IllegalArgumentException.class)
public void testAddChipNegativeValue(){
Player player = new
Player("Jon");
player.addChip(-10);
}
@Test
public void testGetRoundScore() {
Player player = new
Player("Jon");
player.setRoundScore(50);
Assert.assertEquals(50,
player.getRoundScore());
}
@Test
public void testSetRoundScore() {
Player player = new
Player("Jon");
player.setRoundScore(10);
Assert.assertEquals(10,
player.getRoundScore());
}
@Test
public void testGetTurnScore() {
Player player = new
Player("Jon");
player.setTurnScore(10);
Assert.assertEquals(10,
player.getTurnScore());
}
@Test
public void testSetTurnScore() {
Player player = new
Player("Jon");
player.setTurnScore(10);
Assert.assertEquals(10,
player.getTurnScore());
}
@Test
public void testCheckHundred() {
Player player = new
Player("Jon");
player.setTurnScore(10);
Assert.assertEquals(false,
player.checkHundred());
Player player1 = new
Player("Jon1");
player.setTurnScore(110);
System.out.println("**"+player1.checkHundred() +"
"+player1.getTurnScore());
Assert.assertEquals(false,
player1.checkHundred());
}
}
Write Junit test for the following class below: public class Player { public int turnScore; public int roundScore; public int lastTurnScore; public String name; public int chipPile;...
Write Junit test for the following class below: public class Player { public int turnScore; public int roundScore; public int lastTurnScore; public String name; public int chipPile; public Player (String name) { this.name = name; this.turnScore = 0; this.chipPile = 50; } public int getChip() { return chipPile; } public void addChip(int chips) { chipPile...
Write Junit Test for the following class below: public class Turn { private int turnScore; private Dice dice; private boolean isSkunk; private boolean isDoubleSkunk; public Turn() { dice = new Dice(); } public Turn(Dice dice) { this.dice = dice; } public void turnRoll() { dice.roll(); if (dice.getDie1Value() == 1 || dice.getDie2Value() == 1 && dice.getLastRoll() != 2) { turnScore = 0; isSkunk = true; } else if (dice.getLastRoll() == 2) { turnScore = 0; isDoubleSkunk = true; } else { ...
public class Player { private String name; private int health; public Player(String name) { this.name = name; } } Write a complete method using java to find a Player by name in an array of Player objects. Use a linear search algorithm. The method should either return the first Player object with the requested name, or null if no player with that name is found.
This is my playlist class: class Playlist{ private String name; private int numberOfRecordings = 0; private int durationInSeconds = 0; private int MAX_PLAYLIST_SIZE; Recording recordingslist[]; Playlist(){ name = "Unknown"; MAX_PLAYLIST_SIZE = 5; recordingslist = new Recording[MAX_PLAYLIST_SIZE]; durationInSeconds = 0; } Playlist(String name, int size){ this.name = name; this.MAX_PLAYLIST_SIZE = size; recordingslist = new Recording[MAX_PLAYLIST_SIZE]; ...
Assignment (to be done in Java):
Person Class:
public class Person extends Passenger{
private int numOffspring;
public Person() {
this.numOffspring = 0;
}
public Person (int numOffspring) {
this.numOffspring = numOffspring;
}
public Person(String name, int birthYear, double weight, double
height, char gender, int numCarryOn, int numOffspring)
{
super(name, birthYear, weight, height, gender,
numCarryOn);
if(numOffspring < 0) {
this.numOffspring = 0;
}
this.numOffspring = numOffspring;
}
public int getNumOffspring() {
...
How can I make a test case with Junit? import java.util.ArrayList; import board.Board; public class Pawn extends Piece{ public Pawn(int positionX, int positionY, boolean isWhite) { super("P", positionX, positionY, isWhite); } @Override public String getPossibleMoves() { ArrayList<String> possibleMoves = new ArrayList<>(); //check side, different color pawns go on different ways if(isWhite) { if(positionX != 7 && positionY != 0) { ...
You only need to use Junit to test 3 methods of the Nurse class. public Nurse(int a, float s) { ... } public boolean isBusy() {...} public void retire() {....} code: public class Nurse { private int numOfPatients; private float salary; // Helping function // private void trace(char* s) { cout << s << endl; } // Implementor function public Nurse(int a, float s) { numOfPatients =a; salary=s; } // Access function public int getNumOfPatients() { return numOfPatients; } public...
4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...
This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe { private char currentPlayer; private char[][] board; private char winner; /** * Default constructor * Initializes the board to be 3 by 3 and...
Consider the Automobile class: public class Automobile { private String model; private int rating; // a number 1, 2, 3, 4, 5 private boolean isTruck; public Automobile(String model, boolean isTruck) { this.model = model; this.rating = 0; // unrated this.isTruck = isTruck; } public Automobile(String model, int rating, boolean isTruck) { this.model = model; this.rating = rating; this.isTruck = isTruck; } public String getModel()...