Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Issues In Database Management A Refernce For The Thinking Practitioner

Authors: Fabian Pascal

1st Edition

0201485559, 978-0201485554

More Books

Students also viewed these Databases questions