Question

In: Computer Science

Rewrite Program to store the pairs of states and capitals so that the questions are displayed...

Rewrite Program to store the pairs of states and capitals so that the questions are displayed randomly.
import java.util.*;
public class quiz {

        public static void main(String[] args) {
                Scanner sc=new Scanner(System.in);
                String arr[][]= {{"Alabama","Montgomery"},{"Alaska","Juneau"},{"Arizona","Phoenix"}};
                int n =arr.length;
                int count=0;
                for(int i=0;i<n;i++) {
                        System.out.printf("What is the capital of %s? ",arr[i][0]);
                        String capital=sc.next();
                        if (arr[i][1].equalsIgnoreCase(capital)) {
                                count++;
                                System.out.println("Your answer is correct");
                        }
                        else 
                        {
                                System.out.printf("The correct answer should be %s\n",arr[i][1]);
                        }
                }
                System.out.printf("The correct count is %d",count);
        }

}

Solutions

Expert Solution

Java code screenshot:

Java code:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String arr[][] = {{"Alabama","Montgomery"},{"Alaska","Juneau"},{"Arizona","Phoenix"}};
        int n = arr.length;
        
        int index[] = new int[n];
        for(int i=0;i<n;i++)
            index[i] = i;
        shuffleIndex(index);
        
        int count = 0;
        for(int i=0;i<n;i++) {
            System.out.printf("What is the capital of %s? ",arr[index[i]][0]);
            String capital=sc.next();
            if (arr[index[i]][1].equalsIgnoreCase(capital)) {
                count++;
                System.out.println("Your answer is correct");
            }
            else 
            {
                System.out.printf("The correct answer should be %s\n",arr[index[i]][1]);
            }
        }
        System.out.printf("The correct count is %d",count);
    }
    
    static void shuffleIndex(int[] index)
    {
        Random rand = new Random();
        int size = index.length;
        for (int i = 0; i < size; i++) {
                        int randomIndex = rand.nextInt(size);
                        int temp = index[randomIndex];
                        index[randomIndex] = index[i];
                        index[i] = temp;
                }
    }
}

Output:


Related Solutions

IN PYTHON: Write a program to study countries and their capitals. The program will store country:capital...
IN PYTHON: Write a program to study countries and their capitals. The program will store country:capital information and allow the user to quiz themselves. These two functions are required for this assignment. You may add other functions that you deem appropriate: - menu()   This function, which displays all the user options to the screen, prompts the user for their choice and returns their choice. This function will verify user input and ALWAYS return a valid choice. - addCapital(dict) This function...
Rewrite the following program so    that the counting of the a's and    total count...
Rewrite the following program so    that the counting of the a's and    total count are done be    separate functions       The code you turn in should have 2 functions */ #include<stdio.h> int main(void){       char input[81];               // Input string    int a=0, total=0;           // counter variables                     // Ask for an input string    puts(" Input a string of up to 80 characters and\n"    "...
The League with DMA Rewrite your League program from Assignment 8 so that it uses Dynamic...
The League with DMA Rewrite your League program from Assignment 8 so that it uses Dynamic Memory Allocation (DMA) to create the team names and scores arrays. This is a good test of the modularity of your program. You will only need to make slight modifications to your main() function if you wrote your original program using functions similar to the following: void initializeData(string names[], int wins[], int size) void sort(string names[], int wins[], int size) void display(string names[], int...
Using c# rewrite/edit the following program so that it runs the simulation 10,000 times (play the...
Using c# rewrite/edit the following program so that it runs the simulation 10,000 times (play the games 10,000 times) and count the number wins. Then calculate the winning probability by using the formula: the expected winning probability = the number of wins / 10,000 using System; class lottery { static void Main() { int n, random, choice = 1; Random randnum = new Random();    while (choice == 1) {    Console.Write("\nEnter a integer from 1 to 5:"); n =...
Having trouble with this assignment: Rewrite your most recent high scores program (shown below) so that...
Having trouble with this assignment: Rewrite your most recent high scores program (shown below) so that each name/score pair is stored in a struct named highscore. Except as noted below, this new program will continue to meet all of the requirements of your most recent high scores program. Your new program should meet the following requirements: The highscore struct should have two fields: an int named score and a char array named name. The char array should have 24 elements,...
Rewrite your most recent high scores program in C++ so that each name/score pair is stored...
Rewrite your most recent high scores program in C++ so that each name/score pair is stored in a struct named Highscore. Except as noted below, this new program will continue to meet all of the requirements of your most recent high scores program. Your new program should meet the following additional requirements: The Highscore struct should have two fields: an int named score and a char array named name. The char array should have 24 elements, making the maximum length...
[The following information applies to the questions displayed below.] Raleigh Department Store uses the conventional retail...
[The following information applies to the questions displayed below.] Raleigh Department Store uses the conventional retail method for the year ended December 31, 2019. Available information follows: The inventory at January 1, 2019, had a retail value of $45,000 and a cost of $27,500 based on the conventional retail method. Transactions during 2019 were as follows: Cost Retail Gross purchases $ 282,000 $ 490,000 Purchase returns 6,500 10,000 Purchase discounts 5,000 Sales 492,000 Sales returns 5,000 Employee discounts 3,000 Freight-in...
[The following information applies to the questions displayed below.] Oscar’s Red Carpet Store maintains a checking...
[The following information applies to the questions displayed below.] Oscar’s Red Carpet Store maintains a checking account with Academy Bank. Oscar’s sells carpet each day but makes bank deposits only once per week. The following provides information from the company’s cash ledger for the month ending February 28, 2018.    Date Amount No. Date Amount Deposits: 2/4 $ 2,700 Checks: 321 2/2 $ 4,700 2/11 2,300 322 2/8 400 2/18 3,200 323 2/12 2,500 2/25 4,100 324 2/19 2,200 Cash...
Chapter 6: Use a list to store the players Update the program in python so that...
Chapter 6: Use a list to store the players Update the program in python so that it allows you to store the players for the starting lineup. This should include the player’s name, position, at bats, and hits. In addition, the program should calculate the player’s batting average from at bats and hits. Console ================================================================ Baseball Team Manager MENU OPTIONS 1 – Display lineup 2 – Add player 3 – Remove player 4 – Move player 5 – Edit player...
Chapter 6: Use a list to store the players In Python, Update the program so that...
Chapter 6: Use a list to store the players In Python, Update the program so that it allows you to store the players for the starting lineup. This should include the player’s name, position, at bats, and hits. In addition, the program should calculate the player’s batting average from at bats and hits. Console ================================================================ Baseball Team Manager MENU OPTIONS 1 – Display lineup 2 – Add player 3 – Remove player 4 – Move player 5 – Edit player...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT