Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Need help with my Java assignment

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with my Java assignment

    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;
    Last edited by prasaad242; June 27th, 2020 at 10:04 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with my Java assignment

    this is where im stuck
    Please explain what the problem is.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    prasaad242 (June 27th, 2020)

  4. #3
    Junior Member
    Join Date
    Jun 2020
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my Java assignment

    Hi! Thanks for the response! Have edited my original post based on what you said. Sorry as this is my first time using a forum to ask for help. I'm just starting to learn code and appreciate your patience!

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with my Java assignment

    Note: code tags use [] not <>s

    MainActivity.java:30: error: non-static method get(int) cannot be referenced from a static context
    return ArrayList.get(currentQuestionIndex);
    You need to use a reference to an instance of an ArrayList not the name of the class: ArrayList. For example: questions.get(....
    Using the class name with its method is the way to call a static method in the class.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Jun 2020
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my Java assignment

    Quote Originally Posted by Norm View Post
    Note: code tags use [] not <>s


    You need to use a reference to an instance of an ArrayList not the name of the class: ArrayList. For example: questions.get(....
    Using the class name with its method is the way to call a static method in the class.
    Hi! Once again thanks for the quick reponse! I'm still abit lost. I tried changing it to this:

    public Question chooseNewQuestion() {
          int randomQuestion = generateRandomNumber(totalQuestions - 1);
          currentQuestionIndex = randomQuestion;
          return questions.get(currentQuestionIndex);

    but i still get an error message as shown below:

    [error]
    MainActivity.java:30: error: incompatible types: String cannot be converted to Question
    return questions.get(currentQuestionIndex);
    ^
    1 error
    [/error]
    I don't understand why as the .get() method uses the index of the arraylist to get the question inside right?

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with my Java assignment

    How is the variable questions declared? Can you post the code that shows?

    It appears as if questions contains String but the method is supposed to return a Question object.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Jun 2020
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my Java assignment

    Quote Originally Posted by Norm View Post
    How is the variable questions declared? Can you post the code that shows?

    It appears as if questions contains String but the method is supposed to return a Question object.
    This is the code in 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;
        }
    }

    The instructions are for MainActivity.java which was posted in the original post.
    i didnt actually declare the variable questions. if you look at the step 2 in the original post, it doesn't tell me to declare a variable called questions but rather an arraylist.

    ArrayList<String> questions;

    Am i supposed to declare the variable? Thanks for taking it slow with me! Very much appreciated!

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with my Java assignment

    The error message:
    MainActivity.java:30: error: incompatible types: String cannot be converted to Question
    return questions.get(currentQuestionIndex);
    Says that the value returned by the get() method (String) can not be converted to the type that the chooseNewQuestion method is supposed to return (Question).
    Can the ArrayList: questions be changed to hold Question objects instead of String objects? Then the get method would return a Question and that would agree with what the chooseNewQuestion method is supposed to return.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Jun 2020
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my Java assignment

    Oh my god you are an absolute godsend! Changed it to this and it worked! Thanks so much!

     ArrayList<Question> questions;

Similar Threads

  1. Behaviour of shortcut assignment and normal assignment for float
    By rakeshkr2 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 20th, 2014, 02:54 AM
  2. Java assignment help!
    By frodooftheshire in forum Java Theory & Questions
    Replies: 2
    Last Post: September 26th, 2013, 03:30 PM
  3. Replies: 8
    Last Post: February 12th, 2013, 05:45 AM
  4. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM