L07

Due by the end of Tuesday, October 24

Starter Files:


SUBMIT / CHECK

Today's Activities

Activity #1
Download the program DoDice into your lab 7 project. Run the program. It simulates rolling some dice. In the first part it simulates rolling two standard (6-sided) dice. The second and third parts are incomplete.

Your first task is to create a method for rolling the standard, six-sided dice. The program uses the same code on two separate lines (both with the comment Activity 1). Replace those formulas with calls to a method named rollD6. The method takes no arguments, and returns a random number in the range of 1 to 6.

I've already created the javadoc comment for you. Place the definition of this method directly below the javadoc comment for it. (Note that there are three javadoc comments provided -- the other two are for activities 2 and 3.)

Run your program again and make sure it performs exactly as before. For example, you might get

I got a 6 and a 3. The total is 9.
or
I got a 5 and a 5. The total is 10. I got doubles!
Activity #2

Next you will complete part two of the program. There are many different kinds of dice besides just the regular six-sided kind. There are four-sided, eight-sided, ten-sided, twelve-sided and twenty-sided. And you can use two ten-sided dice to simulate rolling a 100-sided die.

Create a method named rollD (under the appropriate javadoc comment) for rolling a multi-sided die. This method is given the number of sides on the die (which can be any integer number at all) and simulates rolling a die with that many sides.

Don't worry if the number of sides doesn't make sense. It's OK if they ask for a seven-sided die or a twenty-three-sided die. It's even OK if they ask for a one-sided die, a zero-sided die, or a minus-nineteen-sided die. Computer scientists have a saying: "Garbage in; garbage out." That means that if they ask for something stupid, we'll give them something stupid back. But if they ask for something reasonable, we'll give them what they want.

Find the place in main where d1 and d2 are assigned the value 0 (the lines marked with the comment Activity 2). Change them so that d1 is set to the result of rolling a 10-sided die, and d2 to the result of rolling a 100-sided die (just as it says in the output). For example, you might get

I got a 6 on the d10 and a 34 on the d100.
or
I got a 5 on the d10 and a 79 on the d100.

Activity #3

Our last activity with this program is to write a method that rolls multiple dice. The method is called rollMany and it's given the number of dice to roll and how many sides are on each die. It uses a loop to simulate rolling that many dice with that many sides, and returns the sum of all those rolls For example, if it were given 2 and 6, it would simulate rolling two six-sided dice, and it might return 2, or 7, or 12 -- whatever those two dice added up to.

Find the place in main where sum is assigned the value 0 (the line marked with the comment Activity 3). Change it so that sum is set to the result of rolling the dice requested by the user (just as it says in the output).

Test your method by running the program several times, checking that the sum is appropriate to the number and type of dice rolled. (The sum must be at least the same as the number of dice, and at most the number of dice times the number of sides. It'll usually be around half-way in between those extremes, tho'.) For example, you might get

How many dice should I roll? 10 And how many sides should each die have? 10 I got a total of 54 on those 10d10.
or
How many dice should I roll? 1 And how many sides should each die have? 1 I got a total of 1 on those 1d1.

Submit this/these files:

You will be graded on the following:

  1. DoDice.java submitted revised; compiles and runs
  2. Formulas for rolling dice in main both replaced with calls to rollD6
  3. 0s in second part of main replaced with calls to rollD
  4. ... with correct arguments
  5. 0 in third part of main replaced with correct call to rollMany
  6. no method called more often than it should be
  7. all method definitions placed in correct locations w.r.t. javadoc comments
  8. all methods declared public (or private) static
  9. all methods have correct number and type of parameters
  10. body of rollD6 is correct (return of copied formula OR call to rollD)
  11. body of rollD is correct (return of slightly modified formula)
  12. body of rollMany loops correct number of times
  13. rollMany calls rollD
  14. rollMany returns sum of rolled dice

SUBMIT   /   CHECK