Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a method countRolls with two arguments, the first indicating a number of distinct six sided dice and the second indicating a desired sum. The

Write a method countRolls with two arguments, the first indicating a number of distinct six sided dice and the second indicating a desired sum. The method should return the number of ways the dice can add up to the desired sum. For example, with two dice and a sum of 11, there are two ways to create the sum: 6 and 5 or 5 and 6. Use recursive backtracking. Start with the code we wrote in class for rollDice with the helper and driver methods, but instead of printing, you will count the number of lines that would be printed. In this environment, methods must be public, method overloading is not allowed (give the helper method a different name like tryRolls), and the driver countRolls method must be defined after the helper method that it uses.

Starter Code:

image text in transcribed

1 import java.util.Arrays; 3 public class RollDice 5 public static void rollDice(int howManyDice, int desiredSum) // Don't use randomness! // Our program assumes each die has 6 faces /umbered 1 -6 int a-new inthowManyDice]; //Recursive method could take: 10 -Array of dice (partially filled) -Which is the current die being rolled 12 13 14 15 16 private static void rollDice(int a, int i, int desiredSum) 17 18 19 20 21 /I-The desired sum rollDice(a, 0, desiredSum); If we've rolled all dice, see if we hit sum if ia.length) [ if (desiredSum0) System.out.println(Arrays.toString(a)); / Maybe we failed? return 23 24 25 26 27 28 29 30 31 32 //If we have overshot our goal, return if (desiredSum a.length-i) return // Try all possibilities for die i for Cint d 1; d6; d++) ai] d; I/ Try this move rollDice(a, i+1, desiredSum-d); 34 35 public static void main(String args) t 36 37 38 39 rollDice(3, 6)

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

The Database Experts Guide To Database 2

Authors: Bruce L. Larson

1st Edition

0070232679, 978-0070232679

More Books

Students also viewed these Databases questions