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!