ulrail.gif

Lab Quiz 1

  ur-2c.gif urrail.gif
blrail-nonavbar.gif

Home | Resources | Homeworks | Exams
Lectures | Labs | Contacts | Submit | TAs

br-2a.gif   br-2c.gif brrail.gif
spacer.gif spacer.gif

Pledged Lab Quiz

This lab quiz is pledged.  You may use the course textbook, and NOTHING ELSE.  This means you cannot use any files in your home directory, previous assignments, slides, etc.  However, you should still save your file into your home directory.  You have 1 hour and 15 minutes to complete this lab quiz.

You will need to submit a single file called LabQuiz1.java.  You may download the skeleton code here: LabQuiz1.java.  You must also save the Deck.class file to the same directory that your LabQuiz1.java file is in.  For both files, right click on the link and select "Save as" (or "Save target as").

The TAs cannot help you with Java-related issues on this quiz.  If you are having problems getting JCreator to work, can't log in to the machines, etc., then you are welcome to ask the TAs for assistance.  If you do not understand what the lab is asking, the TAs may be able to help you (meaning if it's a problem with our explanation, then they can help you -- if you've been skipping the lectures so far and have no idea what any of this means, you are on your own).

If you get an error in JCreator that states, "cannot find symbol class Deck", then you have not saved the Deck.class file properly.  You may ask a TA to help fix this problem.  In other words, if you can't get the skeleton code to compile, then you can ask for help.  Once you make any modifications to the skeleton code, you cannot ask for help.

