Class instances share the value of a data member

555 Views Asked by At

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

3

There are 3 best solutions below

2
On

choiceString is being initialized to "" every time in the constructor.

try

choiceString = questionText;
1
On

You would need to extends AllChoiceQuestion and have it's child class like NumberChoiceQuestion and StringChoicQuestion which will have their own implementation of choiceString, you need to overload choiceString implementation in child classes and create objects of child classes, this way you would have your child class specific choiceString


 public class NumberChoiceQuestion extends AllChoiceQuestion{
        this.choiceString; // Over written value of choiceString specific to NumberChoiceQuestion
    }

public class StringChoiceQuestion extends AllChoiceQuestion{
        this.choiceString; // Over written value of choiceString specific to StringChoiceQuestion
    }
0
On

The problem is you are not incrementing count when variable correct is false. Hence the choice string will always be 1 2.