0

I've checked through this site and it seems other people have had similar problems but theirs were due to static fields. I have 2 classes, a Questions class and a ScienceQuestions class which extends Questions:

import java.util.*;

public abstract class Questions {

public int questionID;
public int difficulty;
public boolean bAsked;
public String question;
public ArrayList<String> answers;
public String correctAnswer;

public Questions addQuestion(int id, int dif, boolean asked, String q, ArrayList<String> ans, String correct) 
{
    questionID = id;
    difficulty = dif;
    bAsked = asked;
    question = q;
    answers = ans;
    correctAnswer = correct;
    return this;
}

}

import java.util.ArrayList;

public class ScienceQuestions extends Questions 
{
public ArrayList<Questions> sciQuestions = new ArrayList<Questions>();

//Add default questions
public ScienceQuestions()
{       
    sciQuestions.add(0, addQuestion1());
    sciQuestions.add(1, addQuestion2());
    sciQuestions.add(2, addQuestion3());
    System.out.println(sciQuestions.get(0).question + " " + sciQuestions.get(1).question + " " + sciQuestions.get(2).question);
}

//question 1
private Questions addQuestion1()
{
    int questionID = 1;
    int dif = 1;
    boolean asked = false;
    String question = "What is the chemical symbol for Magneisum?";
    ArrayList<String> answers = new ArrayList<>();
    String answer1 = "Mg", answer2 = "M", answer3 = "mg", answer4 = "MG";
    String correctAnswer = answer1;
    answers.add(answer1);
    answers.add(answer2);
    answers.add(answer3);
    answers.add(answer4);
    Questions thisQ = addQuestion(questionID, dif, asked, question, answers, correctAnswer);
    return thisQ;
}

//question 2
private Questions addQuestion2()
{
    int questionID = 1;
    int dif = 2;
    boolean asked = false;
    String question = "What is the most accurate acceleration on Earth due to gravity as sea level?";
    ArrayList<String> answers = new ArrayList<>();
    String answer1 = "9.81 m/s", answer2 = "9.81 N/Kg", answer3 = "10 m/s^2", answer4 = "10 N/Kg";
    String correctAnswer = answer2;
    answers.add(answer1);
    answers.add(answer2);
    answers.add(answer3);
    answers.add(answer4);
    Questions thisQ = addQuestion(questionID, dif, asked, question, answers, correctAnswer);
    return thisQ;
}

//question 2
private Questions addQuestion3()
{
    int questionID = 1;
    int dif = 2;
    boolean asked = false;
    String question = "What is the order of magnitude of the gravitational constant in standard units?";
    ArrayList<String> answers = new ArrayList<>();
    String answer1 = "*10^-10", answer2 = "*10^-13", answer3 = "*10^-12", answer4 = "*10^-11";
    String correctAnswer = answer4;
    answers.add(answer1);
    answers.add(answer2);
    answers.add(answer3);
    answers.add(answer4);
    Questions thisQ = addQuestion(questionID, dif, asked, question, answers, correctAnswer);
    return thisQ;
}
}

Now when I instantiate the ScienceQuestions class all of the elements in the ArrayList sciQuestions are of the same as the final element the I've added. Why is that?

2 Answers2

1

You create only one instance and overwrite the properties.

You could:

import java.util.*;

public class Questions {

    public int questionID;
    public int difficulty;
    public boolean bAsked;
    public String question;
    public ArrayList<String> answers;
    public String correctAnswer;

    public Questions(int id, int dif, boolean asked, String q, ArrayList<String> ans, String correct) {
        questionID = id;
        difficulty = dif;
        bAsked = asked;
        question = q;
        answers = ans;
        correctAnswer = correct;
    }
}

and in ScienceQuestions.class remove the extends Questions and replace

addQuestion(questionID, dif, asked, question, answers, correctAnswer);

with

new Questions(questionID, dif, asked, question, answers, correctAnswer);
Frederic Klein
  • 2,846
  • 3
  • 21
  • 37
  • Thank you, it works. So I'm guessing the first time it didn't work was because I wasn't instantiating new Questions() so addQuestion() only affected one "instance" of that class? – user3544582 Apr 13 '16 at 13:18
  • Exactly: you created one instance/one object and added this object to the arraylist. But it is by reference, so no new objects are created. During the addQuestion method you changed the properties of the object and when you call get(index) on the arraylist you get the same object for each index, hence the same parameters you last set. – Frederic Klein Apr 13 '16 at 16:01
0

I might be wrong but I think the problem might be that you're instantiating the arraylist with a no-parameter constructor. This might instantiate an arraylist with a size of 1. Add simply appends to the end of the list (and does not increase the capacity). Perhaps you should instantiate it with a size as described here:

Constructor with initial capacity

edit: changed the word size to capacity

Terabyte
  • 67
  • 6