Title: COMP 14 Introduction to Programming
1COMP 14Introduction to Programming
- Jingyu Yan
- March 30, 2005
2Program 5
- Blackjack Solitaire
- You will allow the user to play a GUI-based
Blackjack game alone (with no other players or
dealer) - Due Wednesday, April 8 at 1159pm
- 75 points
- 10 points extra credit available
3Classes
- Complete Code (no changes)
- Blackjack - main method
- BlackjackUI - user interface
- BlackjackApplet - user interface for applet
- Card - same as Program 4
- Outlines
- Deck
- Hand
- BlackjackGame
- start with Program 4 and make additions
4Deck Class
- Variables
- array of cards
- index of top of the deck
- Methods
- constructor
- int cardsLeft()
- void shuffle()
- void swap (int ind1, int ind2)
- Card dealFromTop()
- String toString()
5Deck Methods
- Deck()
- instantiate and initialize the deck of cards
(create a Card object for each card in the deck) - int cardsLeft()
- returns the number of cards left in the deck
(uses the top of the deck index) - void shuffle()
- shuffle the deck
- swap random pairs 1000 times
- void swap (int ind1, int ind2)
- swap deckind1 and deckind2
- Card dealFromTop()
- return the top card from the deck
- String toString()
- return a String representation of cards left in
the deck
6Swapping
- Exchanging two values
- Requires three assignment statements
temp first first second second temp
Note temp must be the same type as first and
second
7Array of Objects
Card card
card deck2
deck0
deck1
deck2
card
8Returning Objects
- We can return objects from methods just like
passing objects to methods - we're returning the address of the object
public Rectangle getElement(int
position) return (rectangleArrayposition)
Assume rectangleArray is an array of Rectangle
objects
9Cascaded Method Calls
- If the result of a method call is an object, we
can tack on another method without using a
placeholder variable
Rectangle temp getElement(2) int width
temp.getWidth()
Equivalent statments
int width getElement(2).getWidth()
10Hand Class
- Variables
- array of cards
- number of cards in the hand
- Methods
- constructor
- void addCard (Card card)
- int getNumCards()
- void resetHand()
- Card getCard(int position)
- String toString()
11Hand Methods
- Hand (int maxCards)
- instantiate the array of maxCards Card objects
- void addCard (Card card)
- add the given card to the hand if there's room
- int getNumCards()
- return the current number of cards in the hand
- void resetHand()
- clear the hand (set the number of cards to 0)
- Card getCard(int position)
- return the card at the given position in the hand
- String toString()
- return a String representation of the entire hand
12Hand Class
The array of cards in the Hand class should not
be new cards, but should just point to cards from
the deck
deck0
deck1
deck2
hand0
13BlackjackGame ClassAdditions
- Everything should be static
- Variables
- deck of cards (use the Deck class)
- player's hand (use the Hand class)
- Methods
- initGame
- setupNewHand
- playerCanHit
- getPlayerHand
- addPlayerCard
- calcPoints
14BlackjackGame ClassMethods
- initGame
- instantiates deck of cards and player's hand
- shuffles deck of cards
- getPlayerHand
- returns the player's hand
- setupNewHand
- reshuffle the deck (only if needed), reset the
player's hand, deal two cards to the player (add
cards to the player's hand) - addPlayerCard
- add a card from the top of the deck to the
player's hand
15BlackjackGame ClassMethods
- playerCanHit
- the player can "hit" if the point total is less
than Blackjack and there are less than 5 cards in
the hand - calcPoints
- overloaded method
- new version calculates the points for an entire
hand (including adjusting for Aces)
16Extra Credit
- 10 points available
- Make an applet of the Blackjack Solitaire game
- Instructions are provided
- Put the web address (URL ending in .html) of your
applet in the comments section on Blackboard when
you turn in Program 5
17Programming Tips
- Start early!
- Start small!
- get Deck class working first
- get Hand class working next
- Test classes without the whole program
18Write a Tester Class
public class Tester public static void
main(String args) Deck testDeck new
Deck() System.out.println (testDeck) testDe
ck.shuffle() System.out.println
("Shuffled") System.out.println
(testDeck)
The only classes that have to be working for this
program are Tester, Deck, and Card.
19Write a Tester Class
public class Tester public static void
main(String args) Deck testDeck new
Deck() Hand testHand new Hand(5) testHand.a
ddCard(testDeck.dealFromTop()) testHand.addCard(
testDeck.dealFromTop()) System.out.println
(testHand) System.out.println ("cards left
(50) " testDeck.cardsLeft()) System.out.pr
intln (testDeck)
The only classes that have to be working for this
program are Tester, Deck, Hand, and Card.
20Program 5 Announcements
- Blackjack Class Design Document
- You can request a correct version of the Card
class and BlackjackGame class from Program 4 by
emailing the TA who grades your assignments. - Problems w/Program 4? Come during office hours
or set up an appointment to talk to me or TA.
21Recitation This Week
- No laptops needed
- Ask questions about object-oriented design,
methods, arrays, Program 5, etc.
22Dealing from the Deck
deal from top
topOfDeck
2
0
1
deal from top
What card will be dealt next?
0
1
2
deal from top -- return the address of the card
at the top of the deck
23Next Time in COMP 14
- Searching and Sorting
- Read Ch 10 (pgs. 523-542)
24In-Class Programming
- DiceGame handouts
- URL at bottom of handout if you want to
cut-n-paste code outlines