Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 20 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
20
Dung lượng
508,94 KB
Nội dung
toString() method in the BigTruck class overrides this method to include whether or not a trailer is attached. First you used what you learned about the super keyword to call Automobile’s version of the method, and then you simply appended the trailer value. Polymorphism Polymorphism is another complicated word for a simple concept. It simply means that an instance of a subclass is also an instance of its superclass. A BigTruck instance is an Automobile too. All classes inherit from the Object class defined in the Java language, so every instance of Automobile and BigTruck is also an instance of Object. For example, if a method existed that accepted an Automobile object as a parameter: public void doSomething(Automobile auto) { … } You can pass in a BigTruck object, because it is an Automobile: BigTruck truck = new BigTruck(); doSomething(truck); Back to the BlackJack Game You’ve learned a great deal about object-oriented programming in this chapter. Now you put this knowledge to good use by creating a text-based version of a blackjack game. In this section you subclass the CardDeck class, to create Random- CardDeck , a randomized deck of cards so the game isn’t predictable. You also learn about the java.util.Vector class, which will help in creating the final game. Finally, you write the actual BlackJack application. The RandomCardDeck Class You’ve already written a class that represents a deck of cards, the CardDeck class. Now you just need to override it so that the Cards stored in the deck are random- ized. To do this, you import the java.util.Random class. In the constructor, you call the superclass’s constructor to build a regular CardDeck, and then you call the new shuffle() method to rearrange the Cards randomly. One other thing you need to do is override the reset() method so that when you reset the top card, you also shuffle the deck. Here is the source listing for RandomCardDeck.java: /* * RandomCardDeck * Simulates a shuffled deck of cards */ 158 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 158 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. import java.util.Random; public class RandomCardDeck extends CardDeck { public RandomCardDeck () { super(); shuffle(); } public void shuffle() { Card[] shuffled = new Card[cards.length]; Random rand = new Random(); int cIndex; boolean placed; for (int c=0; c < cards.length; c++) { do { placed = false; cIndex = rand.nextInt(cards.length); if (shuffled[cIndex] == null) { shuffled[cIndex] = cards[c]; placed = true; } } while (!placed); } cards = shuffled; top = 0; } public void reset() { super.reset(); shuffle(); } } The shuffle() method deserves some elaboration. A new Card array, shuffled, is declared locally in this method. Its purpose is to be a temporary placeholder for the deck of cards as it is being shuffled. It is initialized to the same size as the cards array instance variable. This initializes the Card objects in the shuffled array to null. The for loop loops on all the Cards in the existing cards array, one at a time. Then a random index for the shuffled array is created. If a card has not already been placed at this index, stored in cIndex (you can tell because the Card object there is null), the card is placed there; if it is already occupied by another Card, it continues to look for vacancies until it finds one. So, the cards in the cards array are copied, one by one, randomly into the shuf- fled array, and then the shuffled array is assigned to the cards instance variable, causing it to be shuffled, and then the top variable is reset to 0. The RandomCard- DeckTest program tests to make sure that the RandomCardDeck class works the way you expect it to. The output is shown in Figure 5.11. Here is the source code: 159 C h a p t e r 5 B l a c k j a c k: O b j e c t- O r i e n t e d P r o g r a m m i n g JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 159 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. /* * RandomCardDeckTest * Tests the RandomCardDeck class */ public class RandomCardDeckTest { public static void main(String args[]) { RandomCardDeck deck = new RandomCardDeck(); System.out.println(“Deck list:”); deck.list(); Card card = deck.deal(); System.out.println(“Dealt “ + card); System.out.println(“Shuffling ”); deck.shuffle(); System.out.println(“Deck list:”); deck.list(); } } The Vector Class The java.util package contains many useful classes, including the Random and Vector classes. The Vector class implements a growable array of objects. It acts similarly to an array except that the size automatically grows and shrinks as objects are added and removed. It is of interest here because you use the Vector class to represent the dealer’s and the player’s hand in the BlackJack game. Table 5.1 contains some useful Vector methods. Remember that because of polymor- phism, any object can be passed to the methods that accept an Object instance. The VectorVictor application demonstrates simple use of the Vector class. The source code for VectorVictor.java is as follows: 160 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r FIGURE 5.11 The RandomCardDeck class represents a random deck of playing cards. JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 160 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. /* * VectorVictor * Demonstrates the Vector class */ import java.util.Vector; public class VectorVictor { public static void main(String args[]) { //initialize a vector of capacity 5 and increment 5 Vector v = new Vector(5, 5); System.out.println(“size, capacity”); System.out.println(v.size() + “, “ + v.capacity()); v.add(new String(“Fuzzy”)); System.out.println(v.size() + “, “ + v.capacity()); v.add(new String(“Wuzzy”)); System.out.println(v.size() + “, “ + v.capacity()); v.add(new String(“was”)); System.out.println(v.size() + “, “ + v.capacity()); v.add(new String(“a”)); System.out.println(v.size() + “, “ + v.capacity()); v.add(new String(“bear”)); System.out.println(v.size() + “, “ + v.capacity()); v.add(new String(“Fuzzy”)); System.out.println(v.size() + “, “ + v.capacity()); v.add(new String(“Wuzzy”)); System.out.println(v.size() + “, “ + v.capacity()); 161 C h a p t e r 5 B l a c k j a c k: O b j e c t- O r i e n t e d P r o g r a m m i n g Method Description void add(int index, Object element) Inserts element at the specified index, shifting the remaining indices by adding 1. boolean add(Object element) Appends element to the end; returns true. int capacity() Returns the capacity (current maximum size). void clear() Removes all elements. void copyInto(Object[] anArray) Copies the Vector’s elements into the given array. Object elementAt(int index) Returns the element at the given index. Object remove(int index) Removes and returns the object at the given index. boolean remove(Object element) Removes the first occurrence of element. void trimToSize() Sets the capacity of the Vector to its current size. TABLE 5.1 V ECTOR M ETHODS JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 161 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. v.add(new String(“had”)); System.out.println(v.size() + “, “ + v.capacity()); v.add(new String(“no”)); System.out.println(v.size() + “, “ + v.capacity()); v.add(new String(“hair”)); System.out.println(v.size() + “, “ + v.capacity()); v.add(new String(“Fuzzy”)); System.out.println(v.size() + “, “ + v.capacity()); v.add(new String(“Wuzzy”)); System.out.println(v.size() + “, “ + v.capacity()); v.add(new String(“wasn’t”)); System.out.println(v.size() + “, “ + v.capacity()); v.add(new String(“fuzzy”)); System.out.println(v.size() + “, “ + v.capacity()); v.add(new String(“was”)); System.out.println(v.size() + “, “ + v.capacity()); v.add(new String(“he”)); System.out.println(v.size() + “, “ + v.capacity()); v.trimToSize(); System.out.println(v.size() + “, “ + v.capacity()); //copy into an array String[] str = new String[v.size()]; v.copyInto(str); for (int s=0; s < str.length; s++) { System.out.print(str[s] + “ “); } } } The constructor accepts two int arguments. The first argument is the initial capacity of the Vector and the second one is the capacity increment. The capac- ity increment is the amount by which the capacity increases any time adding an element to the Vector causes the size to be greater than its capacity. As you can see in the output shown in Figure 5.12, anytime the size of the Vector exceeds 162 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r FIGURE 5.12 What’s your Vector, Victor? JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 162 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. the capacity, it grows by a factor of 5. This program also demonstrates copying the elements of the Vector into an array. The BlackJack Program Okay, here it is, the final project source code listing for BlackJack.java: /* * BlackJack * A simple BlackJack card game simulation */ import java.util.Vector; import java.io.*; public class BlackJack { protected RandomCardDeck deck; protected Vector dealerHand, playerHand; protected int dealerPoints, playerPoints; protected final static char HIT = ‘H’, STAND = ‘S’; public BlackJack() { Card card; dealerHand = new Vector(); playerHand = new Vector(); deck = new RandomCardDeck(); for (int c=0; c < deck.getNumCards(); c++) { card = deck.getCard(c); if (card.isPictureCard()) { card.setValue(10); } else if (card.getFaceValue() == Card.ACE) { card.setValue(1); } } } public static void main(String args[]) { BufferedReader reader; char input; BlackJack bj = new BlackJack(); reader = new BufferedReader(new InputStreamReader(System.in)); do { bj.play(); System.out.print(“Play Again (Y/N)? “); try { input = Character.toUpperCase(reader.readLine().charAt(0)); } catch (IOException ioe) { input = ‘?’; } } while (input == ‘Y’); } 163 C h a p t e r 5 B l a c k j a c k: O b j e c t- O r i e n t e d P r o g r a m m i n g JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 163 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 164 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r public void play() { int input; dealerHand.clear(); playerHand.clear(); if (deck.getNumCardsLeft() <= 15) { System.out.println(“Shuffling deck ”); deck.shuffle(); } deal(); output(); if (playerBlackJack()) { System.out.println(“Player has BlackJack!!!”); ((Card)dealerHand.elementAt(0)).setVisible(true); output(); if (dealerBlackJack()) { System.out.println(“Dealer also has BlackJack.”); System.out.println(“Game is a PUSH.”); } else { System.out.println(“Player wins!”); } } else if (dealerBlackJack()) { ((Card)dealerHand.elementAt(0)).setVisible(true); System.out.println(“Dealer has BlackJack.”); output(); System.out.println(“Player loses.”); } else { // hit or stand loop. do { try { input = getUserInput(); } catch (IOException ioe) { input = STAND; } if (input == HIT) { System.out.println(“Dealt “ + hitPlayer()); output(); } if (playerPoints > 21) { System.out.println(“Player BUSTS!”); ((Card)dealerHand.elementAt(0)).setVisible(true); output(); System.out.println(“Player loses.”); } } while (input == HIT && playerPoints < 21); if (playerPoints <= 21) { System.out.println(“Dealer’s turn”); ((Card)dealerHand.elementAt(0)).setVisible(true); output(); while (dealerPoints <= 16) { System.out.println(“Dealer gets “ + hitDealer()); output(); } JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 164 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. if (dealerPoints > 21) { System.out.println(“Dealer BUSTS!”); System.out.println(“Player wins!”); } else if (dealerBlackJack()) { System.out.println(“Dealer has BlackJack.”); System.out.println(“Player loses.”); } else if (dealerPoints == playerPoints) { System.out.println(“The game is a PUSH.”); } else if (dealerPoints > playerPoints) { System.out.println(“Player loses.”); } else { System.out.println(“Player wins!”); } } } } protected void deal() { Card card; playerHand.add(deck.deal()); playerHand.add(deck.deal()); card = deck.deal(); card.setVisible(false); dealerHand.add(card); dealerHand.add(deck.deal()); updatePoints(); } protected void output() { String d = “Dealer’s Hand: “; String p = “Your Hand : “; for (int c=0; c < dealerHand.size(); c++) { d += (Card)dealerHand.elementAt(c) + “ “; } for (int c=0; c < playerHand.size(); c++) { p += (Card)playerHand.elementAt(c) + “ “; } if (d.indexOf(‘?’) == -1) { d += “ (“ + dealerPoints +”)”; } p += “ (“ + playerPoints +”)”; System.out.println(d); System.out.println(p); } protected Card hitPlayer() { return hit(playerHand); } 165 C h a p t e r 5 B l a c k j a c k: O b j e c t- O r i e n t e d P r o g r a m m i n g JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 165 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. protected Card hitDealer() { return hit(dealerHand); } protected char getUserInput() throws IOException { BufferedReader reader; char input; reader = new BufferedReader(new InputStreamReader(System.in)); do { System.out.print(“(H)it or (S)tand? “); input = Character.toUpperCase(reader.readLine().charAt(0)); } while (input != HIT && input != STAND); return input; } protected boolean dealerBlackJack() { if (dealerHand.size() == 2) { if (((Card)dealerHand.elementAt(1)).getFaceValue() == Card.ACE || ((Card)dealerHand.elementAt(0)).getVisible()) { return dealerPoints == 21; } } return false; } protected boolean playerBlackJack() { if (playerHand.size() == 2) { return playerPoints == 21; } return false; } private Card hit(Vector hand) { Card card = deck.deal(); hand.add(card); updatePoints(); return card; } protected void updatePoints() { playerPoints = addUpPoints(playerHand); dealerPoints = addUpPoints(dealerHand); } private int addUpPoints(Vector hand) { int points = 0; int nAces = 0; Card[] cards = new Card[hand.size()]; hand.copyInto(cards); for (int c=0; c < cards.length; c++) { points += cards[c].getValue(); if (cards[c].getFaceValue() == Card.ACE) nAces++; } 166 J a v a P r o g r am m i n g f o r t h e A b s o l ut e B e gi n n e r JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 166 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. if (points <= 11 && nAces > 0) { points += 10; } return points; } } This class defines five instance variables. deck is a RandomCardDeck object that stores the deck of cards. dealerHand and playerHand are Vector objects that rep- resent the dealer’s cards and the player’s cards, respectively. dealerPoints and playerPoints store the dealer’s points and the player’s points. There are also two class variable char constants, HIT and STAND. They are used as flags that are com- pared to user input in order to determine whether the user wants to hit or stand. The constructor initializes the game for playing. The most notable thing in the game is where it calls the isPictureCard() method defined in the Card class. You do this so that you can set the value of all picture cards to 10. Also, it references the Card.ACE constant to find the Aces and set their initial values to 1. The main() method creates a new BlackJack instance and calls the play() method in a do-while loop. It continues to call play() while the users enter Y to continue playing. Aside from the main() method, play() is the only other public method. In this implementation of the BlackJack game, it is not necessary to give everyone access to any other method. You allow instances to only call the play() method, which defines one hand of blackjack. If you decide to create a new ver- sion of the game, you can always subclass or redefine the BlackJack class itself. The play() Method: BlackJack Driver The play() method is the driver for the game. First it clears the dealer’s and player’s hands. Then it shuffles the deck if the number of remaining cards is run- ning low ( 15 or fewer). It deals the cards by calling the deal() method, which basically deals the cards to the dealer (one face down and one face up) and the player (both cards visible). When it prints the cards, it calls the output() method, which builds a String representation of the hands. Note that when the Vector’s elementAt() method is called, the return value must be cast to a Card object in order to get its String representation: p += (Card)playerHand.elementAt(c) + “ “; If after the cards are dealt, either the dealer or the player has blackjack, the hand is over; otherwise the player is prompted to hit or stand. The getUserInput() method prompts the users. It will accept only an H or an S and will continue to prompt the user until it gets one of those responses. This method throws an IOException, which is caught back in play(), where if it does occur, in order to 167 C h a p t e r 5 B l a c k j a c k: O b j e c t- O r i e n t e d P r o g r a m m i n g JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 167 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... the Java AWT You can see the output for a typical session of the MadLib game in Figure 6.1 The java. awt Package Java s AWT package contains all the classes that are used for creating graphical user interfaces and for creating graphics and displaying images It contains classes used for implementing GUI components such as labels, buttons, text fields, and so on It also contains classes that are used for. .. this chapter, you learn how to create a GUI using Java s AWT package, java. awt TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark JavaProgAbsBeg-06.qxd 2/25/03 8:52 AM Page 172 Java Programming for the Absolute Beginner 172 The Java AWT components covered in this chapter are: • Frames • Lists • Labels • Scroll bars • Buttons... classes by importing them: import java. awt.*; This line imports (references, technically) any of the java. awt package classes used in your Java program, and no others Basically, it just signifies where the classes can be found—in the java. awt package Consult the API documentation for this package after you’ve learned the basics in this chapter for more detailed information Components Components are... throughout the rest of this book TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Creating a GUI Using the Abstract Windowing Toolkit FIGURE 6.1 JavaProgAbsBeg-06.qxd 2/25/03 8:52 AM Page 174 174 Java Programming for the Absolute Beginner HIN T All the components defined in the java. awt package are called heavyweight components... interface for interacting with the users A Graphical User Interface (GUI, pronounced “gooey”) is the official term for this type of interface In a GUI, graphics represent such components as text fields, buttons, menus, and so on, which the users interact with to pass information back and forth to the underlying program In this chapter, you learn how to create a GUI using Java s AWT package, java. awt... TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 169 169 Chapter 5 The player pushes a couple of games of BlackJack Summary This chapter is arguably one of the most important chapters to understand Java is an object-oriented programming language, and every Java program you write...JavaProgAbsBeg-05.qxd 2/25/03 8:51 AM Page 168 Java Programming for the Absolute Beginner 168 end the hand gracefully, sets the user response to BlackJack.STAND, ending the player’s turn Once the player’s turn is done, it moves on to the... scenes Java is still platform-independent because the JRE makes references to the API associated with the operating system it is installed over without requiring any difference in Java source code or recompilation You can create lightweight components that are not associated with a native peer by subclassing the Component class directly Java also includes a package, called “Swing” (specifically, javax.swing)... used for describing what the state of the Checkbox signifies • You use the Canvas component mainly to display graphics in a confined area TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Creating a GUI Using the Abstract Windowing Toolkit Description Chapter 6 Method JavaProgAbsBeg-06.qxd 2/25/03 8:52 AM Page 176 Java Programming. .. also supports graphics programming The Graphics class, defined in the java. awt package, has methods for drawing shapes, text, and images to the screen It is responsible for rendering the actual graphics that make up all the AWT’s GUI components It draws the lines that make a button look raised when it is not clicked and pressed when it is clicked You learn a bit about this aspect of Java s AWT in this . make up the Java AWT. You can see the output for a typical session of the MadLib game in Figure 6.1. The java. awt Package Java s AWT package contains all the classes that are used for creating. is, the final project source code listing for BlackJack .java: /* * BlackJack * A simple BlackJack card game simulation */ import java. util.Vector; import java. io.*; public class BlackJack { protected. the java. awt package classes used in your Java program, and no others. Basically, it just signifies where the classes can be found—in the java. awt package. Consult the API documentation for