University of Pittsburgh
Fall 2003
CS0007:  Introduction to Computer Programming
  Project 2: Gray Jack
DUE DATE
Friday, Oct 24, midnight
VALUE
50 points
BACKGROUND
Chapter 4, JBD
(especially 4.9)

Objectives

For this assignment you will be analyzing a problem and writing several methods that help you accomplish the task.  You will be given a pseudocode outline of a main block to follow, but you have to write the methods it suggests, and build a main() that follows the outline.  Normally, you would be required to do everything (including figuring out a main block layout), but the goal here is to get practice writing and using methods.

To prepare, you should make sure you've read the example in 4.9 (21 pickup) carefully.  Excellent example.

What to do

You will write a program that plays a simplified version of Black Jack, we'll call it "Gray Jack".  Basically, you will "deal" the user two random numbers between 1 and 10, and allow them to take "hits" until either they want to stop, hit 21 ("Gray Jack"), or go over 21 ("bust").  Your program should allow the user to play games as many times as s/he wants.  Do not worry about wagers, aces, or face cards (it's simplified!). 

For the computer's hand, we will simplify by just taking 15, and adding to it a random number between 1 and 10.  There are a number of other details to figure out, so see the hints section for pointers on how to proceed.

The following pseudocode is recommended for structuring your main block:

display rules
while (user wants to play more)
deal user's hand
take hits
deal computer hand
determine result
display result
ask if user wants to play again
end while

Your main() should be relatively small, then.  It should contain the necessary variable declarations, this loop, and calls to methods (that you will write) to handle the details. 

For each method you write, you need to determine a good name for it, input parameters, and return type.  Place comments next to each method describing the input, output, and what it does (briefly).  It is ok to do input and output within these methods.


Handing your work in

Same rules as usual.  See old handouts to remind you of the details.

Copy both your java source file and your compiled .class file in the handin directory.  Use proj2 as the directory name.  See the assignment 1 or the handing your code in handout to remind you of the steps.  Don't forget to verify the handin using ls.

Suggestions/Hints

A gradual approach is a good idea.  For example, start off by just having it deal you a hand.  Then add in the dealer's hand, then determine who wins, etc. etc.  You can add the loop later.  Playing multiple games is contingent upon being able to play a single game.

To come up with random numbers, use Math.random(), described in the book (use the index) and online.  As you will see, it returns a random number between 0.0 and 1.0 (a double).  You need to convert this to an integer between 1 and 10.  Think about it, you'll need a type cast.  If you can't figure it out, just ask. 

Again, the computer's hand is easily computed by picking a random number between 1 and 10 and adding that to 15.  This will give you a variety of outcomes (between 16 and bust). 

Think hard about what your methods should return to main() and what they need to know, as parameters.  This is key if your program parts are to communicate properly.  Your methods must use parameters and return values.  Don't forget about void methods.

You are not required to have exactly the methods above, but you should stick to the general idea.

GOOD LUCK!

Last Updated: 10/16/03 by H. Chad Lane