Back to 111 Main Page

Mobius strip

Assignment 17: Part 3 (UsernameA17)

Task

Finish A17 by writing a dealer class that allows a human user to ask for cards from a Deck.

Steps

The idea of this program is that, when you go to a casino, you don't usually get to touch the deck yourself. Instead, you ask the dealer for a certain number of cards. If the dealer runs out of cards, he opens a new deck and the game continues.

Write a program that allows the user to draw as many cards as she'd like from a shuffled deck. Each card drawn should be displayed one per line. If she asks for 0 cards, your program should quit. (It's up to you whether you want to treat negative numbers as an error or an indication to quit.)

After each draw, your program should state how many cards remain in the deck.

If the last card in the deck is drawn, a new shuffled deck should be created so that drawing may continue until the total number of requested cards have been drawn. The program should indicate when a new deck has been "opened"/created.

Sample Output

This is your digital deck of playing cards!
A new deck has already been opened and shuffled for you.

[52 cards left in deck] How many cards to draw (0 to quit)? one
Error: Please enter a number >= 0.

[52 cards left in deck] How many cards to draw (0 to quit)? 1

5 of Hearts

[51 cards left in deck] How many cards to draw (0 to quit)? 3

Ace of Diamonds
7 of Diamonds
9 of Clubs

[48 cards left in deck] How many cards to draw (0 to quit)? 7

8 of Hearts
8 of Diamonds
8 of Spades
5 of Diamonds
4 of Diamonds
Jack of Hearts
2 of Diamonds

[41 cards left in deck] How many cards to draw (0 to quit)? 45

King of Diamonds
3 of Diamonds
King of Clubs
3 of Clubs
Jack of Clubs
6 of Spades
Queen of Hearts
6 of Diamonds
Queen of Diamonds
4 of Spades
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Ace of Clubs
Ace of Hearts
10 of Spades
2 of Hearts
3 of Hearts
Ace of Spades
4 of Hearts
6 of Hearts
7 of Hearts
9 of Hearts
10 of Hearts
3 of Spades
King of Hearts
2 of Spades
Queen of Spades
5 of Clubs
5 of Spades
7 of Spades
9 of Spades
7 of Clubs
Jack of Spades
King of Spades
2 of Clubs
4 of Clubs
6 of Clubs
8 of Clubs
10 of Clubs
Queen of Clubs
[--Opened a new deck--]
Ace of Clubs
8 of Clubs
8 of Diamonds
Ace of Diamonds

[48 cards left in deck] How many cards to draw (0 to quit)? 0

What to Submit

Normally, each Java class should be in its own file. However, Tamarin only takes a single file per submission, so you are going to put all three classes you wrote into a single .java file. Java will still only allow you to have 1 public class per file though, so you need to remove the word public from the other two classes.

So, your UsernameA17.java file should contain all 3 classes, like this:

public class UsernameA17 {
  //... your dealer class, with a public main method.
}

class Deck {
  //... contents of the Deck class ...
}

class PlayingCard {
  //... contents of PlayingCard class ...
}

Leave the public and privte modifiers on all your instance variables and methods; you just need to remove the public from the two classes. Make sure you don't put one class within another!

Also, you will have to move any import statments from the Deck and PlayingCard class to the top of the file. That is, all import statements must be stated before the first class.

You may want to try compiling and running your UsernameA17.java in a different directory before you submit, just to make everything still works correctly.

Then, upload your complete UsernameA17.java file to Tamarin.

Grading [10 points]

1 - Compiles + Coding Standards
Your program compiles successfully (no errors). Your code follows Java coding standards.
0.5 - Encapsulation
All instance variables are private.
2.5 - PlayingCard
Contains requested public static constants for suits and Joker (0.5). Constructor (0.5), two get methods (0.5), and toString() (1.0) are named correctly and work as specified.
3.5 - Deck
Contains the 2 constructors (1.5), draw() (0.5), getSize() (0.5) and shuffle() (1.0) methods. All perform correctly as specified.
2.5 - UsernameA17
Always draws the specified number of cards from a shuffled deck, displaying each 1 per line (1.0). Indicates when a new deck has been opened (0.5). Quits on 0 and catches bad input (0.5). Indicates how many cards remain in the deck after each draw (0.5).

FAQs

So, although a new Deck can be sorted, we're only creating shuffled decks here?
That is correct. Being able to create a sorted deck is still handy for testing purposes though--it's easier to see if all the 52 cards are really there.
When the Deck runs out of cards, do we reset it somehow or create a new Deck object?
Create a new Deck object.


~ztomasze Index : TA Details: ICS111: A17-3
http://www2.hawaii.edu/~ztomasze
Last Edited: 28 Oct 2009
©2009 by Z. Tomaszewski.