Java Arrays and Loops Game Logic pt. II

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<String> questions;
    // TODO #3 add startNewGame() here    
    void startNewGame(){
            // questions 1-3
        Question question1 = new Question(107343, "How tall is the Eiffel tower?", "1024 ft", "1063 ft", "1124 ft", "1163 ft", 1);
        Question question2 = new Question(107343,"Who invented the computer algorithm?","Charles Babbage","John Carmack","Alan Turing","Ada Lovelace",3);
        Question question2 = new Question("What is the name for the patch of skin found on your elbow?","Elbow Skin","Fascia Elbora","Wenis","Todd",2);
      questions = new Arraylist<String>();
            questions.add(question1);
            questions.add(question2);
            questions.add(question3);

Why does ArrayList<String> questions; have to be defined in // TODO #1: add integer member variables here and then again in // questions 1-3 as questions = new Arraylist<String>(); ? Is there a difference? Why? What does each thing actually do?

In TODO#1, you aren’t defining or creating any <ArrayLists>. All you’re doing is saying “here’s a variable which will store an array list.” In TODO#3, you actually create a new array list, like how you create a new object of the Question class.

Oh, okay thanks that makes sense

1 Like

Had a hard time with this project, so lots of bugs in this code. Hopefully, someone can decipher this mess of my wrongdoing?

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<String> questions;
    // TODO #3 add startNewGame() here    
    void startNewGame(){

    }
  
            // questions 1-3
        Question question1 = new Question(107343, "How tall is the Eiffel tower?", "1024 ft", "1063 ft", "1124 ft", "1163 ft", 1);
        Question question2 = new Question(107343,"Who invented the computer algorithm?","Charles Babbage","John Carmack","Alan Turing","Ada Lovelace",3);
        Question question2 = new Question("What is the name for the patch of skin found on your elbow?","Elbow Skin","Fascia Elbora","Wenis","Todd",2);
      Arraylist<Questions> questions = new Arraylist<>;
            Questions.add(question1, question2, question3);
          

      }

    }
    // TODO #4 add chooseNewQuestion() here
  public Question chooseNewQuestion(){
      int randomQuestion = generateRandomNumber(questions.size(-1));
      randomQuestion = currentQuestionIndex;
      return questions.get(currentQuestionIndex);

  }
    // TODO #5 add getCurrentQuestion() here
    public Question getCurrentQuestion(){
     return questions.get(currentQuestionIndex);

    }
    // TODO #6 add onAnswerSubmission() here
  public void onAnswerSubmission(){
        if (currentQuestion.isCorrect){
      totalCorrect 1++;
      questions.remove(1);
  }


// displayQuestionsRemaining(questions.size());
  if(questions.size() == 0){
      System.out.println(getGameOverMessage)
      
  } 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!";
        }
    }
}

What’s going wrong with it? Any error messages, or is it just not doing what you want? If so, how?

Yeahh… 25 error messages :’)

What are they? Could post (if not all, some of them)?