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 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.
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.
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
java language NetBeans Write a program that prompts the user to enter the weight of a...
java language NetBeans Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
C programming. Write a program that prompts the user to enter a 6x6 array with 0...
C programming. Write a program that prompts the user to enter a 6x6 array with 0 and 1, displays the matrix, and checks if every row and every column have the even number of 1’s.
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT