Unquote startNewGame() method (how to create a new list and assign it to a member variable?)

Link to exercise

I’m trying to create the startNewGame() method.
I don’t understand how create a new list and assign it to a member variable?

Step 2 says:

Make a list, check it twice…

To keep track of every question the player must answer, MainActivity will store every Question in an ArrayList , an object which holds an ordered list of items.

Create an ArrayList object variable named questions below the integers declared in task 1 ( TODO #2 ). The ArrayList will contain objects of the Question class.

As I understand it this means:

    // TODO #2: add ArrayList member variable here
    ArrayList<Question> questions = new ArrayList<Question>();

Step 5 says:

And array we go.

In startNewGame() , create a new ArrayList and assign it to the questions member variable you defined in task 2. Then, add all of your Question objects to the ArrayList .

I don’t understand how create a new list and assign it to a member variable?
The hint says:

To create a new ArrayList , we use the following syntax, Arraylist<Homework> myHomework = new ArrayList<>; .

The ArrayList object provides the add(<T> element) method, which allows us to add a new object of type <T> to the end of the ArrayList , like so, myHomework.add(mathProblems);

I could put:

      ArrayList<Question> questions = new ArrayList<Question>();

but this seems to defeat the point of the code above.

my full code so far:

import java.util.ArrayList;

public class MainActivity {

    // TODO #1: add integer member variables here
    int currentQuestionIndex;
    int totalCorrect;
    int totalQuestions;

    // TODO #2: add ArrayList member variable here
    ArrayList<Question> questions = new ArrayList<Question>();

    // TODO #3 add startNewGame() here    
    public void startNewGame(){
      Question q1 = new Question(921238, "How tall is the Eiffel tower?", "1024 ft", "1063 ft", "1124 ft", "1163 ft", 1);
      Question q2 = new Question(921238, "Who invented the computer algorithm?", "Charles Babbage", "John Carmack", "Alan Turing", "Ada Lovelace", 3);
      Question q3 = new Question(748294, "What is the name for the patch of skin found on your elbow?", "Elbow Skin", "Fascia Elbora", "Wenis", "Todd", 2);
      ArrayList<Question> questions = new ArrayList<Question>();
      questions.add(q1);
      questions.add(q2);
      questions.add(q3);
      totalCorrect = 0;
      totalQuestions = questions.size();
      Question q1 = chooseNewQuestion();
      // displayQuestion(firstQuestion);
      // displayQuestionsRemaining(questions.size());


    }

    // TODO #4 add chooseNewQuestion() here

    // TODO #5 add getCurrentQuestion() here

    // TODO #6 add onAnswerSubmission() here

    int generateRandomNumber(int max) {
      double randomNumber = Math.random();
      double result = max * randomNumber;
      return (int) result;
    }

    String getGameOverMessage(int totalCorrect, int totalQuestions) {
        if (totalCorrect == totalQuestions) {
            return "You got all " + totalQuestions + " right! You won!";
        } else {
            return "You got " + totalCorrect + " right out of " + totalQuestions + ". Better luck next time!";
        }
    }
}

A small correction, within startNewGame method you are trying to create a new local variable with the same name that of the member variable questions.

As per the step, they want you to overwrite the member variable questions collection with a new ArrayList, we can achieve that as below:
questions = new ArrayList();

This will create a new instance of ArrayList and since member variable questions is a reference variable, it will now refer to the new instance.

https://techstackjournal.com/java-tutorial/variables/

1 Like