THINK FIRST·CODE LATER

← All labs

Dice simulator with java.util.Random

Problem

Write DiceLab with the methods rollDie(Random random) (returns 1–6), and bar(int count) (returns that many *). main reads a seed and a number of rolls, creates new Random(seed), and rolls two dice that many times (first die, then second die, with rollDie). Print the number of rolls, the average total, how many totals were 7 and how many rolls were doubles — each with a bar of count × 40 / rolls stars — the highest total, and the share of sevens.

For the input

2026 600

the program prints

Rolls: 600
Average total: 6.97
Sevens: 97 ******
Doubles: 103 ******
Highest total: 12
Share of sevens: 16.2% (theory: 16.7%)

With the same seed, Random always produces the same sequence: that is what makes a simulation testable.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l8-5-data-1.txt

2026 600
What your program must print for l8-5-data-1.txt
Rolls: 600
Average total: 6.97
Sevens: 97 ******
Doubles: 103 ******
Highest total: 12
Share of sevens: 16.2% (theory: 16.7%)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l8-5-data-1.txt

Windows PowerShell does not understand <:

Get-Content l8-5-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.