-3

for an assignment in my Java class I'm supposed to write a program for that creates a Tria Game between two players and I have the program done but she wants us to add on to it too. "Complete the Question class as is described. Add a method that turns the array of 10 answers into a 2D array and returns the 2D array.

In the tester, add the information for each player's answered questions into a 2d array (2 players, 5 questions each).

Display the correct answers (call the new method in Question) and then display the 2D array with player's answers. Finally, display the points for each player and the player who won."

Any help with how I can go about doing this? I appreciate any help that you can give me.

Here is my code so far, first is my class called "Question" and second is the tester called "Trivia Game"

public class Question
{
  String question;
  String possibleAns1;
  String possibleAns2;
  String possibleAns3;
  String possibleAns4;
  int CorrectAnsNum;

  Question(String ques, String ans1, String ans2, String ans3, String ans4, int num)
  {
    question = ques;
    possibleAns1 = ans1;
    possibleAns2 = ans2;
    possibleAns3 = ans3;
    possibleAns4 = ans4;
    CorrectAnsNum = num;
  }

  Question(){
  }

  String getQuestion(){
    return question;
  }

  String getAns1(){
    return possibleAns1;
  }

  String getAns2(){
    return possibleAns2;
  }

  String getAns3(){
    return possibleAns3;
  }

  String getAns4(){
    return possibleAns4;
  }

  int getCorrectAnsNum(){
    return CorrectAnsNum;
  }

  void setQuestion(String newQuestion){
    question = newQuestion;
  }

  void setAns1(String newPossibleAns1){
    possibleAns1 = newPossibleAns1;
  }

  void setAns2(String newPossibleAns2){
    possibleAns2 = newPossibleAns2;
  }

  void setAns3(String newPossibleAns3){
    possibleAns3 = newPossibleAns3;
  }

  void setAns4(String newPossibleAns4){
    possibleAns4 = newPossibleAns4;
  }

  void setCorrectAnsNum(int newCorrectAnsNum){
    CorrectAnsNum = newCorrectAnsNum;
  }
}

///////////TESTER

import java.util.ArrayList;
import java.util.Scanner;

public class TriviaGame
{
  public static void main(String args[])
  {
    Scanner input = new Scanner(System.in);

    int pointsPlayer1 = 0;
    int pointsPlayer2 = 0;

    Question q1 = new Question("How many days are there in a week?", "5", "2", "7", "57", 3);
    Question q2 = new Question("What is the first month of the year?", "February", "January", "December", "August", 2);
    Question q3 = new Question("How many hours are there in one day?", "32", "12", "0", "24", 4);
    Question q4 = new Question("What is the capital of Massachusetts?", "Framingham", "Worcester", "Boston", "Springfield", 3);
    Question q5 = new Question("At what age are you legally aloud to buy lottery tickets", "18", "16", "21", "19", 1);
    Question q6 = new Question("What sport can you hit a homerun in?", "Golf", "Football", "Soccer", "Baseball", 4);
    Question q7 = new Question("Which one of these fruits is yellow?", "Apple", "Banana", "Strawberry", "Grape", 2);
    Question q8 = new Question("Who is the current president of the United States?", "Bill Clinton", "George Bush", "Barack Obama", "Jason Statham", 3);
    Question q9 = new Question("What is the highway speed limit in most areas?", "85", "65", "35", "110", 2);

    Question q10 = new Question();
    q10.setQuestion("What is the company that makes the iPod?");

    q10.setAns1("Microsoft");
    q10.setAns2("Verizon");
    q10.setAns3("Apple");
    q10.setAns4("Walmart");
    q10.setCorrectAnsNum(3);

    ArrayList<Question> questionList = new ArrayList<Question>();

    questionList.add(q1);
    questionList.add(q2);
    questionList.add(q3);
    questionList.add(q4);
    questionList.add(q5);
    questionList.add(q6);
    questionList.add(q7);
    questionList.add(q8);
    questionList.add(q9);
    questionList.add(q10);

    System.out.println("****Player1****");

    for(int i = 0; i < 5; i++)
    {
      Question q = questionList.get(i);

      System.out.println("\n" + (i+1) + ". " + q.getQuestion());
      System.out.println("1) " + q.getAns1());
      System.out.println("2) " + q.getAns2());
      System.out.println("3) " + q.getAns3());
      System.out.println("4) " + q.getAns4());

      System.out.print("Enter your answer: ");
      int userAns = input.nextInt();

      if (userAns == q.getCorrectAnsNum())
        pointsPlayer1++;
    }

    System.out.println("\n****Player2****");

    for(int i = 5; i < 10; i++)
    {
      Question q = questionList.get(i);

      System.out.println("\n" + (i+1) + ". " + q.getQuestion());
      System.out.println("1) " + q.getAns1());
      System.out.println("2) " + q.getAns2());
      System.out.println("3) " + q.getAns3());
      System.out.println("4) " + q.getAns4());

      System.out.print("Enter your answer: ");
      int userAns = input.nextInt();

      if (userAns == q.getCorrectAnsNum())
        pointsPlayer2++;
    }

    System.out.println("\n****Points Earned****");
    System.out.println("Player 1: " + pointsPlayer1);
    System.out.println("Player 2: " + pointsPlayer2);

    if (pointsPlayer1>pointsPlayer2)
      System.out.println("\nPlayer 1 is the winner! ");
    else if (pointsPlayer1<pointsPlayer2)
      System.out.println("\nPlayer 2 is the winner! ");
    else
      System.out.println("\nIt's a tie! ");
  }
}
4
  • 4
    This site is not about doing the homeworks for someone else, but to help you at a specific problem. Please be more specific on your question and tell us where you are stuck.
    – Yogu
    May 4, 2012 at 20:11
  • I'm sorry if I've come across that way but I've attached the work I've done so far all I'm asking for is advice on how to finish it. I never asked for someone to "do my homework". In the question I've put in parentheses the part that I'm confused on
    – Andrew
    May 4, 2012 at 20:47
  • So where exactly is the problem? Can you define that array? Do you know what to put in? Do you know how to fill the array? Do you know how to output it? And do you know when to output it?
    – Yogu
    May 4, 2012 at 21:17
  • ok so my program is using arrays to ask 2 different players 5 questions each. Each question has 4 answers and they use the number to answer the question. At the end it will display the winner and with how many points each player had for every correct answer. The program works but the part that I'm asking about is something that I'm supposed to add to this. I'm supposed to add a method to my "Question" class that turns the array of 10 answers into a 2D array and returns the 2D array. I'm just wondering how to approach as I don't really understand 2D arrays. That is my question
    – Andrew
    May 4, 2012 at 21:34

1 Answer 1

2

In java, you can declare arrays of arrays, which then are two-dimensional:

String[][] allAnswers = new String[10][4];

Now, you can iterate through the outer array:

for (String[] questionAnswers : allAnswers) {

The variable questionAnswers is an array of String. You can then iterate through this inner array, and set the answers:

questionAnswers[0] = "Answer 1";
// ...

Note that arrays in java are always zero-based, so the first answer belongs to index 0.

You also can access a specific field using the index operators:

System.out.println("Second answer of question 3 is: " questionAnswers[2][1];

If this was not quite clear to you, you should read a tutorial on multidimensional arrays in java.

1
  • Thanks, I think you've pretty much cleared it up. I appreciate it :)
    – Andrew
    May 5, 2012 at 2:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.