1. In Chapter 1, you created a class called RandomGuess. In this game, players guess a number, the application generates a random number, and players determine whether they were correct. Now that you can make decisions, modify the application so it allows a player to enter a guess before the random number is displayed, and then displays a message indicating whether the player’s guess was correct, too high, or too low. Save the file as RandomGuess2.java. (After you finish Chapter 6, you will be able to modify the application so that the user can continue to guess until the correct answer is entered.)

    import javax.swing.JOptionPane; public class RandomGuess2 { public static void main(String[] args) { int guess; int result; String msg; final int LOW = 1; final int HIGH = 10; result = LOW + (int)(Math.random() * HIGH); guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try to guess my number between " + LOW + " and " + HIGH)); if(guess == result) msg = "\nRight!"; else if(guess < result) msg = "\nYour guess was too low"; else msg = "\nYour guess was too high"; JOptionPane.showMessageDialog(null,"The number is " + result + msg); } }
  2. Create a lottery game application. Generate three random numbers (see Appendix D for help in doing so), each between 0 and 9. Allow the user to guess three numbers. Compare each of the user’s guesses to the three random numbers and display a message that includes the user’s guess, the randomly determined three-digit number, and the amount of money the user has won as follows: Matching Numbers Award ($) Any one matching 10 Two matching 100 Three matching, not in order 1,000 Three matching in exact order 1,000,000 No matches 0 Make certain that your application accommodates repeating digits. For example, if a user guesses 1, 2, and 3, and the randomly generated digits are 1, 1, and 1, do not give the user credit for three correct guesses—just one. Save the file as Lottery.java.

    // Lottery.java // Allow the user to guess three randomly chosen numbers // Display a message indicating the amount of // money the user has won as follows: // Any one matching $10 // Two matching $100 // Three matching not in order $1,000 // Three matching in exact order $1,000,000 // No matches $0 import javax.swing.*; public class Lottery { public static void main (String args[]) { final int HIGHEST_WIN = 1000000; final int SECOND_BEST_WIN = 1000; final int THIRD_BEST_WIN = 100; final int LOW_WIN = 10; String entry; int number1; int number2; int number3; int userGuess; int ran1, ran2, ran3; int random; int matches = 0; int winnings = 0; ran1 = (int)Math.floor(Math.random()*10); ran2 = (int)Math.floor(Math.random()*10); ran3 = (int)Math.floor(Math.random()*10); random = ran1 * 100 + ran2 * 10 + ran3; entry = JOptionPane.showInputDialog(null, "Enter first digit"); number1 = Integer.parseInt(entry); entry = JOptionPane.showInputDialog(null, "Enter second digit"); number2 = Integer.parseInt(entry); entry = JOptionPane.showInputDialog(null, "Enter last digit"); number3 = Integer.parseInt(entry); if(number1 == ran1 && number2 == ran2 && number3 == ran3) { matches = 4; } else { if(number1 == ran1) { ++matches; ran1 = 99; } else if(number1 == ran2) { ++matches; ran2 = 99; } else if(number1 == ran3) { ++matches; ran3 = 99; } if(number2 == ran1) { ++matches; ran1 = 99; } else if(number2 == ran2) { ++matches; ran2 = 99; } else if(number2 == ran3) { ++matches; ran3 = 99; } if(number3 == ran1) { ++matches; ran1 = 99; } else if(number3 == ran2) { ++matches; ran2 = 99; } else if(number3 == ran3) { ++matches; ran3 = 99; } } if(matches == 4) winnings = HIGHEST_WIN; else if(matches == 3) winnings = SECOND_BEST_WIN; else if(matches == 2) winnings = THIRD_BEST_WIN; else if(matches == 1) winnings = LOW_WIN; else winnings = 0; JOptionPane.showMessageDialog(null, "You guessed " + number1 + number2 + number3 + "\nWinning number was " + random + "\nYou have won $" + winnings + "!"); } }
  3. In Chapter 3, you created a Card class. Modify the Card class so the setValue() method does not allow a Card’s value to be less than 1 or higher than 13. If the argument to setValue() is out of range, assign 1 to the Card’s value. In Chapter 3, you also created a PickTwoCards application that randomly selects two playing cards and displays their values. In that application, all Card objects arbitrarily were assigned a suit represented by a single character, but they could have different values, and the player observed which of two Card objects had the higher value. Now, modify the application so both the suit and the value are chosen randomly. Using two Card objects, play a very simple version of the card game War. Deal two Cards—one for the computer and one for the player—and determine the higher card, then display a message indicating whether the cards are equal, the computer won, or the player won. (Playing cards are considered equal when they have the same value, no matter what their suit is.) For this game, assume the Ace (value 1) is low. Make sure that the two Cards dealt are not the same Card. For example, a deck cannot contain more than one Card representing the 2 of Spades. If two cards are chosen to have the same value, change the suit for one of them. Save the application as War.java. (After you study the chapter “Arrays,” you will be able to create a more sophisticated War game in which you use an entire deck without repeating cards.)

    public class War { 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 > HIGH_SUIT) yourSuit = 1; } if(mySuit == 1) myCard.setSuit('s'); else if(mySuit == 2) myCard.setSuit('h'); else if(mySuit == 3) myCard.setSuit('d'); else myCard.setSuit('c'); if(yourSuit == 1) yourCard.setSuit('s'); else if(yourSuit == 2) yourCard.setSuit('h'); else if(yourSuit == 3) yourCard.setSuit('d'); else yourCard.setSuit('c'); System.out.println("My card is the " + myCard.getValue() + " of " + myCard.getSuit()); System.out.println("Your card is the " + yourCard.getValue() + " 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"); } }
  4. In Chapter 4, you created a Die class from which you could instantiate an object containing a random value from 1 through 6. You also wrote an application that randomly “throws” two dice and displays their values. Modify the application so it determines whether the two dice are the same, the first has a higher value, or the second has a higher value. Save the application as TwoDice2.java

    public class TwoDice2 { public static void main(String[] args) { Die firstDie = new Die(); Die secondDie = new Die(); int value1 = firstDie.getValue(); int value2 = secondDie.getValue(); System.out.println("First die is " + value1); System.out.println("Second die is " + value2); if(value1 > value2) System.out.println("First is greater"); else if(value2 > value1) System.out.println("Second value is greater"); else System.out.println("The dice are equal"); } }
  5. In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows: • Rock beats scissors, because a rock can break a pair of scissors. • Scissors beats paper, because scissors can cut paper. • Paper beats rock, because a piece of paper can cover a rock. Create a game in which the computer randomly chooses rock, paper, or scissors. Let the user enter a number 1, 2, or 3, each representing one of the three choices. Then, determine the winner. Save the application as RockPaperScissors.java. (In the chapter “Characters, Strings, and the StringBuilder,” you will modify the game so that the user enters a string for rock, paper, and scissors, rather than just entering a number.)

    import javax.swing.JOptionPane; public class RockPaperScissors { public static void main(String[] args) { int user; int computer; String msg; String userPick; String computerPick; final int LOW = 1; final int HIGH = 3; computer = LOW + (int)(Math.random() * HIGH); user = Integer.parseInt(JOptionPane.showInputDialog(null, "Type one of the following numbers:\n1 -- Rock" + "\n2 - Paper\n3 -- Scissors")); if(user == 1) userPick = "rock"; else if(user == 2) userPick = "paper"; else userPick = "scissors"; if(computer == 1) computerPick = "rock"; else if(computer == 2) computerPick = "paper"; else computerPick = "scissors"; if(user == 1) if(computer == 1) msg = "tie"; else if(computer == 2) msg = "computer"; else msg = "you"; else if(user == 2) if(computer == 2) msg = "tie"; else if(computer == 3) msg = "computer"; else msg = "you"; else if(computer == 3) msg = "tie"; else if(computer == 1) msg = "computer"; else msg = "you"; JOptionPane.showMessageDialog(null,"You picked " + userPick + "\nComputer picked " + computerPick + "\nWinner: " + msg); } }