Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Follow up question on this code below: import javax.swing.JOptionPane; public class Quiz { public static void main(String[] args) { // declare 3 ques with ans

Follow up question on this code below:

import javax.swing.JOptionPane;

public class Quiz { public static void main(String[] args) { // declare 3 ques with ans key String question1 = "What is 3 to the power of 2? (enter A,B, C or D) "; question1 += "A. 27 "; question1 += "B. 9 "; question1 += "C. 12 "; question1 += "D. none of the above "; question1 += "E. all of the above ";

String question2 = "What is 4 to the power of 2? (enter A,B, C or D) "; question2 += "A. 24 "; question2 += "B. 12 "; question2 += "C. 16 "; question2 += "D. none of the above "; question2 += "E. all of the above ";

String question3 = "What is 5 to the power of 2? (enter A,B, C or D) "; question3 += "A. 25 "; question3 += "B. 20 "; question3 += "C. 10 "; question3 += "D. none of the above "; question3 += "E. all of the above ";

// JOptionPane.showInputDialog(question1); String answer = null; int i = 0; while (true) { if (i == 0) { // first question answer = ask(question1); if (answer.equals("B")) { JOptionPane.showMessageDialog(null, "Excellent! that is the correct answer."); ++i; } else if (answer.equals("A") || answer.equals("C") || answer.equals("D") || answer.equals("E")) { JOptionPane.showMessageDialog(null, "The answer is incorrect, please attempt again."); } else { JOptionPane.showMessageDialog(null, "Invalid answer. Please enter A, B, C, D, or E."); } } if (i == 1) { // second question answer = ask(question2); if (answer.equals("C")) { JOptionPane.showMessageDialog(null, "Excellent! that is the correct answer."); ++i; } else if (answer.equals("A") || answer.equals("B") || answer.equals("D") || answer.equals("E")) JOptionPane.showMessageDialog(null, "The answer is incorrect, please attempt again."); else JOptionPane.showMessageDialog(null, "Invalid answer. Please enter A, B, C, D, or E."); } if (i == 2) { // third question answer = ask(question3); if (answer.equals("A")) { JOptionPane.showMessageDialog(null, "Excellent! that is the correct answer."); ++i; } else if (answer.equals("B") || answer.equals("C") || answer.equals("D") || answer.equals("E")) JOptionPane.showMessageDialog(null, "The answer is incorrect, please attempt again."); else JOptionPane.showMessageDialog(null, "Invalid answer. Please enter A, B, C, D, or E."); } else if (i == 3) //break if i has 3 if user correct the 3 answer the i value will increment to 3 and while loop will end break; } }

// method that ask the question and return the option private static String ask(String q) { String answer = JOptionPane.showInputDialog(q); return answer.toUpperCase(); } }

May you please add a code that meets the following:

***add a score for the quiz above. Add two static member variables, one for the number of questions and one for the number of correct answers. Initialize them to zero for good documentation. static int nQuestions = 0; static int nCorrect = 0; Remember where member variables go: inside a class definition but outside all method definitions. You may declare the variables before, after, or between the existing methods. In the "check" method, increment "nQuestions" each time it is called. Also in the "check" method, increment "nCorrect" for each correct answer. Display the score at the end of the main method using "JOptionPane.showMessageDialog". Use the text, " correct out of questions", with the appropriate numbers. please run your program and check it displays the right numbers of questions and correct answers.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions