Question
We are using the week05 package package week05; import java.util.Random; /** * This class takes a constructor parameter to determine if it should roll or
We are using the week05 package
package week05;
import java.util.Random;
/**
* This class takes a constructor parameter to determine if it should roll or not
*
* @author Scott LaChance
*
*/
public class Die
{
/**
* Constructor
*
* @param roll
* true to roll die, otherwise initialize to NO_NUMBER;
*/
public Die(boolean roll)
{
random = new Random();
if(roll)
{
roll();
}
else
{
number = NO_NUMBER;
}
}
// Rolls the dice
public void roll()
{
number = random.nextInt(MAX_NUMBER - MIN_NUMBER + 1);
}
// Returns the number on this dice
public int getNumber()
{
return number;
}
// Data Members
// the largest number on a dice
private static final int MAX_NUMBER = 6;
// the smallest number on a dice
private static final int MIN_NUMBER = 1;
// to represent a dice that is not yet rolled
private static final int NO_NUMBER = 0;
private int number;
private Random random;
}
package week05;
import java.util.List;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
/**
* This class executes the JUnit Test specified from the command line
* This will be used by the reference system for testing your code.
*
* @author Scott LaChance
*
*/
public class TestHarness
{
public static void main(String[] args)
{
Result result = org.junit.runner.JUnitCore.runClasses(Week05JUnitTest.class);
int failCount = result.getFailureCount();
if( failCount > 0 )
{
List
for(Failure fail : failures)
{
trace("Test Harness FAILED: " + fail.getMessage());
}
}
else
{
trace("SUCCESS");
}
}
private static void trace(String msg)
{
System.out.println(msg);
}
}
Use Die.java, TestHarness.java pasted above
Our objective is to test the Die.java class thoroughly
Create a JUnitTest named Week05JUnitTest.java with the following two tests: testMultipleDie() and testRandomDie()
testMultipleDie() will create multiple Die instances and verify the rolls are unique (that the group doesn't roll to the same value). Use the transitive property to test.
testRandomDie() will run a large number of tests (at least 1000) and check that each facet (1-6) has a value (non-zero) count, calculate the mean and standard deviation, and verify that the counts are evenly distributed within two standard deviations.
Show the unsuccessful run with the buggy Die.java and the successful run with the corrected Die.java. Please demonstrate code for Week05JUnitTest.java and the corrected Die.java.
An example output for an unsuccessful run is:
Counts: 173 161 174 158 151 0
Distribution: 0.17 0.16 0.17 0.16 0.15 0.00
mean: 136.00, std dev: 61.43
Analysis:
- Die failed to be randomly distributed
- A facet has a zero count
An example output for a successful run is:
Counts: 166 175 158 169 169 163
Distribution: 0.17 0.18 0.16 0.17 0.17 0.16
mean: 166.00, std dev: 5.35
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started