Cumulative Project 4

Definitely need some help on this one… Did my best but cant for the life of me get it to run in the main.java. Thanks for any help you can provide.

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<Integer> questions = new ArrayList<Integer>();

    // TODO #3 add startNewGame() here    

public void startNewGame(){

  

  Question question1 = new Question(38373, "In the long run, the greatest weapon of mass destruction is stupidity."), "Thomas Sowell", "Albert Einstein", "FDR", "Thomas Jefferson"

  ,0);

  

  Question question2 =  new Question(54643, "I'd rather be the king of kids, than the prince of fools.", "Robin Williams", "George Carlin", "Jack Black", "Martin Short", 2);

  

  Question question3 = new Question(47478, "Good decisions come from experience. Experience comes from making bad decisions.", "Charles Dickens", "Oscar Wild", "Mark Twain", "Marilyn Monroe",2);

questions.add(question1);

questions.add(question2);

questions.add(question3);

totalCorrect = 0;

totalQuestions = questions.size();

  

Question firstQuestion = chooseNewQuestion();

// displayQuestion(firstQuestion);

// displayQuestionsRemaining(questions.size());

}

    // TODO #4 add chooseNewQuestion() here

public Question chooseNewQuestion(){

int n = generateRandomNumber(2);

currentQuestionIndex = n;

return questions.get(n);

}

    // TODO #5 add getCurrentQuestion() here

public Question getCurrentQuestion(){

return questions.get(currentQuestionIndex);

}

    // TODO #6 add onAnswerSubmission() here

public void onAnswerSubmission(){

questions.getCurrentQuestion();

if(Question.isCorrect){

  totalCorrect = totalCorrect + 1;

}

questions.remove(currentQuestionIndex);

// displayQuestionsRemaining(questions.size());

if(questions.size() == 0){

  System.out.println("GAME OVER");

  MainActivity.startNewGame();

}else{

  Question.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!";

        }

    }

}

Is this a Codecademy project? If so, can you post a link to it?

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

here is the link to the project. Thanks for looking at this for me!

For now, it looks like you’ve got a stray } just before your generateRandomNumber() method. Another issue might be indentation (or it could be that it’s wasn’t formatted properly as a forum post).

I’m sure @midlindner will find something else. :slight_smile:

1 Like

If I do, it won’t be until tomorrow. It does look like the class is closed, due to the extra curly brace, leaving the last 2 methods outside of it. What error messages, if any, do you see, @py4127846397?

1 Like

Okay, I’ve looked at the project, and run your code in the project. I finally got it to work after working through each error message. You’ll need to do the same. I have very little Java experience, and have not even looked at the Build Basic Android Apps with Java course prior to this. I was able to read the error messages, and make the corrections to get your code to work though, so I’m confident you can do the same. If you need help, ask specific questions, and include the error message along with your code. Keep in mind, that the error messages show you the line of code where the issue is.

The first error message I saw was:

MainActivity.java:25: error: <identifier> expected
  Question question1 = new Question(38373, "In the long run, the greatest weapon of mass
destruction is stupidity."), "Thomas Sowell", "Albert Einstein", "FDR", "Thomas Jefferson" 

The line referenced is number 25:

25  Question question1 = new Question(38373, "In the long run, the greatest weapon of mass destruction is stupidity."), "Thomas Sowell", "Albert Einstein", "FDR", "Thomas Jefferson"
//                                                                                                                   ^
26
27  ,0);

Notice anything odd here? Shouldn’t the entire argument list be inside the parenthesis? I’m also unsure why you have the ,0); two lines below the rest of the statement. Keeping your spacing and indentation clean and consistent will help a lot. Fixing this and the error that @dr_victoria pointed out will get you started.

1 Like

So i went through and was able to fix all errors and get the code to run so it is printing the questions and showing the answers. This may be a dumb question but im unsure on how to invoke the onAnswerSubmission method so I can text if player answers work. Any help on how to do this will be appreciated. Thanks

public void onAnswerSubmission(){

if( getCurrentQuestion().isCorrect() ){

  totalCorrect = totalCorrect + 1;

}

questions.remove(currentQuestionIndex);

// displayQuestionsRemaining(questions.size());

if(questions.size() == 0){

  

  System.out.println("GAME OVER");

  startNewGame();

}else{

  

  chooseNewQuestion();

  // TODO: uncomment after implementing displayQuestion()

// displayQuestion(getCurrentQuestion());

  }

}
2 Likes

Congrats on getting your current code working! I haven’t taken the course, so I haven’t completed the project (or even started it for that matter), but I’ve looked through it a bit. It appears that the finished app will have a submit button on the screen. Clicking the submit button would need to trigger the onAnswerSubmission() method it would seem. If you continue on, you should get to that point eventually.

1 Like