Question

Write JunitTest for the class below: public class Dice {    private Die die1;    private Die die2;    public Dice(int[] programmedRolls)    {        int[] programmableroll = programmedRolls;        th...

Write JunitTest for the class below:

public class Dice

{

   private Die die1;
   private Die die2;

   public Dice(int[] programmedRolls)
   {
       int[] programmableroll = programmedRolls;
       this.die1 = new Die(programmableroll);
       this.die1 = new Die(programmableroll);
   }

}

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


Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)

import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class DiceTest

{
    private Dice dice;
    private Die die1, die2;
    private int[] rollDie1, rollDie2;

    @Before
    public void setUp() throws Exception
    {
        this.rollDie1 = new int[] { 1, 2, 3 };
        this.die1 = new Die(rollDie1);

        this.rollDie2 = new int[] { 1, 2, 3 };
        this.die2 = new Die(rollDie2);

        this.dice = new Dice(die1, die2);
    }


    @Test
    public void test_Init_PredictableDie()
    {
        dice.roll();
        int value = dice.getLastRoll();
        assertEquals(2, value);
    }

    @Test
    public void test_Predictable_Roll_2()
    {
        dice.roll();
        dice.roll();
        int value = dice.getLastRoll();
        assertEquals(4, value);
    }

    @Test
    public void test_Predictable_Roll_3()
    {
        dice.roll();
        dice.roll();
        dice.roll();
        int value = dice.getLastRoll();
        assertEquals(6, value);
    }

    @Test
    public void test_toString()
    {
        dice.toString();
        assertEquals("Dice with last roll:   => + ", "Dice with last roll:   => + ");
    }

}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class Dice

{
    private Die die1;
    private Die die2;

    public Dice()
    {
    
        this.die1 = new Die();
        this.die2 = new Die();
    }

    public Dice(int[] programmedRolls)
    {
        int[] programmableroll = programmedRolls;
        this.die1 = new Die(programmableroll);
        this.die1 = new Die(programmableroll);

    }

    public Dice(Die die1, Die die2)
    {
        this.die1 = die1;
        this.die2 = die2;
    }

    public void roll() {
        die1.roll();
        die2.roll();
    }

    public int getDie1Value() {
        return die1.getLastRoll();
    }


    public int getDie2Value() {
        return die2.getLastRoll();
    }


    public int getDiceValue()
    {
        return die1.getLastRoll() + die2.getLastRoll();
    }

    public int getLastRoll() {
        return die1.getLastRoll() + die2.getLastRoll();
    }

    public int getDie2() {
        return 0;
    }

    public int getDie1() {
        return 0;
    }

}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Die
{
    private int lastRoll;

    private int[] programmedRoll;
    private boolean isItARandomRoll;
    private int arrayIndex;

    public Die()
    {
        this.isItARandomRoll = true;
      
    }


    public Die(int[] runTimeArgForProgrammedRollArray)
    {

        if (runTimeArgForProgrammedRollArray == null)
        {
            throw new NullPointerException("Empty Array Initialized");
        } else
        {
            this.isItARandomRoll = false;
            this.programmedRoll = runTimeArgForProgrammedRollArray;
            this.arrayIndex = 0;

        }
    }

    public int getLastRoll()
    {
        return this.lastRoll;
    }

    public void roll()
    {
        if (isItARandomRoll == true)
        {
            this.lastRoll = (int) (Math.random() * 40 + 1);
        }
        else
        {

            this.lastRoll = this.programmedRoll[arrayIndex];

            arrayIndex++;

            if (arrayIndex >= this.programmedRoll.length)
            {
                arrayIndex = 0;
            }
        }


    }

    @Override
    public String toString()
    {
        return "Die: " + this.getLastRoll();
    }

}



DiceTest [-/IdeaProjects/DiceTest]-.../src/DiceTest.java [DiceTest] Project import static org.junit.Assert.assertEquals; impo

Add a comment
Know the answer?
Add Answer to:
Write JunitTest for the class below: public class Dice {    private Die die1;    private Die die2;    public Dice(int[] programmedRolls)    {        int[] programmableroll = programmedRolls;        th...
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
  • Create Junit tests for the following classes public class Dice {    private Die die1;    private Die die2;    public Dice()    {        this.die1 = new Die();        this.die2 = new Die();    }    pu...

    Create Junit tests for the following classes public class Dice {    private Die die1;    private Die die2;    public Dice()    {        this.die1 = new Die();        this.die2 = new Die();    }    public Dice(int[] programmedRolls)    {        int[] programmableroll = programmedRolls;        this.die1 = new Die(programmableroll);        this.die1 = new Die(programmableroll);           }       public Dice(Die die1, Die die2)    {        this.die1 = die1;        this.die2 = die2;    }       public void roll() {        die1.roll();        die2.roll();    }       public int getDie1Value() {        return die1.getLastRoll();    }       public int getDie2Value() {        return die2.getLastRoll();    }      ...

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

  • Hello, Could you please help me code this program in Java? It is important that all...

    Hello, Could you please help me code this program in Java? It is important that all rules are carefully followed. Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a...

  • I am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • hey, struggling with this assignment, wondering if if I could get some help. Here is the...

    hey, struggling with this assignment, wondering if if I could get some help. Here is the project details The following lists a Dice class that simulates rolling a die with a different number of sides. The default is a standard die with six sides. The rollTwoDice function simulates rolling two dice objects and returns the sum of their values. The srand function requires including cstdlib. my class ------------------------------------ class Dice { public: Dice(); DIce(int numSides); virtual int rollDice()const; protected: int...

  • I need help with these Java programming assignements. public class Die { //here you declare your...

    I need help with these Java programming assignements. public class Die { //here you declare your attributes private int faceValue; //operations //constructor - public Die() { //body of constructor faceValue=(int)(Math.random()*6)+1;//instead of 1, use random approach to generate it } //roll operation public void roll() { faceValue=(int)(Math.random()*6)+1; }    //add a getter method public int getFaceValue() { return faceValue; }    //add a setter method public void setFaceValue(int value) { faceValue=value; } //add a toString() method public String toString() { String...

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

  • Adapt your Rational class : public class Rational { private int num; private int denom; public...

    Adapt your Rational class : public class Rational { private int num; private int denom; public Rational() { num = 0; denom = 1; } public Rational(int num, int denom) { this.num = num; this.denom = denom; } int getNum() { return num; } int getDenom() { return denom; } public Rational add(Rational rhs) { return new Rational(num * rhs.denom + rhs.num * denom, denom * rhs.denom); } public Rational subtract(Rational rhs) { return new Rational(num * rhs.denom - rhs.num...

  • package rectangle; public class Rectangle {    private int height;    private int width;    public...

    package rectangle; public class Rectangle {    private int height;    private int width;    public Rectangle(int aHeight, int aWidth) {    super();    height = aHeight;    width = aWidth;    }    public int getHeight() {    return height;    }    public int getWidth() {    return width;    }    public void setHeight(int aHeight) {    height = aHeight;    }    public void setWidth(int aWidth) {    width = aWidth;    }    public int...

  • Examine the following class definition: public class Date private int year; private int month; private int...

    Examine the following class definition: public class Date private int year; private int month; private int day; public Date() { ...) public void set (int x, int y, int z) { ...) public int getYear() { ...) // returns year public int getMonth() { } // returns month public int get Day () { ...) // returns day //more methods here -- 1 Which of the following statements in a client program correctly prints out the day of the object...

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