How can I split this program into two classes? import java.io.*; public class Qu
ID: 654214 • Letter: H
Question
How can I split this program into two classes?
import java.io.*;
public class Quiz
{
static LineNumberReader cin = new LineNumberReader(new InputStreamReader(System.in));
public static void main(String[] args)
{
int score = 0;
final int NumberofQuestions = 5;
int num;
System.out.println(" Welcome to CSC 111 Java Quiz ");
String[][] QandA = {
{"All information is stored in the computer using binary numbers: ","true"},
{"The word "Public" is a reserved word: ","false"},
{"Variable names may begin with a number: ","false"},
{"Will the following code result in 2? int num = 12%5: ","true"},
{"The statement { } is a legal code block: ","true"},
};
String[] Answers = new String[NumberofQuestions];
for(int x = 0; x < NumberofQuestions; x++)
{
System.out.print(" " + (x+1) + ". " + QandA[x][0] + " ");
try { Answers[x] = cin.readLine(); }
catch (IOException e) { System.err.println("Error."); }
Answers[x]=Answers[x].toLowerCase();
if(QandA[x][1].equals(Answers[x]))
{
score++;
}
System.out.print(" ");
}
num = score * 20;
if (score == 5){
System.out.println("Congratulations! you got a perfect score.");
} else if (score >= 4) {
System.out.println("Your score is " + num +"%. You can do better!");
} else if (score >= 0) {
System.out.println("Your score is " + num +"%. I'd suggest you review and try again.");
}
}
}
Explanation / Answer
import java.io.*;
public class Quiz
{
static LineNumberReader cin = new LineNumberReader(new InputStreamReader(System.in));
public static void main(String[] args)
{
int score = 0;
final int NumberofQuestions = 5;
int num;
System.out.println(" Welcome to CSC 111 Java Quiz ");
String[][] QandA = { {"All information is stored in the computer using binary numbers: ","true"},
{"The word "Public" is a reserved word: ","false"},
{"Variable names may begin with a number: ","false"},
{"Will the following code result in 2? int num = 12%5: ","true"},
{"The statement { } is a legal code block: ","true"}};
String[] Answers = new String[NumberofQuestions];
for(int x = 0; x < NumberofQuestions; x++)
{
System.out.print(" " + (x+1) + ". " + QandA[x][0] + " ");
try { Answers[x] = cin.readLine(); }
catch (IOException e) { System.err.println("Error."); }
Answers[x]=Answers[x].toLowerCase();
if(QandA[x][1].equals(Answers[x]))
{
score++;
}
System.out.print(" ");
}
num = score * 20;
if (score == 5)
{
System.out.println("Congratulations! you got a perfect score.");
}
else if (score >= 4)
{
System.out.println("Your score is " + num +"%. You can do better!");
}
else if (score >= 0)
{
System.out.println("Your score is " + num +"%. I'd suggest you review and try again.");
}
}
}
This program can be spited into two more classes answers and scores.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.