Cumulative Project 4 = Arraylist memory address

Before I ask here is the link to the project

https://www.codecademy.com/paths/introduction-to-android-with-java/tracks/java-arrays-and-loops/modules/cumulative-project-4/projects/unquote-game-logic-pt-ii
I have completed the debugging of the project: however, I cant seem to be able to find an effective way to print out my ArrayList without printing the memory address and I don’t know how best to use the to String () method in this scenario.

this is my Question Class

public class Question { int imageId; String questionText; String answer0; String answer1; String answer2; String answer3; int correctAnswer; int playerAnswer; public Question(int imageIdentifier, String questionString, String answerZero, String answerOne, String answerTwo, String answerThree, int correctAnswerIndex) { imageId = imageIdentifier; questionText = questionString; answer0 = answerZero; answer1 = answerOne; answer2 = answerTwo; answer3 = answerThree; correctAnswer = correctAnswerIndex; playerAnswer = -1; } public boolean isCorrect() { return correctAnswer == playerAnswer; } }

This is my Main Activity Class

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 questionOne = new Question( 921238, "How tall is the Eiffel tower?", "1024 ft", "1063 ft", "1124 ft","1163 ft", 1); Question questionTwo = new Question( 107343, "Who invented the computer algorithm?","Charles Babbage", "John Carmack","Alan Turing", "Ada Lovelace", 3); Question questionThree = new Question (748294,"What is the name for the patch of skin found on your elbow?", "Elbow Skin", "Fascia Elbora", "Wenis", "Todd", 2); questions.add(questionOne); questions.add(questionTwo); questions.add(questionThree); totalQuestions = questions.size(); totalCorrect= 0; Question firstQuestion = chooseNewQuestion(); // displayQuestion(firstQuestion); // displayQuestionsRemaining(questions.size()); } // TODO #4 add chooseNewQuestion() here public Question chooseNewQuestion (){ int result = generateRandomNumber(questions.size()-1); currentQuestionIndex = result; return questions.get(result); } // TODO #5 add getCurrentQuestion() here public Question getCurrentQuestion(){ return questions.get(currentQuestionIndex); } // TODO #6 add onAnswerSubmission() here public void onAnswerSubmission(){ Question objectCurrentQuestion = getCurrentQuestion(); if (objectCurrentQuestion.isCorrect()){ totalCorrect++; } questions.remove(objectCurrentQuestion); // displayQuestionsRemaining(questions.size()) if (questions.size()==0){ System.out.println("Game Over"); startNewGame(); } else {chooseNewQuestion();} // TODO: uncomment after implementing displayQuestion() // displayQuestion(getCurrentQuestion()); } public 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!"; } } }

And this is my main class (please excuse the poor spacing i recently debugged)

public class Main { public static void main(String[] args) { MainActivity mainActivity = new MainActivity(); mainActivity.startNewGame(); System.out.println("Questions remaining: " + mainActivity.totalQuestions); Question currentQuestion = mainActivity.getCurrentQuestion(); printQuestion(currentQuestion); // Play the game! System.out.println(mainActivity.getCurrentQuestion()); } static void printQuestion(Question question) { System.out.println("Question: " + question.questionText); System.out.println("Option 1: " + question.answer0); System.out.println("Option 2: " + question.answer1); System.out.println("Option 3: " + question.answer2); System.out.println("Option 4: " + question.answer3); } }

You need a toString method in the Question class

Here’s one idea:

    public String toString() {
       String text = new String(questionText);
       text = text + " [ ";
       text = text + answer0 + ", ";
       text = text + answer1 + ", ";
       text = text + answer2 + ", ";
       text = text + answer3 + " ]";
       return text;
    }

an alternative would be to have the options for question in an Array or ArrayList (of Strings) instead of as the String variables answer0, answer1, answer2, answer3
because the packages
java.util.ArrayList
java.util.Array
have toString methods

Thank you can you explain how it worked it?

I noticed that you were trying to print an instance of question,
and that it was printing a memory address instead of something more useful …
So the question class needed a toString method so that when you try to print an instance of Question, it would use that toString method to print stuff to the screen/console.
I looked at your Question class, and the important class variables seemed to be questionText and answer0, answer1, answer2, answer3 so I thought those should be included in its toString method.
The toString method has to return a String,
so I had to make a String …
I started by copying questionText into a new String and then I added stuff to that.
I though it would look nice if I made it look like a printout of an array so I put strings of commas and brackets between the stuff I was adding in.
I had text = text + stuff to add stuff into the text string one-by-one so that I wouldn’t have to put all the strings I was adding in the same line of code and so that it would be easier to debug if there was an issue.

(In Object-Oriented Java, in Learn Java: Methods, it was part 8/9 in that lesson.)
https://www.codecademy.com/courses/learn-java/lessons/learn-java-methods/exercises/the-tostring-method

The toString method for an object is what goes on the screen for an object when
you do System.out.println( thatObject );