I am writting a program to display a list multiple choice questions.I am having a problem with the value stored in the ChoiceString data member of my java class. Each instance of the class should have its own value of ChoiceString, but for some reason ChoiceString only hold the value initialized by the first instance and share it with other instances, where I want each instance to have its own ChoiceString unique value. how can I fix that?
The following is my code:
public class AllChoiceQuestion extends ChoiceQuestion{
private String note = "Note: The following question has one or more correct choices\nYou must give the number(s) of ALL correct anwers, in order,\nto get full credit andsperated by spaces";
private int count =0;
// A varabile that will hold the user answer
private String choiceString;
public AllChoiceQuestion(String questionText) {
// initilize the question text value
super(questionText);
choiceString="";
}
public void addChoice(String choice, boolean correct){
super.addChoice(choice, correct);
if(correct == true){
count++;
choiceString += "" + count+" " ;
}
super.setAnswer(choiceString.trim());
}
public void display(){
System.out.println(note);
super.display();
}
public String toString(){
return note +"\n"+ super.toString();
}
}
This is the code for my instances
ChoiceQuestion allchoicequestion1 = new AllChoiceQuestion("Which of the basic data type(s) has the size of 16 bit?");
allchoicequestion1.addChoice("Char", true);
allchoicequestion1.addChoice("Short", true);
allchoicequestion1.addChoice("Float", false);
allchoicequestion1.addChoice("Double", false);
ChoiceQuestion allchoicequestion2 = new AllChoiceQuestion("Which of the basic data type(s) has the size of 64 bit?");
allchoicequestion2.addChoice("Long", false);
allchoicequestion2.addChoice("Doulbe", false);
allchoicequestion2.addChoice("Byte", true);
allchoicequestion2.addChoice("Int", true);
So ChoiceStrng for allChoiceQuestion1 should be 1 2 and ChoiceString forallChoiceQuestion2 should be 3 4 but every time I tyee 3 4 as an answer for forallChoiceQuestion2 it gives me a false answer , but if I type 1 2 it would be correct
choiceString is being initialized to "" every time in the constructor.
try