CS 139/149 Programming Fundamentals

System Requirements Specification

Programming Assignment 4

JMU Slots

Due Tuesday Nov 12, by 11:59pm (CS 139)

Due Wednesday Nov 13, by 11:59pm (CS 149)

Corrections

Check here for the latest updates.

Updated a discrepany between UML and writeup - 11/7/2013

Updated a discrepancy between the UML and writeup - 11/11/2013

Updated an error in the description of toString - 11/12/2013

Introduction

In an effort to raise money for the UREC expansion, JMU is considering implementing JMU Slots, a series of slot machines that will entice students to play. Using dining dollars, flex, or cash, bets can be placed and money won. Your job is to write an application that can be used to simulate these games.

Objectives

General Program behavior

There will be two inputs to the program, the wager and then a continuation question. Output will be of the form of Win or Lose and if the user has won, the amount won.

  1. Display an opening message.
  2. Prompt the user for the wager and check that it is valid and read it in.
  3. Display the three items selected at random.
  4. If none of the three items match one another, the program should display "You lose." followed by a newline followed by a prompt "Play again?"
  5. If two or three items match, the program should display "You win " followed by the amount won followed by "!". If two items match, the player wins twice their bet. If all three items match, the player wins five times their bet.
  6. As soon as the player chooses not to play again or if the player has lost more than $50.00 overall, the application should display "Good bye" and the total of the player's winnings or losses is displayed using one of the following results.
    1. If the winnings are > 0, print "You won " followed by the amount formatted for money followed by a period.
    2. If the winnings are < 0, print "You lost " followed by the absolute value of the amount formatted for money followed by a period.
    3. If the winnings are 0, print "You broke even."

Output and Error Handling

All lines except for the Wager line and Go again? line should terminate with the new line character. This is a sample. Another sample can be found here.

Welcome to JMU Slots
<blank>
Wager: 5.00
<blank>
Item1<tab>Item2<tab>Item3
<blank>
You won $15.00!
Go again? Yes
<blank>
Wager: 1.5
<blank>
Item1<tab>Item2<tab>Item3
<blank>
You lose.
Go again? Nope
<blank>
Goodbye. You won $13.50.

If a money amount is entered incorrectly, either by entering a value of 0 or less or a value higher than 50 or something other than a number is entered, display:

<blank>
You entered value. Enter a positive number less than 50.00.
Wager: 

until a good value is entered.

The slots

Your program should choose from among these six items in this order:

Cherries, Oranges, Plums, Bells, Melons, or Bars

To choose, you will generate a random number in the range of 1 - 6. 1 corresponds to Cherries, 2 to Oranges, all the way through to 6 corresponding to Bars. You will generate this number 3 times and list out (in order) the item that corresponds.

Controlling the "random" number

Since we are going to use WebCAT to test your programs, you will need to control the "randomness". To do that, your program should read a long value from the argument list. This long value will be a "seed" for the random generate. A seed starts the random process and provides a consistent pattern of results. To read in the seed, you will use the technique that we used in the Standard Deviation lab and we've seen this in other labs. You will use the Random class in the SlotMachine to create your generator and use the Constructor that takes in the seed. See Random for more information about this class.

Program Structure

UML

You will have 4 classes. The first is JMUSlots.java which will contain a main method. SlotMachine.java will represent a slot machine. The Random number generator will be contained within the SlotMachine. The next is SlotsIO.java. The last is the SlotsGame which calls upon the methods of the other classes and run the game. Each of these classes and their required methods are described below.

JMUSlots.java - This class contains a main method. main will read in the argument from the command line, use it if present to construct a SlotMachine object and pass this object to the SlotsGame constructor, thereby constructing a SlotsGame. It will then call this SlotsGame playGame() method. If the argument is not present, use the default constructor to instantiate a SlotMachine.

