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....
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...
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:...
please choose and write true answer only and quickly?? The key elements of domestic violence are...
please choose and write true answer only and quickly?? The key elements of domestic violence are all of the following except:       Apology.    Intimidation. Physical injury.     Humiliation. ********* Nurse Angela is working in the emergency department of Medical Center. She is conducting an interview with a victim of spousal abuse. Which step should the nurse take first? Establish a rapport with the victim and the abuser. Request the presence of a security guard Ensure privacy for interviewing...
Chinese Maker of Ivanka Trump’s Shoes Looks for Cheaper Labor By KEITH BRADSHER (Links to an...
Chinese Maker of Ivanka Trump’s Shoes Looks for Cheaper Labor By KEITH BRADSHER (Links to an external site.)Links to an external site., JUNE 1, 2017 DONGGUAN, China — The Chinese factory workers who make shoes for Ivanka Trump (Links to an external site.)Links to an external site. and other designers gather at 7:40 every morning to sing songs. Sometimes, they extol worker solidarity. Usually, they trumpet ties between China and Africa, the theme of their employer’s corporate anthem. That’s no...