Advice

  • To maximize your points, make sure your programs compile successfully.
     
  • If towards the end things are not going as well as you would want (i.e., the program does not compile), it might be best to try commenting out errant statements (i.e., put a // in front of them).
     
  • If you run out of time, submit what you have -- there will be partial credit for this quiz.  Also remember that you can submit a file as many times as you want -- only the most recent submission is graded.  So once you have it working, submit what you have.

Purpose

In this lab quiz, you will finish the development of a new and very exciting card came.  In this game, 5 cards are drawn from a deck, and their face values are totaled.  If the value is greater than 36, then you win.  If the value is less than or equal to 36, then you lose.  A pretty simple game, but we are sure it will soon replace Texas Hold'em as the popular card game of choice.

Background

A standard deck of cards has 52 cards, the cards are split evenly into four suits: spades (♠), hearts (), diamonds () and clubs (♣).  Note that two of the suits are colored red (hearts and diamonds) and two are colored black (spades and clubs).  The Deck we use will be abbreviating the suits by their first letter: "S", "H", "D", and "C", respectively.  Each suit has 13 "ranks," with point values 1 through 13.  The point value-to-rank correspondence is as follows: 1 is called an Ace, 2 through 10 are the corresponding rank of the same name, 11 is called a Jack, 12 is a Queen, and 13 is a King. 

If you are confused about how a standard deck of cards works, you may ask a TA for help.  But not how the Deck class (described below) works.

Deck Class

For this quiz, you will need to use the Deck class, which we describe below.  For our purposes, each card in the Deck will be identified by integer values from 1-52.  For example, the int value of 1 is an Ace of clubs; 13 is a King of clubs; 14 is an Ace of diamonds, 26 is a King of diamonds, etc.  You don't have to remember which is which, though, as there are methods (getCardName() and getValue()) which will do that for you.

The relevant methods in the Deck class are as follows.

  • Deck (): The constructor, it allows you to create a new Deck object. The Deck will be initialized to a standard 52 card deck.  The skeleton code already contains this method call.
     

  • toString(): Returns a String representing the contents of the entire Deck.  This allows for easy printing of the deck: System.out.println(myDeck); will print the Deck to the screen.  You won't have to actually call this method directly -- but it allows you to print out the deck as just shown.
     

  • deal(): Deals a card from the Deck.  It takes in no parameters and returns a single int value (in the range of 1-52) that represents the card dealt.
     
  • getSize(): Returns the number of cards in the Deck.  It takes in no parameters and returns an int.  A newly created deck will have 52 cards.
     
  • getValue(int card): Gets the point value for a card.  This is the numerical value of the card -- a 2 of spades will be a 2, a Jack will be an 11, etc.  An Ace is defined as a 1 for this game.  This method takes in an int representing the card (such as a 26 for the King of diamonds), and returns an int representing the face value (such as 13, which is the face value for all Kings).
     
  • getCardName(int card): Returns a String which represents the name of the card.  This method takes in an int representing the card (such as a 26 for the King of diamonds), and returns a String representing the card (such as a "KD" for the King of diamonds).
     
  • shuffle(): This method will shuffle the deck -- it mixes up the order the cards are kept in.  It takes in no parameters and returns no value.
     

Lab Quiz

To get full credit on this quiz, your code must complete the following ten steps.  These steps are included in the skeleton code as comments (and some are already completed).

  1. Print a legend which describes the program
  2. Create the variables.  Declare and create a Deck object.
  3. Print out the deck and the deck's size.
  4. Shuffle deck (and print out that you are doing so).
  5. Print out the deck and the deck's size.
  6. Print a descriptive message that 5 cards will be dealt.  "Deal" 5 cards (via the deal() method) and print the corresponding card names (via the other methods above).
  7. Print out the deck and the deck's size.
  8. Sum the card values to calculate the total point value of the hand of 5 cards that was dealt.
  9. Print the value of the hand that was calculated in Step 8.
  10. Print if the hand dealt won or lost.  A hand has won if its total point value is greater than WINNING_VALUE.

Although this may seem like a lot of steps, note that steps 1 and 2 are already done for you in the skeleton code.  And steps 3, 5, and 7 are the same thing.  Thus, there are really 6 steps that need completing.  For testing your code, you should hand calculate the point total of the hand dealt and compare it to the value printed (you do not need to include proof of testing for lab quizzes).

Get the steps working in order!  Make sure step 1 works before you move onto step 2.  If you don't follow this, you will end up with a mess of code that doesn't work, and you will not be able to fix it within the lab quiz time.  There will be partial credit given for this lab quiz -- so if you only get steps 1-6 working, you will get a good amount of points.  But if you have a large mess of code that doesn't work, you will get very few points.

Sample output

Below is a sample run of the program.  Note that the numbers in parentheses are indicating which of the 10 steps are being performed -- this is just for your aid in reading the sample output.  You don't have to include the numbers in your program (although you are welcome to, if you want).  Note that no user input is required for this program.  Your output should give the same information, but does not have to be formatted the same.

(1) Welcome to the CS 101 Card Game!

(2) Creating variables...

(3) The deck is size 52 and is: [
 AC, 2C, 3C, 4C, 5C, 6C, 7C, 8C, 9C, 10C, JC, QC, KC,
 AD, 2D, 3D, 4D, 5D, 6D, 7D, 8D, 9D, 10D, JD, QD, KD,
 AH, 2H, 3H, 4H, 5H, 6H, 7H, 8H, 9H, 10H, JH, QH, KH,
 AS, 2S, 3S, 4S, 5S, 6S, 7S, 8S, 9S, 10S, JS, QS, KS
]

(4) Shuffling the deck...

(5) The deck is size 52 and is: [
 7D, JD, QH, 2H, 3C, 10H, 6D, 4D, JH, 7C, 3H, 2D, AS,
 7S, JC, 8C, 6H, KD, QS, 3S, 9C, 8D, KC, 6C, 2C, 4C,
 AC, 5S, JS, 9S, 5D, 6S, 4S, AH, 10D, KS, 5H, 4H, 3D,
 AD, QD, 8H, 9H, QC, 10C, KH, 5C, 2S, 10S, 7H, 9D, 8S
]

(6) Dealing 5 cards:
Card 1: 7D
Card 2: JD
Card 3: QH
Card 4: 2H
Card 5: 3C

(7) The deck is size 47 and is: [
 10H, 6D, 4D, JH, 7C, 3H, 2D, AS, 7S, JC, 8C, 6H, KD,
 QS, 3S, 9C, 8D, KC, 6C, 2C, 4C, AC, 5S, JS, 9S, 5D,
 6S, 4S, AH, 10D, KS, 5H, 4H, 3D, AD, QD, 8H, 9H, QC,
 10C, KH, 5C, 2S, 10S, 7H, 9D, 8S
]

(8) Summing the card values...

(9) The value of your hand is: 35

(10) Sorry, that value does not beat 36, please play again!

 

If the sum of the dealt cards was greater than 36, the last two steps would look like the following (of course, the shuffled deck in steps 5 and 7, and the dealt cards in step 6 would be different):

(9) The value of your hand is: 51

(10) Congratulations!  Your hand value: 51 beats 36

Incremental development

  • Test your program incrementally.
  • You should strive to test your program incrementally.
  • Seriously, incremental testing.
  • By incremental testing, we mean that you should get the third step working (as we've provided steps 1 and 2 already), and then run it to make sure it works.  Once you are sure the first step works, move onto the fourth step.  Make sure you get that working, then move onto the fifth step.
  • If you fail to do this, you will have a big pile of broken code that will be impossible for you to debug.
  • Test your program incrementally.
  • We're serious about this.

Other Requirements

You are only required to follow a few of the good programming practices discussed in HW 1.  If you have time, feel free to do the others -- but due to the time constraints of the quiz, you don't have to do them all.  The ones you must do are listed below.

  • Header: The comments at the very beginning of the file, and need to include your name, e-mail ID, and 101 section (or 101-E, if appropriate).  You don't need a purpose for the lab quiz.
     
  • Whitespace: A line or two between separate elements of the code (such as between each of the steps) and good indentation.
     
  • Variable names: All final variables should be in all caps with underscores between the words, such as BOX_WIDTH.  Non-final variable names should have the firstLetterOfEachWordCaptalized in the variable name (excepting the very first letter). Variable names should be relevant and informative, such as mileage and not m.
     
  • Line length: The lines should not go over about 72 characters. Essentially, when displayed on an 80-character wide screen (what we are using), the line should not go past the end of the screen. You can have a System.out.println() statement, for example, use multiple lines (as long as each string starts and ends on the same line). JCreator tells you the column that the cursor is currently in (at the bottom on the status bar), so you can use that to gauge how long your lines are.

Submission

When you are finished, submit your LabQuiz1.java file.  You will have to "sign" a pledge on the submission.

 

spacer.gif
spacer.gif footer-middle.gif spacer.gif