On a roll Download

Christina Cacioppo

for loops, functions, arrays Student approved


Introduction

We've talked a lot about making dice. Now, let's start rolling 'em.


Task

The Dice class has two methods:

  1. getRolls(int), which should return an int array of dice-toss results that's numRolls long. You'd use this to get and store several dice rolls in one array.
  2. roll(), which returns the outcome of one dice roll – a 1, 2, 3, 4, 5, or 6. It's already written and works.

Write the getRolls(int) code.


Starter code

import java.util.Random; 

class Dice{

	/** Returns an array of values from the Dice as it's
	    tossed numRolls times.
	    @param    numRolls – the number of tosses we'll make
	    @return   an array of dice-roll results. It should be
	    		  numRolls long.
	**/
	int[] getRolls(int numRolls){
		/*		Your code here		*/
	}

	int roll(){
		Random gen = new Random();
		return gen.nextInt(5) + 1;
	}
}

Expected output

As one example, this code:

    Dice d = new Dice();
    int[] rolls = d.getRolls(6);
    System.out.println(rolls);
should print an int array that might look like:
[3,5,2,2,1,6]

Useful tools to solve the problem