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

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...
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 input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write an assembly language program that repeatedly prompts the user to enter signed decimal integer numbers....
Write an assembly language program that repeatedly prompts the user to enter signed decimal integer numbers. The program should be run from the command prompt, output a text prompt to the screen, and then wait for the user to type in a number followed by the Enter key. (The legitimate range of user input values is any signed integer that can be represented in 32 bits.) After each number is entered, the program should determine and display the following information...
Write a JAVA program that prompts the user for the number of names they’d like to...
Write a JAVA program that prompts the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT