Hi i am currently doing a course with codecademy for android app building. Part of it is coming up with the logic behind the game through java. However, i dont understand the instructions. it is a simple quiz game with 4 options per question. However, i keep getting error messages. i will post the steps provided by the school followed by my code.
Track three key integers.
Step 1 :Inside of MainActivity, add 3 integer member variables to the MainActivity object at TODO #1:
currentQuestionIndex
totalCorrect
totalQuestions
Step 2: Create an ArrayList object variable named questions below the integers declared in task 1 (TODO #2). The ArrayList will contain objects of the Question class.
Step 3: Define startNewGame() inside of the MainActivity class at TODO #3.
Step 4: create 3 or more Question objects within startNewGame()
Step 5: In startNewGame(), create a new ArrayList and assign it to the questions member variable you defined in task 2. Then, add all of your Question objects to the ArrayList.
Step 6: Reset the totals within startNewGame().
Step 7: Copy and paste some code.
Question firstQuestion = chooseNewQuestion();
// displayQuestion(firstQuestion);
// displayQuestionsRemaining(questions.size());
Step 8: Define the chooseNewQuestion() method which returns a Question object and requires no parameters at TODO #4 in MainActivity.
Step 9: Within chooseNewQuestion(), use the generateRandomNumber() method to pick a number between 0 and one less than the number of Question objects in your questions ArrayList.
Step 10: Modify currentQuestionIndex to reflect the index of the randomly chosen Question object.
Step 11: Use currentQuestionIndex and the questions ArrayList to return the appropriate Question object from chooseNewQuestion().
There are more steps but this is where im stuck. Below is my code. Most of the methods below are defined in other pages.
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 public static void startNewGame(String Question) { Question question1 = new Question(921238, "How tall is the Empire State Building?", "1200 ft", "1553 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 question3 = new Question(748294, "What is the name for the patch of skin found on your elbow?", "Elbow Skin", "Wenis", "Fascia Elbora", "Todd", 1); ArrayList<Question> questions = new ArrayList<>(); 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 randomQuestion = generateRandomNumber(totalQuestions - 1); currentQuestionIndex = randomQuestion; return ArrayList.get(currentQuestionIndex);
Edit: What is the point of Step 2 and Step 5? and When i run my code after step 11 i get two errors as shown below:
<error>
MainActivity.java:30: error: non-static method get(int) cannot be referenced from a static context
return ArrayList.get(currentQuestionIndex);
^
where E is a type-variable:
E extends Object declared in class ArrayList
MainActivity.java:30: error: incompatible types: Object cannot be converted to Question
return ArrayList.get(currentQuestionIndex);
^
2 errors
</error>
EDIT: I changed 2 things and it worked out all thanks to the moderator who patiently guided me:
return ArrayList.get(currentQuestionIndex);
to
return questions.get(currentQuestionIndex);
and
ArrayList<String> questions;
to
ArrayList<Question> questions;