SlotsGame will contain an attribute for the SlotMachine passed to the constructor and an attribute for the SlotsIO object used for this game. It should have an attribute to keep track of winnings (winnings - wagers). It must contain the following methods, but it can contain additional methods as you see needed.

Constructor - SlotsGame constructor has a single parameter, the SlotMachine. It will store that parameter in the corresponding attribute (field) and will also instantiate a SlotsIO object. It should initialize the winnings attribute to 0.

public SlotsGame(SlotMachine machine)    

playGame() will call methods in the other classes based on game play described above and will terminate when the user no longer wants to play or has reached the limit of greater than $50.00 loss.

	public void playGame()

lostFifty() will return true if the winnings attribute is less than -50 and false otherwise.

public boolean lostFifty()

calcWinnings() will take in the double representing the current wager and will return the winnings based on the slot machine matches. Wagers are first subtracted from the overall winnings and then the amount won is added back in. So if I wager $5.00, $5.00 is subtracted from the winnings and then if 2 matches are made, $10 is added back.

public double calcWinnings(double wager)

SlotMachine - This class contains an attribute for the Random number generator and the three "slots". It must contain the following methods. It may contain others.

Constructor - SlotMachine constructor has a single parameter, the Random generator. constructing the generator removed. It will set each of the slots to a starting value in this order: "Cherries", "Oranges", and "Plums".

public SlotMachine(Random rand)

Default Constructor - SlotMachine overloaded constructor has no parameters. It will construct a new Random generator using its default constructor and will set each of the slots to a starting value in this order: "Cherries", "Oranges", and "Plums".

public SlotMachine()

pullHandle() - This method will simulate pulling the handle of the slot machine. It will generate three Strings from the list based on the random generator nextInt method (think switch). It will update the attributes (change the state of this SlotMachine) and return a String that matches the required output of the values (see toString).

public String pullHandle()

matches() - This method returns 0, 2, or 3 depending on how many of the three Strings match one another.

public int matches()

toString() - This method returns a String in the format of "%s\t%s\t%s", where the substitutions are each of the three slot values (in order).

public String toString()

SlotsIO - This class contains an attribute for a Scanner object. It must contain the following methods. It may contain others.

Constructor - The SlotsIO constructor will instantiate the Scanner object.

public SlotsIO()

readWager() - This method will have a single parameter which is the prompt to output for the wager. It should error check to make sure that the wager has been entered correctly and will return a valid double number representing the wager.

public double readWager(String prompt)

playAgain() - This method will take in a single parameter which is the prompt to ask to play again. It should return true if the user enters any form of Yes or the single letter Y. It should return false otherwise.

public boolean playAgain(String prompt)

writeGoodbye() - This method takes in a single parameter which is the amount of overall winnings (or losses) and displays the goodbye line.

public void writeGoodbye(double winnings)

writeBlank() - This method outputs a blank line.

public void writeBlank()

writeWelcome() - This method outputs the welcome message.

public void writeWelcome()

writeResult() - This method takes in a single paramter which is the round winnings and prints the result.

public void writeResult(double winnings)


writeSlots() - This method takes in a single parameter which is the String of the slot items.

public void writeSlots(String slots)

Suggested approach

You should start stub out with documentation each of the classes. While you are doing this, think through how these classes will interact with one another.

You should build a tester for each class. This does not need to be a JUnit test, but should have methods to call each of the individual methods in the class as you build it (unit testing).

Don't delay. While each method is small, there are a lot of methods. You will likely need some time to get the classes all interacting with one another.

Honor Code

This work must conform to the JMU Honor Code and the specific requirements of this class. NO help may be provided by another student to another student. Authorized help is limited to your textbook, the TA’s for any CS139 section, and the professor for your section. You may work with your Lab Partner as long as you both are working together.You will be able to submit one program to WebCAT. Divide and conquer is not the way to produce quality code. At the same time, make sure that there is an equal sharing of both roles. It is important that both partners understand what is going on in the program. You may not receive help from any other students in the class or outside of the class.

Grading

HINTS