Question

Write Junit test for the following class below: public class Player {       public int...

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;
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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());

   }

}

Add a comment
Know the answer?
Add Answer to:
Write Junit test for the following class below: public class Player {       public int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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 += chips;    }       public int getRoundScore() {        return roundScore;    }       public void setRoundScore(int points) {        roundScore += points;    }       public int getTurnScore() {        return turnScore;   ...

  • 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()    {...

    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        {           ...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    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...

    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) {           ...

  • public class Player { private String name; private int health; public Player(String name) { this.name =...

    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 code for a TicTacToe game. However, I don't know how to write JUnit...

    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...

  • Write one JUnit test in Java for the following: Essay class: import java.util.Scanner; public class Essay implements IAn...

    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...

  • Consider the Automobile class: public class Automobile {    private String model;    private int rating;...

    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()...

  • Consider the class as partially defined here: //class Pizzaorder class Pizzaorder // static public members public...

    Consider the class as partially defined here: //class Pizzaorder class Pizzaorder // static public members public static final int MAX TOPPINGS 20: // instance members private String toppings[]; private int numToppings; // constructor public PizzaOrder () { numToppings toppings 0; String[MAX TOPPINGS]; new // accessor tells # toppings on pizza, i.e., #toppings in array public int getNum Toppings ()return numToppings; // etc. We want to write an instance method, addTopping) that will take a String parameter, topping, and add it...

  • public enum Rating {               GENERAL(0),        PARENTALGUIDANCE(1),        MATURE(2);     

    public enum Rating {               GENERAL(0),        PARENTALGUIDANCE(1),        MATURE(2);               private int minAge;               private Rating(int i)        {              minAge = i;        }               public int getMinAge()        {              return minAge;        }               public void setMinAge(int age)        {              minAge = age;        }               public String toString()        {              switch(this)              {              case GENERAL:                     return "G";              case PARENTALGUIDANCE:                     return "P";              case MATURE:                     return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT