I got stuck in Game Logic Pt.II. Please help me

Help. I got stuck in task 14 in Game Logic Pt.II.
This is my Question.java file:

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;
    }

    boolean isCorrect() {
      return correctAnswer == playerAnswer;
    }
}

This is my MainActivity.java file:

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 question1 = new Question(921238, "How tall is the Eiffel tower?", "1024 ft", "1063 ft", "1124 ft", "1163 ft", 1);
    Question question2 = new Question(283912, "How much is the net worth of Elon Musk?", "USD$197 billion", "USD$201 billion", "USD$123 billion", "USD$312 billion", 0);
    Question question3 = new Question(991226, "When did the Battle of Hong Kong end?", "12 December 1939", "13 January 1941", "25 December 1941", "7 March 1940", 2);
    Question question4 = new Question(192032, "Which year did the movie Fight Club publish in?", "1996", "1999", "1997", "1990", 1);
    Question question5 = new Question(283019, "What is the official name of the first ever Mac computer?", "Macbook 128K", "Apple Macbook", "Apple Macbook 128K", "Macintosh 128K", 3);
    ArrayList<Question> questionList = new ArrayList<>();
    questionList.add(question1); 
    questionList.add(question2); 
    questionList.add(question3); 
    questionList.add(question4); 
    questionList.add(question5); 
    totalCorrect = 0;
    totalQuestions = 0;
    Question firstQuestion = chooseNewQuestion();
    // displayQuestion(firstQuestion);
    // displayQuestionsRemaining(questions.size());
  }
    // TODO #4 add chooseNewQuestion() here
  public Question chooseNewQuestion() {
    int chosenQuestionIndex = generateRandomNumber(4);
    currentQuestionIndex = chosenQuestionIndex;
    return getCurrentQuestion();
  }
    // TODO #5 add getCurrentQuestion() here
  public Question getCurrentQuestion() {
    return questions.get(currentQuestionIndex);
  }
    // TODO #6 add onAnswerSubmission() here
  public void onAnswerSubmission() {
    getCurrentQuestion();
    if (Question.isCorrect()) {
      totalCorrect += 1;
    }
  }

    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 lastly this is my Main.java file:

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!
    }

    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);
    }
}

This is the error I encountered in the MainActivity.java file:

MainActivity.java:43: error: non-static method isCorrect() cannot be referenced from a static context
    if (Question.isCorrect()) {
                ^
1 error

How could I change my code to solve this error? Thanks a lot!

Haven’t tested the code but I assume it would be something like this. Feel free to correct me if I’m incorrect.

Short answer:
boolean isCorrect()
to
static boolean isCorrect()

Long answer:

But after I changed the code, there are 2 errors occured now:

Question.java:29: error: non-static variable correctAnswer cannot be referenced from a static context
      return correctAnswer == playerAnswer;
             ^
Question.java:29: error: non-static variable playerAnswer cannot be referenced from a static context
      return correctAnswer == playerAnswer;
                               ^

What should I do now? Thank you

Here are a few suggestions:

  • Remove the static from static boolean isCorrect() {... so that you have it like in your original post:
boolean isCorrect() {...
  • Make the following modifications in startNewGame():
ArrayList<Question> questionList = new ArrayList<>();
// Change it to:
questions = new ArrayList<Question>();

Why? Because I don’t see questionList being used by you anywhere else in the code outside the startNewGame method. You have already declared the questions ArrayList in the MainActivity class (on the line under your TODO #2 comment), so if you want to reset the questions ArrayList at the start of a new game, then you should assign a new ArrayList<Question>(). You don’t want to re-declare questions inside startNewGame, you just want to assign an empty list so that the already existing questions list is reset.

questionList.add(question1); 
// Change it to:
questions.add(question1); 
// Do the same for question2 through question5

// You wrote:
totalQuestions = 0;
// It should probably be:
totalQuestions = questions.size();
  • In your onAnswerSubmission() method, you make a call to getCurrentQuestion() but do nothing with the returned question. You could do:
// You wrote:
getCurrentQuestion();
    if (Question.isCorrect()) { ...

// Change it to:
    if (getCurrentQuestion().isCorrect()) { ...

// OR assign it to a variable and then use in condition
// You can name the variable whatever you find appropriate.
Question currQues = getCurrentQuestion();
    if (currQues.isCorrect()) { ...
1 Like

Oh my god, thank you so much for your detailed solution!

1 Like