Note that there are some explanatory texts on larger screens.

plurals
  1. POClass instances share the value of a data member
    primarykey
    data
    text
    <p>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?</p> <p>The following is my code:</p> <pre><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(); } } </code></pre> <p>This is the code for my instances</p> <pre><code> 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); </code></pre> <p><strong>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</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload