Question

[Java] Write a program that stores pairs of all the states and capitals (USA). The program...

[Java] Write a program that stores pairs of all the states and capitals (USA). The program should give the user a State and ask for the capital. The program should keep track of how many correct answers were given. User should see score printed at the end. IMPORTANT: Make sure the program RANDOMLY asks for the capital of a random state. This question has already been asked before in Expert Q&A but the answer never randomly gave states.

Homework Answers

Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// StatesCapitals.java

import java.util.Scanner;

public class StatesCapitals {

   public static void main(String[] args) {
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);
       // Declaring variables
       int cnt = 0;
       int index;
       String ans;
       // Declaring and initializing an array
       String states[] = { "Alabama", "Alaska", "Arizona", "Arkansas",
               "California", "Colorado", "Connecticut", "Delaware", "Florida",
               "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa" };

       String capitals[] = { "Montgomery", "Juneau", "Phoenix", "Little Rock",
               "Sacramento", "Denver", "Hartford", "Dover", "Tallahassee",
               "Atlanta", "Honolulu", "Boise", "Springfield", "Indianapolis",
               "Des Moines" };

       for (int i = 0; i < 5; i++) {
           index = (int) (Math.random() * ((states.length-1) + 1));
           System.out.print("What is the Capital for \"" + states[index]
                   + "\" ? :");
           ans = sc.nextLine();
           if (ans.equalsIgnoreCase(capitals[index])) {
               System.out.println("Correct!");
               cnt++;
           }
           else
           {
               System.out.println("Wrong.The answer is : "+capitals[index]);
           }

       }

       System.out.println("Out of 5 no of correct answers :" + cnt);

   }

}

===========================================

output;

What is the Capital for "Arkansas" ? :Little Rock
Correct!
What is the Capital for "Connecticut" ? :Boise
Wrong.The answer is : Hartford
What is the Capital for "Idaho" ? :Boise
Correct!
What is the Capital for "Alabama" ? :Montgomery
Correct!
What is the Capital for "California" ? :Sacramento
Correct!
Out of 5 no of correct answers :4


=====================Could you plz rate me well.Thank You

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a...
**JAVA LANGUAGE** Write a program that models an employee. An employee has an employee number, a name, an address, and a hire date. A name consists of a first name and a last name. An address consists of a street, a city, a state (2 characters), and a 5-digit zip code. A date consists of an integer month, day and year. All fields are required to be non-blank. The Date fields should be reasonably valid values (ex. month 1-12, day...
Write a program that sorts the content of a map by Values. Before we start, we...
Write a program that sorts the content of a map by Values. Before we start, we need to check the explanation of Java Map Interface and Java LinkedHashMap. This program can be implemented as follows: 1. Create a LinkedHashMap class <String (Country), String(Capital)> that stores Capitals of FIVE countries. 2. Use sortMap() for receiving content of the created class of LinkedHashMap and reorder it in a sorted Map. (sort in natural order). 3. You need to transfer the map into...
This is a Java program Program Description You work for a local cell phone company and...
This is a Java program Program Description You work for a local cell phone company and have been asked to write a program to calculate the price of a cell phone data plan being purchased by a customer. The program should do the following tasks: Display a menu of the data plans that are available to be purchased. Read in the user’s selection from the menu.  Input Validation: If the user enters an invalid option, the program should display an error...
WRITE A JAVA PROGRAM 1. Assignment Description Write a stock transaction program that calculates a customer's...
WRITE A JAVA PROGRAM 1. Assignment Description Write a stock transaction program that calculates a customer's profit (or loss) within a certain period of time. 1) First, the program need to get user's name, the stock's code, number of shares and the price he/she purchased the stock, it also asks the user for the price he/she sold the stock later on. The program then compute the following: The amount of money the customer paid for the stock. The amount of...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
Write a Python  program with correct indentation that creates a dictionary containing the U.S. states as keys...
Write a Python  program with correct indentation that creates a dictionary containing the U.S. states as keys and their abbreviations as values. The program should then randomly quiz the user by displaying the abbreviation and asking the user to enter that state's name. The program should keep a count of the number of correct and incorrect responses, as well as which abbreviation the user missed. You should include the following: Mainline logic and functions Error handling Dictionaries A file is attached...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
Objective: Write a Java program that will use a JComboBox from which the user will select...
Objective: Write a Java program that will use a JComboBox from which the user will select to convert a temperature from either Celsius to Fahrenheit, or Fahrenheit to Celsius. The user will enter a temperature in a text field from which the conversion calculation will be made. The converted temperature will be displayed in an uneditable text field with an appropriate label. Specifications Structure your file name and class name on the following pattern: The first three letters of your...
In this assignment you will write a program that compares the relative strengths of two earthquakes,...
In this assignment you will write a program that compares the relative strengths of two earthquakes, given their magnitudes using the moment magnitude scale. Earthquakes The amount of energy released during an earthquake -- corresponding to the amount of shaking -- is measured using the "moment magnitude scale". We can compare the relative strength of two earthquakes given the magnitudes m1 and m2 using this formula: f=10^1.5(m1−m2) If m1>m2, the resulting value f tells us how many times stronger m1...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...