I’m sure my code is absolutely littered with errors because I found this project very challenging, but I managed to debug most of them until I’m down to one, and I can’t seem to solve it:
Error: Main method not found in class MainActivity, please define the main method as: public static void main(String args) or a JavaFX application class must extend javafx.application.Application
Initially I had loads of errors down to the fact that I mixed question/Questions. I’m not entirely convinced I solved that…
Here is my code, in this project all the coding has happened in Main Activity, but I’ve included Questions.java and Main.java for extra context
MAINACTIVITY
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;
// 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);
ArrayList<Question> question = new ArrayList<Question>();
question.add(questionOne);
question.add(questionTwo);
question.add(questionThree);
totalCorrect = 0;
totalQuestions = question.size();
Question firstQuestion = chooseNewQuestion();
// displayQuestion(firstQuestion);
// displayQuestionsRemaining(questions.size());
}
// TODO #4 add chooseNewQuestion() here.
public Question chooseNewQuestion(){
int randomNumber = generateRandomNumber(questions.size());
currentQuestionIndex = randomNumber;
return questions.get(currentQuestionIndex);
}
// TODO #5 add getCurrentQuestion() here
public Question getCurrentQuestion(){
return questions.get(currentQuestionIndex);
}
// TODO #6 add onAnswerSubmission() here
public void onAnswerSubmission(){
Question currentQuestion = getCurrentQuestion();
if (currentQuestion.isCorrect()){
totalCorrect =+1;
}
questions.remove(currentQuestion);
// displayQuestionsRemaining(questions.size());
if (questions.size()==0){
System.out.println("Game over!");
startNewGame();
} else {
chooseNewQuestion();
}
}
// TODO: uncomment after implementing displayQuestion()
// displayQuestion(getCurrentQuestion());
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!";
}
}
}
MAIN.JAVA
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);
}
}
QUESTION.JAVA
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;
}
}
Any help appreciated because I’m tearing my hair out a bit here. Would be nice if a solution from codecademy was available so you could actually figure out where you went wrong - of course you don’t want to encourage people to peak at the answers, but it feels very non-productive to just have to abandon a project when you can’t figure it out! New to coding & first time posting so let me know if I’ve missed any key info
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