rewrite to store the pair of states and capitals so that the questions are displ
ID: 3660935 • Letter: R
Question
rewrite to store the pair of states and capitals so that the questions are displayed randomly import java.util.*; import javax.swing.JOptionPane; import java.io.*; import javax.swing.JFrame; //class name public class CapitalGuess { //taken 50 states and their capitals static String[][] capitalStates = new String [50][2]; //main public static void main(String[] a) throws Exception { //declare variables int lineCount = 0; int correctAnswerCount = 0; //reads capitals and sates details in text file File sourceFile = new File("Guess.txt"); if(!sourceFile.exists()) { System.out.println("Source file does not exist. Program will exit"); System.exit(0); } Scanner sc = new Scanner(sourceFile); while(sc.hasNext()) { String s1 = sc.nextLine(); String[] temp = s1.split(","); //stores sate details in array capitalStates[lineCount][1] = temp[1]; lineCount++; } for(int i = 0; 1 < 10; i++) { //generates the name of the state randomly String stateName = getState(); String display = ""; String sName = JOptionPane.showInputDialog("What is the capital of " + stateName + " ?"); if(sName == null) { System.exit(0); } else { boolean isCorrect = checkAnswer(stateName, sName); if(!isCorrect) { //confirmation message display = "Your answer is correct"; //increments answer count correctAnswerCount++; } else //confirmation message display = "Your answer is incorrect"; JOptionPane.showMessageDialog(null,display, "message", 1); } } JFrame parent = new JFrame(); //displaying the right answer count JOptionPane.showMessageDialog(parent, "total correct answers: " + correctAnswerCount); System.exit(0); } //method for generating randomly the name of the state public static String getState() { Random random = new Random(); int randomValue = (int)(0 + Math.random() * 50); return capitalStates[randomValue][0]; } //method to check the answer public static boolean checkAnswer(String stateName, String sName) { for(int i = 0; i < 50; i++) { if(capitalStates[i][1].equals(stateName)) { if(capitalStates[i][1].equalsIgnoreCase(sName)) { return true; } } } return false; } } US States and capitals Alabama, Montgomery Alaska, Juneau Arizona, Phoenix Arkansas, Little Rock California, Sacramento Colorado, Denver Connecticut, Hartford Delaware, Dover Florida, Tallahassee Georgia, Atlanta Hawaii, Honolulu Idaho, Boise Illinois, Springfield Indiana, Indianapolis Iowa, Des Moines Kansas, Topeka Kentucky, Frankfort Louisiana, Baton Rouge Maine, Augusta Maryland, Annapolis Massachusetts, Boston Michigan, Lansing Minnesota, St. Paul Mississippi, Jackson Missouri, Jefferson City Montana, Helena Nebraska, Lincoln Nevada, Carson City New Hampshire, Concord New Jersey, Trenton New Mexico, Santa Fe New York, Albany North Carolina,Raleigh North Dakota, Bismarck Ohio, Columbus Oklahoma, Oklahoma City Oregon, Salem Pennsylvania, Harrisburg Rhode Island, Providence South Carolina,Columbia South Dakota, Pierre Tennessee, Nashville Texas, Austin Utah, Salt Lake City Vermont, Montpelier Virginia, Richmond Washington, Olympia West Virginia, Charleston Wisconsin, Madison Wyoming, CheyenneExplanation / Answer
import java.util.*; import javax.swing.JOptionPane; import java.io.*; import javax.swing.JFrame; public class CapitalGuess { // taken 50 states and their capitals // state is the key (state,capital) value pair map static HashMap capitalStates = new HashMap(); // main public static void main(String[] a) throws Exception { // declare variables int lineCount = 0; int correctAnswerCount = 0; // reads capitals and sates details in text file File sourceFile = new File("D:/Guess.txt"); if (!sourceFile.exists()) { System.out.println("Source file does not exist. Program will exit"); System.exit(0); } Scanner sc = new Scanner(sourceFile); while (sc.hasNext()) { String s1 = sc.nextLine(); String[] temp = s1.split(","); // stores sate details // state is the key capitalStates.put(temp[0], temp[1]); } for (int i = 0; 1 < 10; i++) { // generates the name of the state randomly String stateName = getState(); String display = ""; String sName = JOptionPane .showInputDialog("What is the capital of " + stateName + " ?"); if (sName == null) { System.exit(0); } else { boolean isCorrect = checkAnswer(stateName, sName); if (!isCorrect) { // confirmation message display = "Your answer is correct"; // increments answer count correctAnswerCount++; } else // confirmation message display = "Your answer is incorrect"; JOptionPane.showMessageDialog(null, display, "message", 1); } } } // method for generating randomly the name of the state public static String getState() { Random random = new Random(); int randomValue = (int) (0 + Math.random() * 3); // key is the state name, keySet contians array of states return (String) capitalStates.keySet().toArray()[randomValue]; } // method to check the answer public static boolean checkAnswer(String stateName, String sName) { if (capitalStates.containsKey(stateName) && capitalStates.get(stateName).equalsIgnoreCase(sName)) return true; else return false; } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.