Lab 09 10 pts Week 3 Due Apr 20

Lab 09: Dice Roller

Random numbers, static methods, and compound assignment

math-random static-methods compound-assignment
Clone from GitHub
3 checkpoints 5 autograder 2 timeliness
Prerequisites: lesson-1-5

Learning Objectives

After completing this lab, you will be able to:

  • Generate random integers in a specific range using Math.random()
  • Write and call static methods with parameters and return values
  • Use compound assignment operators (+=) to accumulate values
  • Build a menu-driven program using switch and a loop

What You’re Building

A dice-rolling utility with three static methods. The user picks how many dice to roll and how many sides each die has. The program rolls the dice, displays each result, and prints the total. A menu lets the user roll again, change settings, or quit.


Concepts and Common Misconceptions

Concept What students get wrong
Math.random() Returns a double in [0.0, 1.0). To get 1–6: (int)(Math.random() * 6) + 1. Forgetting the + 1 gives 0–5.
Static methods Trying to call a static method on an instance, or forgetting that static methods belong to the class, not an object.
Compound assignment += Writing total = total + roll works, but += is the idiomatic form. Some students write total =+ roll, which assigns +roll (positive roll) instead of adding.
Return vs. print A method that returns an int should not also print it. Print in the caller, not in the utility method.

Checkpoint 1: rollDie (1 pt)

Write a static method rollDie(int sides) that returns a random integer between 1 and sides inclusive.

public static int rollDie(int sides) {
    return (int)(Math.random() * sides) + 1;
}

Test it by calling rollDie(6) several times from main and printing the results. Verify that values stay in the range 1–6.

Debugging tip: If you always get 0, you forgot the + 1. If you get values above 6, your multiplier is wrong. Cast to int before adding 1, not after.


Checkpoint 2: rollMultiple (1 pt)

Write a static method rollMultiple(int count, int sides) that rolls count dice, each with sides sides. Use a for loop and += to accumulate the total. Print each individual roll and return the total.

Roll 1: 4
Roll 2: 2
Roll 3: 6
Total: 12

Call rollDie inside rollMultiple – do not duplicate the random logic.

Debugging tip: If your total is always 0, check that you initialized it to 0 before the loop and are actually adding each roll with +=. If you wrote total =+ roll, that is assignment, not addition.


Checkpoint 3: Menu-Driven Roller (1 pt)

Add a menu loop in main using switch:

1. Roll dice
2. Change settings
3. Quit

Option 1 calls rollMultiple with the current count and sides. Option 2 prompts the user for new values. Option 3 exits the loop. Use a boolean flag or a break to terminate the loop.

Default values: 2 dice, 6 sides.

Debugging tip: If Scanner reads the wrong input after a menu choice, you likely have a leftover newline from nextInt(). Call scanner.nextLine() after nextInt() to consume it.


How to Debug

  1. Test rollDie in isolation. Call it 20 times and verify the range before moving on.
  2. Print the accumulator. Inside your loop, print total after each += to watch it grow.
  3. Hard-code menu choices first. Before wiring up Scanner, call rollMultiple(3, 6) directly to verify output format.
  4. Check edge cases. What happens with 1 die? 1 side? 0 dice?

Scoring Breakdown

Component Points
Checkpoint 1: rollDie method 1
Checkpoint 2: rollMultiple with accumulator 1
Checkpoint 3: Menu-driven interface 1
Autograder correctness 5
Timeliness (on-time submission) 2
Total 10