public class MultipleChoice{ String question = (\"\"), answerText = (\"\"), ques
ID: 3615534 • Letter: P
Question
public class MultipleChoice{
String question = (""), answerText = (""), questionText = (""),
answer_a = "", answer_b = "", answer_c = "", answer_d = "", answer_e = "";
boolean answer, option;
static char correctChar = 'f';
static int questions = 0;
static char whichAnswer;
static int index = 1;
public MultipleChoice(){
}
public void setQuestion(String questionText)
{
this.questionText = questionText;
questionsCreated();
}
public boolean setAnswer(char whichAnswer, String answerText)
{
this.whichAnswer = whichAnswer;
this.answerText = answerText;
if (whichAnswer=='a') {
option = true;
answer_a=answerText;
}
if (whichAnswer=='b') {
option = true;
answer_b=answerText;
}
if (whichAnswer=='c') {
option = true;
answer_c=answerText;
}
if (whichAnswer=='d') {
option = true;
answer_d=answerText;
}
if (whichAnswer=='e') {
option = true;
answer_e=answerText;
}
else{
option = false;
}
return option;
}
public boolean setCorrect(char correctChar)
{
this.correctChar = correctChar;
if(correctChar == 'a' || correctChar == 'b' || correctChar == 'c' ||
correctChar == 'd' || correctChar == 'e')
answer = false;
return answer;
}
public char getCorrect()
{
return correctChar;
}
public static boolean grade(MultipleChoice q, char selection)
{
boolean compare_answers;
if(selection == correctChar)
{
compare_answers = true;
}
else
{
compare_answers = false;
}
return compare_answers;
}
public static int questionsCreated()
{
questions++;
return questions;
}
public void setIndex(int i)
{
index = i;
}
public String toString()
{
String newQuestion = index+". "+questionText+" "+
"a) "+answer_a+" "+
"b) "+answer_b+" "+
"c) "+answer_c+" "+
"d) "+answer_d+" "+
"e) "+answer_e+" ";
return newQuestion;
}
}
Explanation / Answer
// The modified Quiz class
public class Quiz
{
String quizName;
boolean indexNumber;
MultipleChoice q1, q2, q3, q4, q5;
public Quiz(String name)
{
quizName = name;
q1 = new MultipleChoice();
q2 = new MultipleChoice();
q3 = new MultipleChoice();
q4 = new MultipleChoice();
q5 = new MultipleChoice();
}
public boolean addQuestion(int index, MultipleChoice q)
{
if(index <= 5 && index >= 1)
{
indexNumber = true;
switch(index)
{
case 1:
q1 = q;
break;
case 2:
q2 = q;
break;
case 3:
q3 = q;
break;
case 4:
q4 = q;
break;
case 5:
q5 = q;
break;
}
}
else
{
indexNumber = false;
}
return indexNumber;
}
public String toString()
{
String result;
result = quizName+" ";
q1.setIndex(1);
result += q1+" ";
q2.setIndex(2);
result += q2+" ";
q3.setIndex(3);
result += q3+" ";
q4.setIndex(4);
result += q4+" ";
q5.setIndex(5);
result += q5+" ";
return result;
}
}
-------------------------------
Note: No need to modify the MultipleChoice.java file
public class MultipleChoice
{
String question = (""), answerText = (""), questionText = (""),
answer_a = "", answer_b = "", answer_c = "", answer_d = "",
answer_e = "";
boolean answer, option;
static char correctChar = 'f';
static int questions = 0;
static char whichAnswer;
static int index = 1;
public MultipleChoice()
{
}
public void setQuestion(String questionText)
{
this.questionText = questionText;
questionsCreated();
}
public boolean setAnswer(char whichAnswer, String answerText)
{
this.whichAnswer = whichAnswer;
this.answerText = answerText;
if(whichAnswer == 'a')
{
option = true;
answer_a = answerText;
}
if(whichAnswer == 'b')
{
option = true;
answer_b = answerText;
}
if(whichAnswer == 'c')
{
option = true;
answer_c = answerText;
}
if(whichAnswer == 'd')
{
option = true;
answer_d = answerText;
}
if(whichAnswer == 'e')
{
option = true;
answer_e = answerText;
}
else
{
option = false;
}
return option;
}
public boolean setCorrect(char correctChar)
{
this.correctChar = correctChar;
if(correctChar == 'a' || correctChar == 'b' || correctChar == 'c'
|| correctChar == 'd' || correctChar == 'e')
answer = false;
return answer;
}
public char getCorrect()
{
return correctChar;
}
public static boolean grade(MultipleChoice q, char selection)
{
boolean compare_answers;
if(selection == correctChar)
{
compare_answers = true;
}
else
{
compare_answers = false;
}
return compare_answers;
}
public static int questionsCreated()
{
questions++;
return questions;
}
public void setIndex(int i)
{
index = i;
}
public String toString()
{
String newQuestion = index + ". " + questionText + " " + "a) "
+ answer_a + " " + "b) " + answer_b + " " + "c) "
+ answer_c + " " + "d) " + answer_d + " " + "e) "
+ answer_e + " ";
return newQuestion;
}
}
-----------------------------
// Test class
public class Test
{
public static void main(String[] args)
{
Quiz example = new Quiz("Quiz 1");
MultipleChoice q = new MultipleChoice();
q.setAnswer('a', "First Choice");
q.setAnswer('b', "Second Choice");
q.setAnswer('c', "Third Choice");
q.setAnswer('d', "Fourth Choice");
q.setAnswer('e', "Fifth Choice");
q.setQuestion("This is the question.");
example.addQuestion(3, q);
System.out.println(example);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.