0% found this document useful (0 votes)
8 views3 pages

Assignment 4

The assignment involves creating two classes: Die, which represents a die with a specified number of sides, and DiceCollection, which holds an array of Die objects. The DiceCollection class includes methods for rolling the dice, reporting sums, and generating a histogram of roll results. The main method interacts with the user to manage dice rolls and display results without performing calculations.

Uploaded by

tomcao2809
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Assignment 4

The assignment involves creating two classes: Die, which represents a die with a specified number of sides, and DiceCollection, which holds an array of Die objects. The DiceCollection class includes methods for rolling the dice, reporting sums, and generating a histogram of roll results. The main method interacts with the user to manage dice rolls and display results without performing calculations.

Uploaded by

tomcao2809
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

COMP10062: Assignment 4

Dice Collection
Learning Objectives
This assignment is about creating and using arrays of objects.

The Assignment
You will define two classes – one to represent a die with an
arbitrary number of sides, and one to represent a collection of dice
(these are the model classes). Then you will roll the entire collection
repeatedly and use an array to build a histogram of the results. The
view class for this assignment uses a main method and Standard
Input and Output.

The Dice Collection (model)


A Die object can have any integer number of sides. The number of sides is specified when a Die is
created and cannot be changed. A Die can report its number of sides, can report the side that is
currently showing, and can be rolled. When a die is rolled, it generates and stores a new integer from 1
to n, where n is its number of sides. It has a toString() method that reports the number of sides and the
number that is currently showing. That’s all it can do.

A DiceCollection object holds a set of Die objects in an array. The number of sides on each Die is
specified by passing an array of integers to the DiceCollection constructor. This array is used to create
and store the corresponding Die objects. A DiceCollection object can report the current sum of all the
sides showing on the dice, it can report the maximum and minimum possible sums, and it can roll all the
dice at once. It has a toString() method that reports all the dice, the minimum possible roll, the
maximum possible roll, and the current total showing on the dice. It also has a histogram method,
described below. That’s all it can do.

The Histogram Method


The histogram method of DiceCollection accepts
a parameter that specifies the number of rolls it
should make. Then it rolls the entire collection of
dice that number of times and returns an integer
array of counters (i.e., a histogram) to keep track
of how many times each possible sum got rolled.
For example, if the sum of the dice is 12, you
should add 1 to array element 12.

See HistogramExample.java in this week’s


examples for some code that might help.

Design and Implementation


The exact details of the implementation and interface of these two classes is up to you, but you must
respect the specifications given above, and you must create and hand in a UML class diagram to
represent Die, DiceCollection and the association relationship between the two classes. This UML
diagram can be hand drawn, or it you can use UMLet or draw.io. If you have another piece of software
you would like to use, check with your instructor.

The Main Method (View)


The main method asks the user to enter the number of dice and number of sides for each die. Then it
should create the DiceCollection and print it to the screen. Then it should present a menu in a loop that
allows them to roll once or roll 100,000 times. The main method should do no calculations at all.

If they choose to roll once, show the result by printing the dice they got and the sum. If choose 100,000
rolls, call the histogram method described above, and print the non-zero elements of the array, as
shown in the example output.

Documentation Standards
Don’t forget to follow the Documentation Standards for the course (i.e. Javadoc comments, meaningful
variable and class names, consistent indenting). See the Documentation Standards on Canvas.

Submitting Your Work


You have approximately 1 week to complete this assignment

Please see Canvas for the instructions on due date and time and how to how to submit your work.

Evaluation
Your assignment will be evaluated for performance (20%), class diagram (20%), structure (40%), and
documentation (20%) using the rubric in the drop box.
Example Output
Below is an example output of the program. User input is boldfaced and red. You are free to make your
interface look however you like.

How many dice? 4


Enter the number of sides of each die: 4 4 4 6

Dice Collection: d4=4 d4=1 d4=1 d6=4


Min=4 Max=18 Current=10

1=roll once, 2=roll 100000 times, 3=quit: 1

Dice Collection: d4=1 d4=4 d4=3 d6=3


Min=4 Max=18 Current=11

1=roll once, 2=roll 100000 times, 3=quit: 2

4: 246 Optional Extra 1: Here’s a nicer way to display the


5: 1096
histogram, courtesy of Mark Yendt. Scale the numbers
6: 2489
down and print asterisks to represent the quantities.
7: 5253 4: 257
8: 8288 5: 1056 *
9: 11423 6: 2559 ***
7: 5308 *****
10: 13782 8: 8331 ********
11: 14621 9:11303 ***********
12: 13790 10:13788 **************
11:14643 ***************
13: 11507 12:13747 **************
14: 8347 13:11398 ***********
14: 8339 ********
15: 5257 15: 5287 *****
16: 2597 16: 2670 ***
17: 1053 17: 1043 *
18: 271
18: 251

Dice Collection: d4=1 d4=3 d4=1 d6=4 Optional Extra 2: How about
Min=4 Max=18 Current=9 displaying the histogram as a
bar graph using the
1=roll once, 2=roll 100000 times, 3=quit: 3 FXAnimationTemplate?
BYE!!!

You might also like