1. Write an application that creates a quiz. The quiz should contain at least five questions about a hobby, popular music, astronomy, or any other personal interest. Each question should be a multiple-choice question with at least four answer options. When the user answers the question correctly, display a congratulatory message. If the user responds to a question incorrectly, display an appropriate message as well as the correct answer. At the end of the quiz, display the number of correct and incorrect answers, and the percentage of correct answers. Save the file as Quiz.java.

      import javax.swing.*; public class Quiz { public static void main (String args[]) { int answer, totalAsked = 0, correct = 0; String entry; String q1 = "Who is buried in Grant's Tomb?\n"; String q2 = "Who wrote Beethoven's 5th Symphony?\n"; String q3 = "Who is honored by the Lincoln Memorial?\n"; String q4 = "Who discovered the Fibonacci Sequence?\n"; String q5 = "Who discovered Haley's Comet?\n"; String a1 = "Grant", a2 = "Beethoven", a3 = "Lincoln", a4 = "Fibonacci", a5 = "Haley"; String answerList = "(1) " + a1 + "\n" + "(2) " + a2 + "\n" + "(3) " + a3 + "\n" + "(4) " + a4 + "\n" + "(5) " + a5 + "\n" + "(6) " + "All of the above" + "\n"; entry = JOptionPane.showInputDialog(null, q1 + answerList); answer = Integer.parseInt(entry); while(answer < 1 || answer > 6) { entry = JOptionPane.showInputDialog(null, "Sorry - answer must be a number 1 through 6\n" + q1 + answerList); answer = Integer.parseInt(entry); } ++totalAsked; if(answer != 1) { JOptionPane.showMessageDialog(null, "Sorry - answer was " + a1); } else { ++ correct; JOptionPane.showMessageDialog(null, "Good job!"); } //-------------------------- entry = JOptionPane.showInputDialog(null, q2 + answerList); answer = Integer.parseInt(entry); while(answer < 1 || answer > 6) { entry = JOptionPane.showInputDialog(null, "Sorry - answer must be a number 1 through 6\n" + q2 + answerList); answer = Integer.parseInt(entry); } ++totalAsked; if(answer != 2) { JOptionPane.showMessageDialog(null, "Sorry - answer was " + a2); } else { ++ correct; JOptionPane.showMessageDialog(null, "Good job!"); } //-------------------------- entry = JOptionPane.showInputDialog(null, q3 + answerList); answer = Integer.parseInt(entry); while(answer < 1 || answer > 6) { entry = JOptionPane.showInputDialog(null, "Sorry - answer must be a number 1 through 6\n" + q3 + answerList); answer = Integer.parseInt(entry); } ++totalAsked; if(answer != 3) { JOptionPane.showMessageDialog(null, "Sorry - answer was " + a3); } else { ++ correct; JOptionPane.showMessageDialog(null, "Good job!"); } //-------------------------- entry = JOptionPane.showInputDialog(null, q4 + answerList); answer = Integer.parseInt(entry); while(answer < 1 || answer > 6) { entry = JOptionPane.showInputDialog(null, "Sorry - answer must be a number 1 through 6\n" + q4 + answerList); answer = Integer.parseInt(entry); } ++totalAsked; if(answer != 4) { JOptionPane.showMessageDialog(null, "Sorry - answer was " + a4); } else { ++ correct; JOptionPane.showMessageDialog(null, "Good job!"); } //-------------------------- entry = JOptionPane.showInputDialog(null, q5 + answerList); answer = Integer.parseInt(entry); while(answer < 1 || answer > 6) { entry = JOptionPane.showInputDialog(null, "Sorry - answer must be a number 1 through 6\n" + q5 + answerList); answer = Integer.parseInt(entry); } ++totalAsked; if(answer != 5) { JOptionPane.showMessageDialog(null, "Sorry - answer was " + a5); } else { ++ correct; JOptionPane.showMessageDialog(null, "Good job!"); } //-------------------------- JOptionPane.showMessageDialog(null, "You got " + correct + " Right out of " + totalAsked + "\nScore = " + (correct * 100 / totalAsked) +"%"); } }
    2. Modify the Quiz application so that the user is presented with each question continually until it is answered correctly. Remove the calculation for percentage of correct answers—all users will have 100% correct by the time they complete the application. Save the file as Quiz2.java.

      import javax.swing.*; public class Quiz2 { public static void main (String args[]) { int answer; String entry; String q1 = "Who is buried in Grant's Tomb?\n"; String q2 = "Who wrote Beethoven's 5th Symphony?\n"; String q3 = "Who is honored by the Lincoln Memorial?\n"; String q4 = "Who discovered the Fibonacci Sequence?\n"; String q5 = "Who discovered Haley's Comet?\n"; String a1 = "Grant", a2 = "Beethoven", a3 = "Lincoln", a4 = "Fibonacci", a5 = "Haley"; String answerList = "(1) " + a1 + "\n" + "(2) " + a2 + "\n" + "(3) " + a3 + "\n" + "(4) " + a4 + "\n" + "(5) " + a5 + "\n" + "(6) " + "All of the above" + "\n"; entry = JOptionPane.showInputDialog(null, q1 + answerList); answer = Integer.parseInt(entry); while(answer != 1) { entry = JOptionPane.showInputDialog(null, "Try again\n" + q1 + answerList); answer = Integer.parseInt(entry); } JOptionPane.showMessageDialog(null, "Good job!"); //-------------------------- entry = JOptionPane.showInputDialog(null, q2 + answerList); answer = Integer.parseInt(entry); while(answer != 2) { entry = JOptionPane.showInputDialog(null, "Try again\n" + q2 + answerList); answer = Integer.parseInt(entry); } JOptionPane.showMessageDialog(null, "Good job!"); //-------------------------- entry = JOptionPane.showInputDialog(null, q3 + answerList); answer = Integer.parseInt(entry); while(answer != 3) { entry = JOptionPane.showInputDialog(null, "Try again\n" + q3 + answerList); answer = Integer.parseInt(entry); } JOptionPane.showMessageDialog(null, "Good job!"); //-------------------------- entry = JOptionPane.showInputDialog(null, q4 + answerList); answer = Integer.parseInt(entry); while(answer != 4) { entry = JOptionPane.showInputDialog(null, "Try again\n" + q4 + answerList); answer = Integer.parseInt(entry); } JOptionPane.showMessageDialog(null, "Good job!"); //-------------------------- entry = JOptionPane.showInputDialog(null, q5 + answerList); answer = Integer.parseInt(entry); while(answer != 5) { entry = JOptionPane.showInputDialog(null, "Try again\n" + q5 + answerList); answer = Integer.parseInt(entry); } JOptionPane.showMessageDialog(null, "Good job!"); //-------------------------- } }
  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. In Chapter 5, you improved the application to display a message indicating whether the player’s guess was correct, too high, or too low. Now, add a loop that continuously prompts the user for the number, indicating whether the guess is high or low, until the user enters the correct value. After the user correctly guesses the number, display a count of the number of attempts it took. Save the file as RandomGuess3.java.

    import javax.swing.JOptionPane; public class RandomGuess3 { public static void main(String[] args) { int guess; int result; String msg; int attempts = 1; 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)); while(guess != result) { if(guess < result) msg = "Your guess was too low"; else msg = "Your guess was too high"; guess = Integer.parseInt(JOptionPane.showInputDialog(null, msg + "\nGuess again")); ++attempts; } JOptionPane.showMessageDialog(null,"Correct! You got it in " + attempts + " attempts"); } }
  2. In Chapter 4, you created a Die class from which you could instantiate an object containing a random value from 1 through 6. Now use the class to create a simple dice game in which the user chooses a number between 2 (the lowest total possible from two dice) and 12 (the highest total possible). The user “rolls” two dice up to three times. If the number chosen by the user comes up, the user wins and the game ends. If the number does not come up within three rolls, the computer wins. Save the application as TwoDice3.java.

    import javax.swing.*; public class TwoDice3 { public static void main(String[] args) { final int HIGH = 12; final int LOW = 2; int guess = 0; int total = 0; int attempts = 0; boolean isDone = false; final int MOST_ATTEMPTS = 3; String msg; while(guess < LOW || guess > HIGH) { guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Choose a number between " + LOW + " and " + HIGH)); } while(!isDone) { Die firstDie = new Die(); Die secondDie = new Die(); int value1 = firstDie.getValue(); int value2 = secondDie.getValue(); total = value1 + value2; attempts = attempts + 1; msg = "Roll " + attempts + ": " + value1 + " and " + value2 + "\nTotal: " + total; if(guess == total) { JOptionPane.showMessageDialog(null, msg + "\nYou win!"); isDone = true; } else { if(attempts == MOST_ATTEMPTS) { JOptionPane.showMessageDialog(null, msg + "\nLast try - you lose"); isDone = true; } else JOptionPane.showMessageDialog(null, msg + "\nClick OK to roll again"); } } } }
    1. a. Using the Die class you created in Chapter 4, create a version of the dice game Pig that a user can play against the computer. The object of the game is to be the first to score 100 points. The user and computer take turns rolling a pair of dice following these rules: • On a turn, each player “rolls” two dice. If no 1 appears, the dice values are added to a running total, and the player can choose whether to roll again or pass the turn to the other player. • If a 1 appears on one of the dice, nothing more is added to the player’s total and it becomes the other player’s turn. • If a 1 appears on both of the dice, not only is the player’s turn over, but the player’s entire accumulated score is reset to 0. • In this version of the game, when the computer does not roll a 1 and can choose whether to roll again, generate a random value between 0 and 1. Have the computer continue to roll the dice when the value is 0.5 or more, and have the computer quit and pass the turn to the player when the value is not 0.5 or more. Save the game as PigDiceGame.java.

      import javax.swing.*; public class PigDiceGame { public static void main(String[] args) { final int WINNING_SCORE = 100; int computerTotal = 0; int playerTotal = 0; int selection = JOptionPane.YES_OPTION;; boolean isDone = false; boolean computerChoosesToGoOn = true; String msg; String msg2 = "\nDo you want to roll the dice?"; while(!isDone) { while(selection == JOptionPane.YES_OPTION) { Die firstDie = new Die(); Die secondDie = new Die(); int value1 = firstDie.getValue(); int value2 = secondDie.getValue(); if(value1 == 1 && value2 == 1) { playerTotal = 0; selection = JOptionPane.NO_OPTION; msg2 = "\nYou rolled two 1s and cannot go on"; } else if(value1 != 1 && value2 !=1) { playerTotal = playerTotal + value1 + value2; msg2 = "\nYou can go on"; } else { selection = JOptionPane.NO_OPTION; msg2 = "\nYou rolled a 1 and cannot go on"; } if(playerTotal >= WINNING_SCORE) { msg2 = "\nYou win!"; isDone = true; computerChoosesToGoOn = false; selection = JOptionPane.NO_OPTION; } else { computerChoosesToGoOn = true; } JOptionPane.showMessageDialog(null, "Your dice are " + value1 + " and " + value2 + "\nYour total is " + playerTotal + msg2); if(selection == JOptionPane.YES_OPTION) selection = JOptionPane.showConfirmDialog(null, "\n(Your total is " + playerTotal + ")" + "\nDo you want to roll the dice?"); } selection = JOptionPane.YES_OPTION; while(computerChoosesToGoOn) { msg = "\nThe computer chooses to roll"; Die firstDie = new Die(); Die secondDie = new Die(); int value1 = firstDie.getValue(); int value2 = secondDie.getValue(); if(value1 == 1 && value2 == 1) { computerTotal = 0; computerChoosesToGoOn = false; msg = "\nComputer rolled two 1's and cannot go on"; } else if(value1 != 1 && value2 !=1) computerTotal = computerTotal + value1 + value2; else { computerChoosesToGoOn = false; msg = "\nComputer rolled a 1 and cannot go on"; } if(computerTotal >= WINNING_SCORE) { msg = "\nComputer wins!"; isDone = true; } else { if(computerChoosesToGoOn) { double compChoice = Math.random(); computerChoosesToGoOn = (compChoice >= 0.50); if(!computerChoosesToGoOn) msg = "\nThe computer chooses to not roll again"; } } JOptionPane.showMessageDialog(null, "The computer's dice are " + value1 + " and " + value2 + "\nThe computer total is " + computerTotal + msg); } } } }
    2. Modify the PigDiceGame application so that if a player rolls a 1, not only does the player’s turn end, but all the player’s earned points during that round are eliminated. (Points from earlier rounds are not affected. That is, when either the player or computer rolls a 1, all the points accumulated since the other’s turn are subtracted.) Save the game as PigDiceGame2.java.

      import javax.swing.*; public class PigDiceGame2 { public static void main(String[] args) { final int WINNING_SCORE = 100; int computerTotal = 0; int playerTotal = 0; int selection = JOptionPane.YES_OPTION;; boolean isDone = false; boolean computerChoosesToGoOn = true; String msg; String msg2 = "\nDo you want to roll the dice?"; int pointsThisRoundPlayer = 0, pointsThisRoundComputer = 0; while(!isDone) { while(selection == JOptionPane.YES_OPTION) { Die firstDie = new Die(); Die secondDie = new Die(); int value1 = firstDie.getValue(); int value2 = secondDie.getValue(); if(value1 == 1 && value2 == 1) { playerTotal = 0; pointsThisRoundPlayer = 0; selection = JOptionPane.NO_OPTION; msg2 = "\nYou rolled two 1s and cannot go on"; } else if(value1 != 1 && value2 !=1) { playerTotal = playerTotal + value1 + value2; pointsThisRoundPlayer = pointsThisRoundPlayer + value1 + value2; msg2 = "\nYou can go on"; } else { selection = JOptionPane.NO_OPTION; msg2 = "\nYou rolled a 1 and cannot go on\nSubtracting this round's " + pointsThisRoundPlayer + " points"; playerTotal = playerTotal - pointsThisRoundPlayer; pointsThisRoundPlayer = 0; } if(playerTotal >= WINNING_SCORE) { msg2 = "\nYou win!"; isDone = true; computerChoosesToGoOn = false; selection = JOptionPane.NO_OPTION; } else { computerChoosesToGoOn = true; } JOptionPane.showMessageDialog(null, "Your dice are " + value1 + " and " + value2 + "\nYour total is " + playerTotal + msg2); if(selection == JOptionPane.YES_OPTION) selection = JOptionPane.showConfirmDialog(null, "\n(Your total is " + playerTotal + ")" + "\nDo you want to roll the dice?"); } pointsThisRoundPlayer = 0; selection = JOptionPane.YES_OPTION; while(computerChoosesToGoOn) { msg = "\nThe computer chooses to roll"; Die firstDie = new Die(); Die secondDie = new Die(); int value1 = firstDie.getValue(); int value2 = secondDie.getValue(); if(value1 == 1 && value2 == 1) { computerTotal = 0; computerChoosesToGoOn = false; msg = "\nComputer rolled two 1's and cannot go on"; } else if(value1 != 1 && value2 !=1) { computerTotal = computerTotal + value1 + value2; pointsThisRoundComputer = pointsThisRoundComputer + value1 + value2; } else { computerChoosesToGoOn = false; computerTotal = computerTotal - pointsThisRoundComputer; msg = "\nComputer rolled a 1 and cannot go on" + "\nSubtracting " + pointsThisRoundComputer + " points earned this round"; pointsThisRoundComputer = 0; } if(computerTotal >= WINNING_SCORE) { msg = "\nComputer wins!"; computerChoosesToGoOn = false; isDone = true; } else { if(computerChoosesToGoOn) { double compChoice = Math.random(); computerChoosesToGoOn = (compChoice >= 0.50); if(!computerChoosesToGoOn) { msg = "\nThe computer chooses to not roll again"; pointsThisRoundComputer = 0; } } } JOptionPane.showMessageDialog(null, "The computer's dice are " + value1 + " and " + value2 + "\nThe computer total is " + computerTotal + msg); } pointsThisRoundComputer = 0; } } }
  3. Two people play the game of Count 21 by taking turns entering a 1, 2, or 3, which is added to a running total. The player who adds the value that makes the total reach or exceed 21 loses the game. Create a game of Count 21 in which a player competes against the computer, and program a strategy that always allows the computer to win. On any turn, if the player enters a value other than 1, 2, or 3, force the player to reenter the value. Save the game as Count21.java.

    import javax.swing.JOptionPane; public class Count21 { public static void main(String[] args) { int player; int computer; String msg; int total = 0; final int FACTOR = 4; final int HIGH = 3; final int LOSING_VAL = 21; final String inputMsg = "\nInput a number between 1 and " + HIGH; player = Integer.parseInt(JOptionPane.showInputDialog(null, inputMsg)); while(player < 1 || player > HIGH) player = Integer.parseInt(JOptionPane.showInputDialog(null, "Number must be between 1 and " + HIGH + "\nPlease reenter")); total += player; while(total < LOSING_VAL) { msg = "You entered " + player + " bringing the total to " + total; if(player == 1) computer = 3; else if(player == 2) computer = 2; else computer = 1; total += computer; msg += "\nComputer adds " + computer + " bringing total to " + total; msg += inputMsg; player = Integer.parseInt(JOptionPane.showInputDialog(null, msg)); while(player < 1 || player > HIGH) player = Integer.parseInt(JOptionPane.showInputDialog(null, "Number must be between 1 and " + HIGH + "\nPlease reenter")); total += player; } JOptionPane.showMessageDialog(null,"You brought the total to " + total + "\nSorry - you lose."); } }