Question: Write a static method called rollSum that takes a Random object and an integer as parameters and returns the number of dice rolls it takes

Write a static method called rollSum that takes a Random object and an integer as parameters and returns the number of dice rolls it takes to roll the sum specified.

The method should use the Random object to select numbers in the range of 1 to 6 inclusive (simulating a six-sided die roll) where each number is equally likely to be chosen. The method will output the die roll value each time the die is rolled. Rolling will stop when the sum of all rolls is equal to or greater than the specified sum (the integer parameter).

For example, given the following code:

Random r = new Random(); rollSum(r, 10); 

May have an output of:

Roll: 6 Roll: 2 Roll: 5 

In this example, 10 was the target sum. 6 is less than 10, so the die was rolled again. 6 + 2 = 8 is less than 10, so another roll. 6 + 2 + 5 = 13, so the rolling stopped.

This example would return 3 because the die was rolled three times (6, 2, and 5).

Another example run:

Random r = new Random(); rollSum(r, 18); 

May have an output of:

Roll: 6 Roll: 1 Roll: 1 Roll: 6 Roll: 2 Roll: 4

In this example, since the target is 18, the method should return 6 because the die was rolled six times (6 + 1 + 1 + 6 + 2 + 4 = 20).

You may assume the integer value passed to your method is greater than or equal to 0. You are to exactly reproduce the format of these logs.

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!