// 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.
*/
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?
// Answer no: All of the instance fields have been initialized on their
// line of declaration.
// In java, if there is no constructor in the class, a default (i.e. no
// input
// parameters) constructor that does nothing is automatically added to the
// class.
// That way, a user can instantiate the class.
/**
* 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() { // For the input Input input = new Input(); // 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 if (position >= capital.length()) { JOptionPane.showMessageDialog(null, "You have found all of the letters", "Game over", JOptionPane.INFORMATION_MESSAGE); return true; } // Ask for and get the next letter // Use JOptionPane.showInputDialog char letter; // to receive the input letter = input.readCharDialog("What is the capital of the state of " + state + "?\n" + answer + "\nEnter the next letter"); // Make the input upper case // use the method toUpperCase from the String class letter = Character.toUpperCase(letter); // Is it the right character // (use the charAt method) if (letter == capital.charAt(position)) { // update the current answer // (use the substring method) answer = answer.substring(0, position) + letter + answer.substring(position + 1); // update the position of the letter to find position = position + 1; // Message for the player: game over or move on to the next letter if (position >= capital.length()) { JOptionPane.showMessageDialog(null, "Congratulations, you have found all of the letters", "Game over", JOptionPane.INFORMATION_MESSAGE); return true; } else { JOptionPane.showMessageDialog(null, "Correct. Move on to the next letter", "Message", JOptionPane.INFORMATION_MESSAGE); } } else { // Wrong character JOptionPane.showMessageDialog(null, "Incorrect. Try again.", "Error", JOptionPane.ERROR_MESSAGE); } return false; } /** * Starts the application * * @param args */ public static void main(String[] args) { USStateCapitalTest test = new USStateCapitalTest(); while (!test.findNextLetter()) { } } }