Cumulative Project 4 - Game Logic Part II

I am working on this and I am trying to set an Arraylist to another, is it possible? If so, how?:face_with_raised_eyebrow:

Here is my code in MainActivity.java:

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 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;
      totalQuestion = question.size();
      questions.set(question);
      Question firstQuestion = chooseNewQuestion();
      // displayQuestion(firstQuestion);
      // displayQuestionsRemaining(questions.size());
    }
    // TODO #4 add chooseNewQuestion() here
    public Question chooseNewQuestion(){
      int index = generateRandomNumber(questions.size);
    }
    // TODO #5 add getCurrentQuestion() here

    // TODO #6 add onAnswerSubmission() here

    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!";
        }
    }
}

Thanks in advance!:smiley:

You can try something like set() maybe?

Here’s documentation:
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

I believe that is what I did.

What was the issue you got?
@yimoliu5061522555

Check the docs for instructions on set:

I think it’s expecting set(int index, E element)

You wrote: questions.set(question); which means it will think question is an index of type int and throw an error if it’s not. You’d still need the second argument.

MainActivity.java:22: error: no suitable method found for set(ArrayList<Question>)
      questions.set(question);
               ^
    method List.set(int,Question) is not applicable
      (actual and formal argument lists differ in length)
    method AbstractList.set(int,Question) is not applicable
      (actual and formal argument lists differ in length)
    method ArrayList.set(int,Question) is not applicable
      (actual and formal argument lists differ in length)

I think I might be able to use a for-each loop for this, thank though!

1 Like

I now have ran into a problem, I am not sure how to get the index of something in an ArrayList. Can you help??

The method indexOf() is your friend.

A crude example:

private int getSomeIndex(String target) {
  return _listYouAreSearchingFrom.indexOf(target);
}

Better examples:

Documentation:
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(int)