Question

In: Computer Science

THIS IS JAVA PROGRAMMING Guessing the Capitals. Write a program Lab5.java that repeatedly prompts the user...

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.

Solutions

Expert Solution

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");
   }

}


Related Solutions

[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
JAVA Programming (Convert decimals to fractions) Write a program that prompts the user to enter a...
JAVA Programming (Convert decimals to fractions) Write a program that prompts the user to enter a decimal number and displays the number in a fraction. Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the Rational class in LiveExample 13.13 to obtain a rational number for the decimal number. Use the template at https://liveexample.pearsoncmg.com/test/Exercise13_19.txt for your code. Sample Run 1 Enter a decimal number: 3.25 The fraction number is...
Write a C program that repeatedly prompts the user for input at a simple prompt (see...
Write a C program that repeatedly prompts the user for input at a simple prompt (see the sample output below for details). Your program should parse the input and provide output that describes the input specified. To best explain, here's some sample output: ibrahim@ibrahim-latech:~$ ./prog1 $ ls -a -l -h Line read: ls -a -l -h Token(s): ls -a -l -h 4 token(s) read $ ls -alh Line read: ls -alh Token(s): ls -a -l -h 2 token(s) read $...
JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
Write a program in JAVA that prompts the user for a lower bound and an upper...
Write a program in JAVA that prompts the user for a lower bound and an upper bound. Use a loop to output all of the even integers within the range inputted by the user on a single line.
JAVA: Write a program that prompts the user to input a type of medication and the...
JAVA: Write a program that prompts the user to input a type of medication and the output will be a list of side effects that can occur from that medication.
Write a java program that prompts the user to see if they wish to encode or...
Write a java program that prompts the user to see if they wish to encode or decode a message. (a) If they choose to encode a message: i. Ask them to enter the message and store it in a String variable. ii. Once the message is entered, ask them for a “shift” integer, similar to Lab 6. iii. Using that shift, move all alphabetic characters in the message (i.e. excluding spaces and punctuation) using char and int arithmetic. Be sure...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code. Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code.   Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT