1. In Chapter 3, you designed a Card class. The class holds fields that contain a Card’s value and suit. Currently, the suit is represented by a single character (s, h, d, or c). Modify the class so that the suit is a string (Spades, Hearts, Diamonds, or Clubs). Also, add a new field to the class to hold the string representation of a Card’s rank based on its value. Within the Card class setValue() method, besides setting the numeric value, also set the string rank value as follows. Numeric Value String Value for Rank 1 Ace 2 through 10 2 through 10 11 Jack 12 Queen 13 King
      public class Card { private String suit; private int value; private String rank; public String getSuit() { return suit; } public int getValue() { return value; } public String getRank() { return rank; } public void setSuit(String s) { suit = s; } public void setValue(int v) { final int LOW = 1; final int HIGH = 13; if(v >= LOW && v <= HIGH) value = v; else value = LOW; if(value == 1) rank = "Ace"; else if(value == 11) rank ="Jack"; else if(value == 12) rank = "Queen"; else if(value == 13) rank = "King"; else rank = Integer.toString(value); } }
    2. In Chapter 5, you created a War Card game that randomly selects two cards (one for the player and one for the computer) and declares a winner (or a tie). Modify the game to set each Card’s suit as the appropriate string, then execute the game using the newly modified Card class. Figure 7-18 shows a typical execution. Recall that in this version of War, you assume that the Ace is the lowest-valued card. Save the game as War2.java.

      public class War2 { public static void main(String[] args) { int myValue, mySuit; int yourValue, yourSuit; final int HIGH = 13; final int LOW = 1; final int HIGH_SUIT = 4; Card myCard = new Card(); Card yourCard = new Card(); myValue = ((int)(Math.random() * 100) % HIGH + LOW); yourValue = ((int)(Math.random() * 100) % HIGH + LOW); myCard.setValue(myValue); yourCard.setValue(yourValue); mySuit = ((int)(Math.random() * 100) % HIGH_SUIT + LOW); yourSuit = ((int)(Math.random() * 100) % HIGH_SUIT + LOW); if(myValue == yourValue && mySuit == yourSuit) { yourSuit = yourSuit + 1; if(yourSuit > 4) yourSuit = 1; } if(mySuit == 1) myCard.setSuit("Spades"); else if(mySuit == 2) myCard.setSuit("Hearts"); else if(mySuit == 3) myCard.setSuit("Diamonds"); else myCard.setSuit("Clubs"); if(yourSuit == 1) yourCard.setSuit("Spades"); else if(yourSuit == 2) yourCard.setSuit("Hearts"); else if(yourSuit == 3) yourCard.setSuit("Diamonds"); else yourCard.setSuit("Clubs"); System.out.println("My card is the " + myCard.getRank() + " of " + myCard.getSuit()); System.out.println("Your card is the " + yourCard.getRank() + " of " + yourCard.getSuit()); if(myValue == yourValue) System.out.println("It's a tie"); else if(myValue > yourValue) System.out.println("I win"); else System.out.println("You win"); } }
  1. In Chapter 5, you created a Rock Paper Scissors game. In the game, a player entered a number to represent one of the three choices. Make the following improvements to the game: • Allow the user to enter a string (rock, paper, or scissors) instead of a digit. • Make sure the game works correctly, whether the player enters a choice in uppercase or lowercase letters, or a combination of the two. • To allow for player misspellings, accept the player’s entry as long as the first two letters are correct. (In other words, if a player types scixxrs, you will accept it as scissors because at least the first two letters are correct.) • When the player does not type at least the first two letters of the choice correctly, reprompt the player and continue to do so until the player’s entry contains at least the first two letters of one of the options. • Allow 10 complete rounds of the game. At the end, display counts of the number of times the player won, the number of times the computer won, and the number of tie games. Save the file as RockPaperScissors2.java.
    import javax.swing.JOptionPane; public class RockPaperScissors2 { public static void main(String[] args) { String userPick =""; int computer; String msg = ""; String instructions = "Type one of the following :\nRock\nPaper\nScissors"; String computerPick; final int LOW = 1; final int HIGH = 3; final int NUM_GAMES = 10; int gameCount = 0; int playerWon = 0; int tieGames = 0; while(gameCount < NUM_GAMES) { userPick = ""; msg = ""; computer = LOW + (int)(Math.random() * HIGH); while(!userPick.equals("rock") && !userPick.equals("paper") && !userPick.equals("scissors")) { userPick = JOptionPane.showInputDialog(null, msg + instructions); userPick = userPick.toLowerCase(); if(userPick.startsWith("ro")) userPick = "rock"; else if(userPick.startsWith("pa")) userPick = "paper"; else if(userPick.startsWith("sc")) userPick = "scissors"; msg = "You must pick rock, paper or scissors\n"; } if(computer == 1) computerPick = "rock"; else if(computer == 2) computerPick = "paper"; else computerPick = "scissors"; if(userPick.equals(computerPick)) { msg = "tie"; ++tieGames; } else if(userPick.equals("rock")) if(computerPick.equals("paper")) msg = "computer"; else { msg = "you"; ++playerWon; } else if(userPick.equals("paper")) if(computerPick.equals("scissors")) msg = "computer"; else { msg = "you"; ++playerWon; } else if(computerPick.equals("rock")) msg = "computer"; else { msg = "you"; ++playerWon; } JOptionPane.showMessageDialog(null,"You picked " + userPick + "\nComputer picked " + computerPick + "\nWinner: " + msg); ++gameCount; } JOptionPane.showMessageDialog(null, "End of " + NUM_GAMES + " games\nYou won " + playerWon + " games\nComputer won " + (NUM_GAMES - playerWon - tieGames) + " games\nThere were " + tieGames + " tie games");; } }
  2. Create a simple guessing game, similar to Hangman, in which the user guesses letters and then attempts to guess a partially hidden phrase. Display a phrase in which some of the letters are replaced by asterisks: for example, G* T*** (for Go Team). Each time the user guesses a letter, either place the letter in the correct spot (or spots) in the phrase and display it again or tell the user the guessed letter is not in the phrase. Display a congratulatory message when the entire correct phrase has been deduced. Save the game as SecretPhrase.java. In the next chapter, you will modify this program so that instead of presenting the user with the same phrase every time the game is played, the program randomly selects the phrase from a list of phrases.

    import javax.swing.*; public class SecretPhrase { public static void main(String[] args) { String targetPhrase = "Hello There"; String displayPhrase = "H**** T****"; String buildDisplayPhrase; String prompt; String play = "Play our game - guess the phrase\n"; String enterOne = "Enter one letter\n"; String sorry = "Sorry - not in the phrase: "; String correct = "Correct! "; String userResponse; char guess; char letter; boolean found = false; int position; int oldPos = 0; final int LEN = targetPhrase.length(); prompt = play + enterOne; while(displayPhrase.indexOf('*') != -1) { userResponse = JOptionPane.showInputDialog(null, prompt + displayPhrase); guess = userResponse.charAt(0); found = false; for(position = 0; position < LEN; ++position) { letter = targetPhrase.charAt(position); if(letter == guess) { displayPhrase = displayPhrase.substring(0, position) + guess + displayPhrase.substring(position + 1, LEN); found = true; } } if(!found) { prompt = sorry + guess + '\n'; } else { prompt = correct + enterOne; } } JOptionPane.showMessageDialog(null, "Congratulations!\nThe phrase was:\n" + targetPhrase); } }
  3. Eliza is a famous 1966 computer program written by Joseph Weizenbaum. It imitates a psychologist (more specifically, a Rogerian therapist) by rephrasing many of a patient’s statements as questions and posing them to the patient. This type of therapy (sometimes called nondirectional) is often parodied in movies and television shows, in which the therapist does not even have to listen to the patient, but gives “canned” responses that lead the patient from statement to statement. For example, when the patient says, I am having trouble with my brother, the therapist might say, Tell me more about your brother. If the patient says, I dislike school, the therapist might say, Why do you say you dislike school? Eliza became a milestone in the history of computers because it was the first time a computer programmer attempted to create the illusion of human-to-human interaction. Create a simple version of Eliza by allowing the user to enter statements continually until the user quits by typing Goodbye. After each statement, have the computer make one of the following responses: • If the user entered the word my (for example, I am having trouble with my brother), respond with Tell me more about your and insert the noun in question—for example, Tell me more about your brother. When you search for a word in the user’s entry, make sure it is the entire word and not just letters within another word. For example, when searching for my, make sure it is not part of another word such as dummy or mystic. • If the user entered a strong word, such as love or hate, respond with, You seem to have strong feelings about that. • Add a few other appropriate responses of your choosing. • In the absence of any of the preceding inputs, respond with a random phrase from the following: Please go on, Tell me more, or Continue. Save the file as Eliza.java.
    import javax.swing.*; public class Eliza { public static void main(String[] args) { String user; String msg = ""; String msg1 = "Tell me more about your "; String msg2 = "You seem to have strong feelings about that"; String msg3 = "Please go on"; String msg4 = "Tell me more"; String msg5 = "Continue"; int random; int length; int x, y; int stopWord; final String END = "Goodbye"; boolean foundMy = false, foundLove = false, foundHate = false; user = JOptionPane.showInputDialog(null, "Hello. I am Eliza, your tharapist. Tell me what" + "\nis troubling you. Any time you want to quit," + "\njust type \"Goodbye\""); while(!user.equalsIgnoreCase(END)) { user = user.toLowerCase(); length = user.length(); foundMy = false; for(x = 0; x < length - 3; ++x) // If "my" is at end of sentence, no need to go further { if(user.charAt(x) == 'm') { if(user.charAt(x + 1) == 'y') if(user.charAt(x + 2) == ' ') if(x >= 1) { if(user.charAt(x - 1) == ' ') foundMy = true; } else foundMy = true; } if(foundMy) { stopWord = length; for(y = x + 3; y < length; ++y) { if(user.charAt(y) == ' ' || user.charAt(y) == '.' || user.charAt(y) == ',') stopWord = y; } msg = msg1 + user.substring(x + 3, stopWord); x = length; // do not look for possible second "my" } } if(!foundMy) { foundLove = false; for(x = 0; x < length - 5; ++x) { if(user.charAt(x) == 'l') { if(user.charAt(x + 1) == 'o') if(user.charAt(x + 2) == 'v') if(user.charAt(x + 3) == 'e') if(user.charAt(x + 4) == ' ') if(x >= 1) { if(user.charAt(x - 1) == ' ') foundLove = true; } else foundLove = true; } if(foundLove) { msg = msg2; x = length; // do not look for possible second "love" } } } if(!foundLove && !foundMy) { foundHate = false; for(x = 0; x < length - 5; ++x) { if(user.charAt(x) == 'h') { if(user.charAt(x + 1) == 'a') if(user.charAt(x + 2) == 't') if(user.charAt(x + 3) == 'e') if(user.charAt(x + 4) == ' ') if(x >= 1) { if(user.charAt(x - 1) == ' ') foundHate = true; } else foundHate = true; } if(foundHate) { msg = msg2; x = length; // do not look for possible second "hate" } } } if(!foundHate && !foundLove && !foundMy) { random = (int) (Math.random() * 100) % 3; if(random == 0) msg = msg3; else if(random == 1) msg = msg4; else msg = msg5; } user = JOptionPane.showInputDialog(null, msg); } JOptionPane.showMessageDialog(null, END); } }