A Modified MiniQuiz Class Files Question.java, Complexity.java, and MiniQuiz.jav
ID: 650999 • Letter: A
Question
A Modified MiniQuiz Class
Files Question.java, Complexity.java, and MiniQuiz.java contain the classes. These classes
demonstrate the use of the Complexity interface; class Question implements the interface, and
class MiniQuiz creates two Question objects and uses them to give the user a short quiz.
Save these three files to your directory and study the code in MiniQuiz.java. Notice that after the Question objects are created, almost exactly the same code appears twice, once to ask and grade the first question, and again to ask and grade the second question. Another approach is to write a method askQuestion that takes a Question object and does all the work of asking the user the question, getting the user
Explanation / Answer
import javax.swing.*;
import java.io.*;
public class Quiz
{
//Initializaions and Instantiations
static lnumber cin = new lnumber(new InputStreamReader(System.in));
//-------------------------------------------------------------------------------
public static void main(String[] args)
{
int scr = 0;
final int noQuestions = 5;
System.out.println(" Quiz 1.0 ");
//Store questions and answers in 2 dimensional array
String[][] QandA = {
{"Who is Scooby Doo's best friend?","shaggy"},
{"Who developed relativity?","einstein"},
{"When year did the twin towers fall?","2001"},
{"Who was the first president?","washington"},
{"What is the meaning of life?","42"} };
String[] Answers = new String[noQuestions];
//loop through each string in the array and compare it to answers
for(int x = 0; x < noQuestions; x++)
{
System.out.print(" " + (x+1) + ". " + QandA[x][0] + " ");
try { Answers[x] = cin.readLine(); }
catch (IOException e) { System.err.println("Error."); }
Answers[x].toLowerCase();
if(QandA[x][1].equals(Answers[x]))
{
scr++;
}//close if
System.out.print(" ");
}//close outer loop
System.out.println(" You got " + scr + " of "
+ noQuestions + " right! ");
System.exit(0);
}//close main() function
//-------------------------------------------------------------------------------
}//close Quiz class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.