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.
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) { ...
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()...
Write one JUnit test in Java for the following: Essay class: import java.util.Scanner; public class Essay implements IAnswer{ private String question; public Essay(String q){ this.question = q; } //This function returns question text public String getQuestionText() { return question; } //This function takes answer from user public void answer(String userAnswer) { // Take care of answer } @Override public String getAnswer() { System.out.println(question); Scanner scan = new Scanner(System.in); System.out.print("Answer: "); String ans =scan.nextLine(); scan.close(); if(ans.length() <=140){ return ans; }else{ return...
Read through the code of the class Player, noting it has two instance variables, name and rank, which are of type String and three further instance variables won, drew and lost which are of type int. There is also an attribute of the class, points, which does not have a corresponding instance variable. Also note the constructor and methods of the class and what they do. TournamentAdmin class code: public class RankAdmin { /** * Constructor for objects of class...