// Using the String class and a first view of conditionals in java // You will need to consult the String class documentation to write this program. import javax.swing.JOptionPane; import uwcse.io.*; /** * This class simulates a simple game: The player enters letter by letter the * name of the capital of a US state.
* If the letter entered is correct, the game moves on to the next letter or * terminates if this was the last letter.
* If the letter entered is incorrect, the game asks for the same letter again.
* Input and output is done in dialog and message boxes.
* To play the game in BlueJ, create an instance of USStateCapitalTest and right * click on the object icon to call the method findNextLetter() */ public class USStateCapitalTest { // The state and its capital private String capital = "OLYMPIA"; private String state = "Washington"; // The answer from the user private String answer = "_______"; // same length as capital. Initially all // underscores private int position = 0; // Current position of the letter to find. // Note: character indices start at 0 in a String // Do we need a constructor? /** * If the current answer is incomplete,
* asks for the next letter in the name (via a dialog box).
* Tells the player if the entry is correct or incorrect.
* If correct, updates the current answer. *

* If the current answer is now complete, tells the player that the game is * over and return true, otherwise return false. */ public boolean findNextLetter() { // The answer is complete: the game is over // Hint: the answer is complete when the position of the next letter // to find is beyond the end of the answer (recall that positions in // a String starts at 0). // Use JOptionPane.showMessageDialog to display the message // Ask for and get the next letter // For the input Input input = new Input(); char letter; // to receive the input // Make the input upper case // use the method toUpperCase from the Character class // Is it the right character // (use the charAt method) if (letter == capital.charAt(position)) { // update the current answer // (use the substring method) // update the position of the letter to find // Message for the player: game over or move on to the next letter } else { // Tell the player that the input is incorrect } } /** * Starts the application * * @param args */ public static void main(String[] args) { USStateCapitalTest test = new USStateCapitalTest(); while (!test.findNextLetter()) { } } }