Site Search

## Student Learning Objectives

• Demonstrate the ability to write a simple Java `DoublesGame` program and the user-defined `Die` class
• Demonstrate the ability to use a loop inside a Java main method
• Demonstrate the ability to write `equals` and `toString` methods in your user-defined `Die` class
• Demonstrate the ability to use the `Random` class to generate a random number
• Include a file header comment to identify the author, account, and honor code
• Browse through the Java Programming Style Guide for other standards and conventions to use in CS 110.

## Program 7 Assignment

Write a program that allows its user to play the dice game "Doubles"

Rules of the game

• Player begins with ten dollars (\$10.00)
• The player places a bet amount and a pair of dice are rolled
• If the two values showing on the pair of dice are the same (they rolled doubles), the player wins twice their bet amount
• If the two values showing on the dice are not equal, then the player loses their bet amount
• The game ends when the player is out of money

## Class Design

The `Die` class will be similar to your `Coin` class done in Lab 11 except that the instance variable representing the die's face value should be an `integer` type (to store the value `1` to `6`).

• Constructor method: initialize the die's value to 0 and instantiate the Random number generator object
• `roll`() method: use the Random object to simulate a die roll between 1 and 6 (see tip below)
• `equals(Die die2)`: returns true if both die have equal values, false otherwise
• `toString`(): returns the string "one", "two", "three", etc. based on the integer value of the die

Tip: The Random class method nextInt(int n) returns a random integer value between 0 to n-1. To generate a random integer between 1 to 6 you can use

```randomNumberGenerator.nextInt(6) + 1
```

## Sample Session

```You have \$10.00
How much would you like to bet? `5.00`
You rolled a one and one
You win \$10.00

You have \$20.00
How much would you like to bet? `5.00`
You rolled a five and four
You lose \$5.00

You have \$15.00
How much would you like to bet? `15.00`
You rolled a six and two
You lose \$15.00

Better luck next time
```

## What to turn in

• Be sure your files are saved in your `U:\programs\program7\` folder on the computer science network.
• Two source program: `Die.java` and `DoublesGame.java` are required
• Both files should begin with a file header comment
• Note: if you complete the assignment on your personal computer, you must copy the file(s) to your your CS network account
• An easy way to copy the files is to use a USB portable drive, email, or a digital drop box
• Instructions for accessing your U-Drive remotely is provided if you are unable to physically come into the Hebeler lab to copy the files
• Your program will be checked online. You do not need to print out a copy of your program to turn in.
• The assignment is due at the start of the next lab.
• If you are unable to finish in time, you may turn in what you have done to receive partial credit for the assignment.
• You are allowed to drop one program assignment score for the quarter

Include a file header comment at the top of every source code file. Include the assignment number, your cs110 account number, your name, completion time, and the honor code.

Remember, program assignments are to be done on your own. If you receive help understanding or debugging the assignment, you must acknowledge the person who helped. You must do the coding on your own.

```//**********************************************************
// Assignment: Program 7
// Account: (Enter your cs110 account number here)
//
// Author: (Enter your full name here)
//
// Completion time: (Enter the total number of hours you
//                   spent on the assignment)
//
// Honor Code: I pledge that this program represents my own
//   program code. I received help from (enter the names of
//   others that helped with the assignment, write no one if
//   you received no help) in understanding and debugging my program.
//*********************************************************
```

20 points possible
CS 110 Program 7 Grading Sheet (PDF)

• 7 points - Output Correctness
• The program prompts and reads in bet amounts from the user (2 points possible)
• The correct winning or losing amount is displayed and the player's money adjusted correctly (2 points possible)
• The program loops until the player is out of money (2 points possible)
• Duplicate the sample output exactly, do not make up your own words or formatting (1 point possible)
• 9 points - Class Definitions
• The `Die` class is implemented correctly with
• `roll` method that uses a Random object to obtain a random number between `1` and `6` (2 points)
• `equals` method that compares two die and returns a Boolean value: true or false (2 points)
• `toString` method that returns the string equivalent of the value showing on the die (2 points)
• The `DoublesGame` main method uses two `Die` objects and uses the `roll`, `equals`, and `toString` methods correctly (3 points)
• 2 points - Comments and Alignment
• Skip a blank line before every single-line comment
• Generally one single-line comment is needed for every four to six lines of code
• Use jGRASP Generate CSD to properly align your printout
• Alignment and indentation are important. Read through the Java Style Guide for CS 110 expectations
• 1 points - Identifier Names
• Meaningful identifier names used for all identifiers
• Variable names should begin with a lower case letter, and each subsequent word should start with an upper-case letter.
• Avoid the use of abbreviations in choosing variable names
• Read through the Java Style Guide for CS 110 expectations.
• 1 points - Creation of Assignment Folder, File, and Printouts
• Folder `U:\programs\program7\` created correctly on your network U:\ drive
• Java source code file saved as `DoublesGame.java` and `Die.java`
• Program printouts submitted correctly