In: Computer Science
THIS IS JAVA PROGRAMMING
Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user to enter a capital for a state (by getting a state/capital pair via the StateCapitals class. Upon receiving the user’s input, the program reports whether the answer is correct.
The program should randomly select 10 out of the 50 states.
Modify your program so that it is guaranteed your program never asks the same state within one round of 10 guesses.
package module;
import java.util.*;
public class countryStates {
public static void main(String[] args) {
// TODO Auto-generated method
stub
System.out.println("Enter capitals
of states with their spaces");
System.out.println();
Random r = new Random();
Scanner scan =new
Scanner(System.in);
//Adding capitals and states to
dictionary
Dictionary<String, String>
dict = new Hashtable<String, String>();
dict.put("Alabama",
"Montgomery");
dict.put("Alaska", "Juneau");
dict.put("Arizona",
"Phoenix");
dict.put("Arkansas", "Little
Rock");
dict.put("California",
"Sacramento");
dict.put("Colorado",
"Denver");
dict.put("Connecticut",
"Hartford");
dict.put("Delaware",
"Dover");
dict.put("Florida",
"Tallahassee");
dict.put("Georgia",
"Atlanta");
dict.put("Hawaii",
"Honolulu");
dict.put("Idaho", "Boise");
dict.put("Illinois",
"Springfield");
dict.put("Indiana",
"Indianapolis");
dict.put("Iowa", "Des
Moines");
dict.put("Kansas", "Topeka");
dict.put("Kentucky",
"Frankfort");
dict.put("Louisiana", "Baton
Rouge");
dict.put("Maine", "Augusta");
dict.put("Maryland",
"Annapolis");
dict.put("Massachusetts",
"Boston");
dict.put("Michigan",
"Lansing");
dict.put("Minnesota", "St.
Paul");
dict.put("Mississippi",
"Jackson");
dict.put("Missouri", "Jefferson
City");
dict.put("Montana",
"Helena");
dict.put("Nebraska",
"Lincoln");
dict.put("Nevada", "Carson
City");
dict.put("New Hampshire",
"Concord");
dict.put("New Jersey",
"Trenton");
dict.put("New Mexico", "Santa
Fe");
dict.put("New York",
"Albany");
dict.put("North Carolina",
"Raleigh");
dict.put("North Dakota",
"Bismarck");
dict.put("Ohio", "Columbus");
dict.put("Oklahoma", "Oklahoma
City");
dict.put("Oregon", "Salem");
dict.put("Pennsylvania",
"Harrisburg");
dict.put("Rhode Island",
"Providence");
dict.put("South Carolina",
"Columbia");
dict.put("South Dakota",
"Pierre");
dict.put("Tennessee",
"Nashville");
dict.put("Texas", "Austin");
dict.put("Utah", "Salt Lake
City");
dict.put("Vermont",
"Montpelier");
dict.put("Virginia",
"Richmond");
dict.put("Washington",
"Olympia");
dict.put("West Virginia",
"Charleston");
dict.put("Wisconsin",
"Madison");
dict.put("Wyoming",
"Cheyenne");
String[] capitals = new
String[50];
int i = 0;
//Separating capitals from
dictionary
for (Enumeration k = dict.keys();
k.hasMoreElements();)
{
capitals[i] = (String) k.nextElement();
i = i+1;
}
//Looping to get 10 random
different elements
ArrayList<String> check = new
ArrayList<String>();
int j =1;
while(j<=10) {
String state =
capitals[r.nextInt(capitals.length)];
if(!check.contains(state)) {
check.add(state);
j = j+1;
System.out.print("Capital city of "+state+":
");
String capital = scan.nextLine();
if(dict.get(state).toLowerCase().equals(capital.strip().toLowerCase()))
{
System.out.println("Answer is
correct");
}
else {
System.out.println("Answer is
Incorrect");
}
System.out.println();
}
}
System.out.println("The program is
over");
}
}