import java.util.Scanner;
import java.util.Random;
public class ScoreDice {
private Random r;
public ScoreDice(long seed) {
r = new Random(seed);
}
public int rollD6() {
return r.nextInt(6) + 1;
}
public int score() {
int roll1 = rollD6();
int roll2 = rollD6();
return scoreWithNumbers(roll1, roll2);
}
// You will need to write a static method named
scoreWithNumbers.
// scoreWithNumbers returns a score based on the values
// of its inputs, as such:
//
// - If both inputs are 1 or if both inputs are 6, then
// the returned score is 10
// - If both inputs are the same (but not 1 or 6), then
// the returned score is 8
// - If the inputs are different, the score is whatever the
// smaller value is (e.g., if given 3 and 4, it returns 3).
//
// TODO - write your code below this comment.
public static int scoreWithNumbers(int a, int b){
if ( a != b){
return Math.min(a,b);
}
if (( a == 1 && b == 1) || (a == 6 && b ==
6)){
return 10;
}
else if ( a == b){
return 8;
}
return 0;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter seed: ");
long seed = input.nextLong();
ScoreDice scoreDice = new ScoreDice(seed);
System.out.println("Score: " + scoreDice.score());
}
}
NEED HELP WITH TEST CODE
import static org.junit.Assert.assertEquals; import org.junit.Test; public class ScoreDiceTest { @Test public void testBoth6() { assertEquals(10, ScoreDice.scoreWithNumbers(6, 6)); } // You must write at least FOUR more tests, one for each unique // behavior that scoreWithNumbers can show. The test above // already tests for one of five possible behaviors; you must write // tests for the other four. You may define redundant tests, but // you will receive no extra credit for them. // // If you're not sure you hit all the behaviors of interest, feel // free to ask us! // // TODO - write your code below this comment. }
Here are the 4 more test cases which will test for different cases.
Case 1: When both the inputs are 1, it should return 10.
Case 2: When both are equal but not 1 or 6, it should return 8.
Case 3: When inputs are not equal then function should return minimum input, in this case first argument is minimum.
Case 4: When inputs are not equal then function should return minimum input, in this case second argument is minimum.
Else everything in the program is correct.
Code:
@Test
public void testBoth1() {
assertEquals(10, ScoreDice.scoreWithNumbers(1, 1));
}
@Test
public void testBothEqualButNot6or1() {
assertEquals(8, ScoreDice.scoreWithNumbers(2, 2));
}
@Test
public void testFirstArgumentIsMin() {
assertEquals(3, ScoreDice.scoreWithNumbers(3, 5));
}
@Test
public void testSecondArgumentIsMin() {
assertEquals(3, ScoreDice.scoreWithNumbers(5, 3));
}
Code screenshot.
Get Answers For Free
Most questions answered within 1 hours.