Cumulative Project 3 - Can't figure out isCorrect error

I’m working through Cumulative Project 3 (“Game Logic Pt. I”) and when I run the Main.java file I get this error:

Main.java:11: error: method isCorrect in class Question cannot be applied to given types;
        if (testQuestion.isCorrect()) {
                        ^
  required: int,int
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error

I can’t figure out why I’m getting this error as I think my isCorrect method should work. Here is my entire Question.java code:

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

    // Add isCorrect() here
    boolean questionCorrect;
    
    boolean isCorrect(int correctAnswer, int playerAnswer){
      if (correctAnswer == playerAnswer) {
        questionCorrect = true;
      } else {questionCorrect = false;}
        return questionCorrect;
    }
} 

Is there something I’m missing? Thanks in advance for any assistance!

isCorrect() takes two arguments, both ints, one for the correct answer and one for the player answer. You need to pass these in when you call the method.

1 Like

Thanks, Victoria! I guess I’m expecting the arguments to have been provided? The instructions say to run the code in Main.java, but when I do I get the above error message. Below is the Main.java code, all of which was provided by Codecademy. I didn’t edit anything, and based on the instructions it sounds like I shouldn’t have to, but maybe that’s a faulty assumption?

public class Main {

    public static void main(String[] args) {

        Question testQuestion = new Question(0, "Who invented the computer algorithm?", "Bill Gates", "Charles Babbage", "Ada Lovelace", "Leonardo DaVinci", 2);

        System.out.println("The player’s guess is: " + testQuestion.playerAnswer);

        if (testQuestion.playerAnswer == -1) {

            System.out.println("Default answer selected!");

        }

        testQuestion.playerAnswer = 2;

        System.out.println("The player’s guess is: " + testQuestion.playerAnswer);

        if (testQuestion.isCorrect()) {

            System.out.println("The player’s guess is correct!");

        } else {

            System.out.println("The player’s guess is incorrect!");

        }

        MainActivity mainActivity = new MainActivity();

        System.out.println("A random number between 0 and 50: " + mainActivity.generateRandomNumber(50));

        System.out.println("A random number between 0 and 1000: " + mainActivity.generateRandomNumber(1000));

        System.out.println("A random number between 0 and 1 million: " + mainActivity.generateRandomNumber(1000000));

        

        System.out.println("Game over test 1 (should be 2 out of 3): " + mainActivity.getGameOverMessage(2, 3));

        System.out.println("Game over test 2 (should be 4 out of 4): " + mainActivity.getGameOverMessage(4, 4));

    }

}

I can’t see the exact instructions as I don’t have PRO access, but from what you’ve posted, it looks like isCorrect() should be written to take no arguments. Try rewriting the isCorrect() method so that it takes no arguments but still returns either true or false depending on whether the player answered correctly. :slight_smile:

Hint

Because isCorrect() is an instance method and you’re calling the method on testQuestion, you have access to all of testQuestion's instance variables. This means you already have access to the integers for correctAnswer and playerAnswer and don’t need these parameters in isCorrect().

1 Like

Thanks so much, Victoria!! This did the trick. Your hint helped explain what I was missing; I had set up the isCorrect method incorrectly. Really appreciate your assistance!

1 Like

No problem, happy coding!

1 Like