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
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 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...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
Write a Python 3 program called “parse.py” using the template for a Python program that we...
Write a Python 3 program called “parse.py” using the template for a Python program that we covered in this module. Note: Use this mod7.txt input file. Name your output file “output.txt”. Build your program using a main function and at least one other function. Give your input and output file names as command line arguments. Your program will read the input file, and will output the following information to the output file as well as printing it to the screen:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • How can you use Bayes’ theorem in light of new information? In Bayes’ theorem, how does...
    asked 5 minutes ago
  • Which of the following is not one of the four states of a working file? Unchanged,...
    asked 8 minutes ago
  • Assume we have CPU instructions that look like this: load register, address save register, address Where...
    asked 21 minutes ago
  • What is the difference between the following two declarations? char array[] = “Hello World”; char *array...
    asked 36 minutes ago
  • Discuss knowledge and understanding gleaned from The Least Dangerous Assumption and Strategies for Presuming Competence. How...
    asked 37 minutes ago
  • Exercise 13-20 (LO13-3) The owner of Maumee Ford-Volvo wants to study the relationship between the age...
    asked 39 minutes ago
  • Scenario The Department of Administrative Services (DAS) provides a number of services to other departments in...
    asked 47 minutes ago
  • Linear Regressions The number of newly reported crime cases in a county in New York State...
    asked 52 minutes ago
  • Specialty courts have been developed for various categories of crimes and offenders (e.g., mental health, substance...
    asked 56 minutes ago
  • An air-track cart with mass m=0.40kg and speed v0=1.2m/s approaches two other carts that are at...
    asked 56 minutes ago
  • Write a program in C# that reverses a collection and removes elements that are divisible by...
    asked 59 minutes ago
  • A gas pipeline with the thickness of 4mm is to be joint together by using welding...
    asked 1 hour ago