582 Strings and Characters Chapter 10 148 } (Windows 2003 server web)

582 Strings and Characters Chapter 10 148 } // end method main 149 150 } // end class DeckOfCards 151 152 // class to represent a card 153 class Card { 154 private String face; 155 private String suit; 156 157 // constructor to initialize a card 158 public Card( String cardFace, String cardSuit ) 159 { 160 face = cardFace; 161 suit = cardSuit; 162 } 163 164 // return String represenation of Card 165 public String toString() 166 { 167 return face + ” of ” + suit; 168 } 169 170 } // end class Card Fig. 10.21 Card dealing program (part 4 of 4). Class Card (lines 153 170) contains two String instance variables face and suit that are used to store references to the face name and suit name for a specific Card. The constructor for the class receives two Strings that it uses to initialize face and suit. Method toStringis provided to create a Stringconsisting of the faceof the card, the string “of”and the suitof the card. Class DeckOfCards(lines 11 150) consists of an array deckof 52 Cards, an integer currentCard representing the most recently dealt card in the deck array ( 1 if no cards have been dealt yet) and the GUI components used to manipulate the deck of cards. The constructor method of the application instantiates the deck array (line 29) and uses the for structure at lines 33 35 to fill the deckarray with Cards. Note that each Cardis instantiated and initialized with two Strings one from the faces array (Strings “Ace” through “King”) and one from the suitsarray (”Hearts”, “Diamonds”, “Clubs” and “Spades”). The calculation count%13 always results in a value from 0 to 12 (the Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply