package P4 This file includes 1 Solution to P3 2 Question

package P4;

/* This file includes:

*    1. Solution to P3

* 2. Questions for P4. Comments starting with REQ represent the questions.

*

* Features:

*    - We have from 1 to 3 players

* - We have many questions. Each player may be asked more than one question.

* - User can play many rounds of the game.

*

* Focus:

*    - Arrays and Methods

*

* Aim:

*    - Organize code and avoid code redundancy by the use of methods

*/

public class Main {              

   static Game game;          

  

   //Two arrays for questions and answers (both are global, i.e., accessible by all code in the class).

   //REQ1:   Replace array values with real questions/answers

   static String[] questions = {\"question0\", \"question1\", \"question2\", \"question3\", \"question4\", \"question5\", \"question6\", \"question7\", \"question8\"};

   static String[] answers =    {\"answer0\", \"answer1\", \"answer2\", \"answer3\", \"answer4\", \"answer5\", \"answer6\", \"answer7\", \"answer8\"};

  

   public static void main(String[] args) {

       String ans;

       do{                              

           //Reset the game

           game = new Game();          

          

           //Get number of players (from 1 to 3)

           int numPlayers = game.askForInt(\"How many players\", 1, 3);

           //Add up to 3 players to the game

           for (int i = 0; i < numPlayers; i++) {

               String name = game.askForText(\"What is player \" + i + \" name?\");

               game.addPlayer(name);              

           }

          

           //REQ2:   Call a method to shuffle questions and answers. For that, you need to create a method with the header: void shuffleQuestions();

          

           //REQ3:   - Calculate the maximum number of questions each player could be asked (equals to the number of available questions divided by numPlayers). Store this value in a variable maxQuestions

           //   - Ask the user how many questions should be given to each player. The value read from the user should not exceed MaxQuestion. Store this value in a variable numQuestions.

           //       - Ask each player the next unanswered question (e.g., player 0 gets the first question. If it is answered correctly, then player1 gets the next question in the array, otherwise player1 gets the same question again, and so on).

           //           Assume that an incorrectly answered question will keep popping up until it is correctly answered.

           //          Hint: you need to create a for loop that repeats the below code block numQuestions times.

           //          Hint: you need to have a variable that keeps track of the next question to be offered.

           for (int i = 0; i < numPlayers; i++) {

               game.setCurrentPlayer(i);//draw rectangle around player 0, and currentPlayer = player0

               String answer = game.askForText(questions[i]);

               if(answers[i].equals(answer))

                   game.correct();       //display \"Correct\", increment score, change frame color to green

               else

                   game.incorrect();   //display \"incorrect\", change frame color of player to red

           }  

          

           //Do you want to play again? make sure you get valid input

           ans = game.askForText(\"Play again? (Y/N)\");

           while(ans != null && !ans.toUpperCase().equals(\"Y\") && !ans.toUpperCase().equals(\"N\"))

               ans = game.askForText(\"Invalid input. Play again? (Y/N)\");

       }while(ans.toUpperCase().equals(\"Y\"));   //play again if the user answers \"Y\" or \"y\"

       System.exit(1);    //This statement terminates the program

      

   }

}

Solution

Answer

static String[] questions = {\"Who is the Prime Minister of India?\", \"What does ctrl+s does?\", \"In which direction does sun rises?\", \"Who is the father of Biology?\", \"What is the size of an A4 sheet?\", \"Which animal did human evolved from?\", \"What is the full form of ATM?\", \"What is the tag line of Mahindra?\", \"Tom and Jerry belong to which production?\"};

static String[] answers =    {\"Narendra Modi\", \"Save the file\", \"East\", \"Aristotle\", \"210 × 297 mm\", \"Apes\", \"Automated Teller Machine\", \"We live young, We live free\", \"Turner Entertainment\"};

void shuffleQuestions(int numOfQn)
{
int i,qn_num;
   for(i=1; i<=numOfQn;i++)
   {
   qn_num = rand() % numOfQn;
cout<<questions[qn_num];
   cout<<answers[qn_num];
}
}

int cal_max_qn_num()
{
   int qn_num = 9;
   int num_player = 1;
   int num_qn_can_ask = 0;
cout<<\"Enter the number of players::\";
cin>>num_player;
   num_qn_can_ask = qn_num/num_player;
   return num_qn_can_ask;
}

package P4; /* This file includes: * 1. Solution to P3 * 2. Questions for P4. Comments starting with REQ represent the questions. * * Features: * - We have from
package P4; /* This file includes: * 1. Solution to P3 * 2. Questions for P4. Comments starting with REQ represent the questions. * * Features: * - We have from
package P4; /* This file includes: * 1. Solution to P3 * 2. Questions for P4. Comments starting with REQ represent the questions. * * Features: * - We have from

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site