java: Programming problem # 1 : Develop an application that repeatedly prompts t
ID: 3709270 • Letter: J
Question
java: Programming problem # 1 : Develop an application that repeatedly prompts the user to enter a capital for a state. Upon receiving the user input, the application must report whether the answer is correct or incorrect, display the correct answer if the answer is incorrect, and prompt the user to enter the capital for the next state. At the end the application must display the total and the percentage of correct count in this format. “You answered # out of 13 questions (####90) correctly." The percentage must have two decimal places only. The application must not be "case sensitive." Use the database (array) storing the states and their capitals below {"Alabama","Montgomery", , {"Alaska","Juneau", {"Arizona","Phoenix") , "Arkansas", "Little Rock") , {"California","Sacramento" , {"Colorado","Denver" , "Connecticut","Hartford" {"Delaware","Dover", , {"Florida","Tallahassee", {"Georgia","Atlanta"j , {"Hawaii","Honolulu"; , {"Idaho","Boisej , "Illinois","Springfield"Explanation / Answer
package January;
import java.util.Scanner;
// Class CapitalQuiz definition
public class CapitalQuiz
{
// Creates a two dimensional array of type string to store state and capital names
static String questions[][] = {{"Alabama","Montgomery"}, {"Alaska", "Juneau"}, {"Arizona", "Phoenix"},
{"Arkansas", "Little Rock"}, {"California", "Sacramento"}, {"Colorado", "Denver"},
{"Connecticut", "Hartford"}, {"Delaware", "Dover"}, {"Florida", "Tallahassee"},
{"Georgia", "Atlanta"}, {"Hawaii", "Honolulu"}, {"Idaho", "Boise"}, {"Illinois", "Springfield"}};
// Static method to play the quiz
static void playQuiz()
{
// Scanner class object created
Scanner sc = new Scanner(System.in);
// Counter for correct answer
int correct = 0;
// To store the correct answer percentage
double percent;
// To store the capital entered by the user
String capital;
// Loops till number of states in the matrix
for(int counter = 0; counter < questions.length; counter++)
{
// Displays the state name stored at questions counter index position for row and 0 index position for column
System.out.printf(" Quiz question %d: Enter the capital name for the state %s: ", (counter + 1), questions[counter][0]);
// Accepts the capital name from the user
capital = sc.nextLine();
// Checks if the user entered capital is equals to
// Capital stored at matrix questions counter index position for row and 1 for column index position
if(capital.equals(questions[counter][1]))
{
// Displays message
System.out.println("Good! Correct answer.");
// Increase the correct answer counter by one
correct++;
}// End of if condition
// Otherwise wrong answer
else
{
// Displays message
System.out.println("Sorry! Wrong answer.");
// Displays the correct answer
System.out.println("Correct answer is: " + questions[counter][1]);
}// End of else
}// End of for loop
// Calculates the percentage
percent = (correct / (double)questions.length) * 100;
// Displays message
System.out.println("Quiz over.");
System.out.println(" Your Score");
// Displays total correct answer with percentage
System.out.printf(" Your answered %d out of %d questions %.2f correctly", correct, questions.length, percent);
}// End of method
// main method definition
public static void main(String[] ss)
{
// Calls the method to play the quiz
playQuiz();
}// End of main method
}// End of class
Sample Output:
Quiz question 1:
Enter the capital name for the state Alabama: Montgomery
Good! Correct answer.
Quiz question 2:
Enter the capital name for the state Alaska: Denver
Sorry! Wrong answer.
Correct answer is: Juneau
Quiz question 3:
Enter the capital name for the state Arizona: Phoenix
Good! Correct answer.
Quiz question 4:
Enter the capital name for the state Arkansas: Little Rock
Good! Correct answer.
Quiz question 5:
Enter the capital name for the state California: Sacramento
Good! Correct answer.
Quiz question 6:
Enter the capital name for the state Colorado: Tallahassee
Sorry! Wrong answer.
Correct answer is: Denver
Quiz question 7:
Enter the capital name for the state Connecticut: Hartford
Good! Correct answer.
Quiz question 8:
Enter the capital name for the state Delaware: Dover
Good! Correct answer.
Quiz question 9:
Enter the capital name for the state Florida: Tallahassee
Good! Correct answer.
Quiz question 10:
Enter the capital name for the state Georgia: Atlanta
Good! Correct answer.
Quiz question 11:
Enter the capital name for the state Hawaii: Honolulu
Good! Correct answer.
Quiz question 12:
Enter the capital name for the state Idaho: Boise
Good! Correct answer.
Quiz question 13:
Enter the capital name for the state Illinois: Springfield
Good! Correct answer.
Quiz over.
Your Score
Your answered 11 out of 13 questions 84.62 correctly
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.