Tue Sep 17 and Thu Sep 19
Due: Mon Sep 23 before 11:59PM
The goals of this lab are to review the Java style rules you are expected to follow, implement the Yahtzee utility class, and then write a small simulation that computes the odds of rolling each Yahtzee category with a single roll.
The lab also introduces the idea of writing a computer simulation to solve or explore a problem. Computer simulations are used widely in the sciences and engineering to study problems that are hard or impossible to solve with absolute certainty: for example, climate simulations are used to study the potential effects of global warming; computational fluid dynamics are used to simulate the flow of air around an airplane or rocket; many scientists are trying to simulate the human brain on a computer; and your professor uses simulations to study positioning errors in computer-assisted surgery. This lab uses simulation to study the behavior of some playing dice; the problems studied in the lab are simple, but sufficient to illustrate many aspects common to creating more sophisticated simulations.
The style rules are not overly restrictive in CSE1030.
1. Your programs should use the normal Java conventions (class names begin with an uppercase letter, variable names begin with a lowercase letter, etc.).
2. In general, use descriptive variable names. There are exceptions to this rule; for example, traditional loop variables are often called i, j, k, etc.
3. Use a consistent indentation size. Beware of the TAB vs SPACE problem: Tabs have no fixed size; one editor might interpret a tab to be 4 spaces and another might use 8 spaces. If you mix tabs and spaces, you will have indenting errors when your code is viewed in different editors.
4. Use a consistent brace style:
// left aligned braces class X { public void someMethod() { // ... } public void anotherMethod() { for (int i = 0; i < 1; i++) { // ... } } }
or
// ragged braces class X { public void someMethod() { // ... } public void anotherMethod() { for (int i = 0; i < 1; i++) { // ... } } }
5. This one always causes problems for students. Insert a space around operators (except the period ".").
The following is
// some code somewhere boolean isBetween = (x > MIN_VALUE) && (x > MAX_VALUE); int someValue = x + y * z;
much easier to read than this
// AVOID DOING THIS // some code somewhere boolean isBetween=(x>MIN_VALUE)&&(x>MAX_VALUE); int someValue=x+y*z;
Utility classes are used to group a set of methods that perform related
tasks. For example, java.lang.Math
groups together
mathematical methods, java.util.Arrays
groups together
methods that operate on arrays, and java.util.Collections
groups
together methods that operate on collections.
A possible use for a utility class is to group together methods that define the rules for a game, such as the Yahtzee game that was introduced in the lectures.
As a brief reminder, Yahtzee is game played with dice where the goal is to try to roll certain categories of values using five six-sided dice. In Yahtzee, there are six special categories of dice values:
Name | Description | Examples |
---|---|---|
three-of-a-kind | at least three dice having the same value | 6-2-3-2-2 (has three 2s) |
four-of-a-kind | at least four dice having the same value | 5-5-5-1-5 (has four 5s) |
five-of-a-kind or Yahtzee | all five dice having the same value | 4-4-4-4-4 |
full house | three-of-a-kind and a pair | 2-2-3-3-3, 4-4-4-5-5 |
small straight | at least four sequential dice (roll must contain 1-2-3-4, 2-3-4-5, or 3-4-5-6) |
3-1-3-4-2 (has 1-2-3-4) 3-5-3-4-2 (has 2-3-4-5) 6-5-3-4-6 (has 3-4-5-6) |
large straight | five sequential dice (roll must contain 1-2-3-4-5 or 2-3-4-5-6) |
5-1-3-4-2 (has 1-2-3-4-5) 6-5-3-4-2 (has 2-3-4-5-6) |
Imagine that you are trying to implement the game of Yahtzee in Java. One
of the problems that you must solve is to determine if a roll of the dice
is one (or more) of the special categories of dice values.
In eclipse:
lab1
)cse1030.games
cse1030.games
create a new Java class named Die
.
The completed class is here:
and its API is here:
cse1030.games
create a new Java class named Yahtzee
.
A partial implementation is here:
This is all of the code that you have seen in the lectures thus far.
Yahtzee
so that it implements the following
API:
This means that you must create and complete the following methods:
isFourOfAKind
isFiveOfAKind
isFullHouse
isSmallStraight
isLargeStraight
main
(see Step 6)isSmallStraight
and isLargeStraight
. Instead,
consider using the method java.util.List.containsAll
main
method should compute the odds of rolling each
of the Yahtzee categories. To do so, you will simulate rolling 5 dice
1,000,000 times and count the number of times that each Yahtzee
category appears. The odds in percent of rolling a category can be computed using
the formula100.0 * n / 1000000
n
is the number of times the category appeared in
the 1,000,000 rolls. Your method should print the odds of rolling
each category as shown below (your odds should be similar but not exactly the same
because this is a random simulation; you can search the web for the
exact odds):Three of a kind: 21.3345% Four of a kind: 2.0084% Five of a kind: 0.0758% Full house: 3.9542% Small straight: 15.4229% Large straight: 3.0795%
Submit your solution using the submit
command. Remember that
you first need to find your workspace directory, then you need to find
your project directory. In your project directory, your files will be
located in the directory src/cse1030/games
submit 1030 L1 Yahtzee.java