Affordable web hosting - 594 Strings and Characters Chapter 10 SELF-REVIEW EXERCISES
Sunday, December 9th, 2007594 Strings and Characters Chapter 10 SELF-REVIEW EXERCISES 10.1 State whether each of the following is true or false. If false, explain why. a) When String objects are compared with ==, the result is true if the Strings contain the same values. b) A String can be modified after it is created. 10.2 For each of the following, write a single statement that performs the indicated task. a) Compare the string in s1 to the string in s2 for equality of contents. b) Append the string s2 to the string s1, using +=. c) Determine the length of the string in s1. ANSWERS TO SELF-REVIEW EXERCISES 10.1 a) False. String objects that are compared with operator == are actually compared to determine if they are the same object in memory. b) False. String objects are constant and cannot be modified after they are created. StringBufferobjects can be modified after they are created. 10.2 a) s1.equals( s2 ) b) s1 += s2; c) s1.length() EXERCISES Exercises 10.3 through 10.6 are reasonably challenging. Once you have done these problems, you ought to be able to implement most popular card games easily. 10.3 Modify the program in Fig. 10.21 so that the card-dealing method deals a five-card poker hand. Then write the following additional methods: a) Determine if the hand contains a pair. b) Determine if the hand contains two pairs. c) Determine if the hand contains three of a kind (e.g., three jacks). d) Determine if the hand contains four of a kind (e.g., four aces). e) Determine if the hand contains a flush (i.e., all five cards of the same suit). f) Determine if the hand contains a straight (i.e., five cards of consecutive face values). g) Determine if the hand contains a full house (i.e., two cards of one face value and three cards of another face value). 10.4 Use the methods developed in Exercise 10.3 to write a program that deals two five-card poker hands, evaluates each hand and determines which is the better hand. 10.5 Modify the program developed in Exercise 10.4 so that it can simulate the dealer. The dealer s five-card hand is dealt face down so the player cannot see it. The program should then evaluate the dealer s hand and, based on the quality of the hand, the dealer should draw one, two or three more cards to replace the corresponding number of unneeded cards in the original hand. The program should then reevaluate the dealer s hand. (Caution: This is a difficult problem!) 10.6 Modify the program developed in Exercise 10.5 so that it can handle the dealer s hand automatically, but the player is allowed to decide which cards of the player s hand to replace. The program should then evaluate both hands and determine who wins. Now, use this new program to play 20 games against the computer. Who wins more games, you or the computer? Have one of your friends play 20 games against the computer. Who wins more games? Based on the results of these games, make appropriate modifications to refine your poker-playing program. (This, too, is a difficult problem.) Play 20 more games. Does your modified program play a better game? Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01