Question: Can someone help write a JUnit4 Tester for this die roll class. import java.util.Random; public class Die { public static final int DEFAULT_SIDES = 6;

Can someone help write a JUnit4 Tester for this die roll class.

import java.util.Random;

public class Die { public static final int DEFAULT_SIDES = 6; private static Random ourRandNumGen = new Random(); private final int iMyNumSides; private int iMyResult;

public Die() { this(DEFAULT_SIDES); }

public Die(int numSides) { iMyNumSides = numSides; iMyResult = 1; }

public int roll() { iMyResult = ourRandNumGen.nextInt(iMyNumSides) + 1;

return iMyResult; }

public int getNumSides() { return iMyNumSides; }

public int getResult() { return iMyResult; }

public boolean equals(Object otherObj) { boolean result = true; if (otherObj == null) result = false; else if (this == otherObj) result = true; else if (this.getClass() != otherObj.getClass()) result = false; else { Die otherDie = (Die) otherObj; result = this.iMyResult == otherDie.iMyResult && this.iMyNumSides == otherDie.iMyNumSides; } return result; }

public String toString() { return "Num sides " + getNumSides() + " result " + getResult();

using these test methods:

Can someone help write a JUnit4 Tester for this die roll class.

You will create two different objects: a default and one with 5-sides. A default object is created with the default constructor and the 5-sided die with the one-parameter constructor. The tester files should have the following test cases: Test Method testDefaultRoll test5SideRoll Description Tests the roll of a die with the default sides Tests the roll of a die with 5 sides Tests the getNum Sides of default die Tests the getNumSides of a 5-sided die Tests the result of a default die testDefaultNum Sides test5Num Sides testGetDefaultResult testGet5SideResult Test the result of a 5 sided die testEquals True Test two equal objects testEquals False Test two different (not equal) objects testDefaultToString Test the toString for default die Test the toString for a 5-sided die test5SideToString Example: If you are trying to implement testDefaultRoll: 1 @Test 2 public void testDefaultRoll() { 3 4 }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!