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...
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...
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.
The clause below was written by the seller. You are the buyer. Rewrite it so it...
The clause below was written by the seller. You are the buyer. Rewrite it so it is to your advantage. Delivery; Title; and Risk of Loss. Unless otherwise stated in Exhibit A, the Seller shall deliver the Goods FOB the Seller’s facility and title to and risk of loss of the Goods will pass to the Buyer upon such delivery by the Seller. Any stated delivery dates are approximate. The Seller will not be liable for any losses, damages, penalties,...
Rewrite this code of a game of Moropinzee so that it works as intended without the...
Rewrite this code of a game of Moropinzee so that it works as intended without the "break;" in the last few lines of the code. Code: import java.util.*; public class Moropinzee { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(true) { System.out.println("Player 1 enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:"); int p1 = sc.nextInt(); while(p1<1 || p1>5) { System.out.println("Invalid choice, Player 1. Enter a number 1-5:"); p1 = sc.nextInt(); } System.out.println("Player 2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT