Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USE PYTHON PLEASE -) In this programming exercise you will create a simple trivi

ID: 3885949 • Letter: U

Question

USE PYTHON PLEASE -)

In this programming exercise you will create a simple trivia game for two players. The program will work like this:

Starting with player 1, each player gets a turn at answering 5 trivia questions. (There should be a total of 10 questions.) When a question is displayed, 4 possible answers are also displayed. Only one of the answers is correct, and if the player selects the correct answer, he or she earns a point.

After answers have been selected for all the questions, the program displays the number of points earned by each player and declares the player with the highest number of points the winner.

To create this program, write a Question class to hold the data for a trivia question. The Question class should have attributes for the following data:

A trivia question

Possible answer 1

Possible answer 2

Possible answer 3

Possible answer 4

The number of the correct answer (1, 2, 3, or 4)

The Question class also should have an appropriate __init__ method, accessors, mutators and an __str__ to display the question and possible choices.

The program should have a list or a dictionary containing 10 Question objects, one for each trivia question. Make up your own trivia questions on the subject or subjects of your choice for the objects.

Explanation / Answer

Hello This code is is in java and pretty correct what you have asked and take the reference of this

List<Question> questions;

public List<Question> getQuestions()

{

return questions;

}

TrivaGame() throws IOException

{

questions = readQuestions();

}

class Question

{

private String question;

private List<String> possibleAnswers;

private int answer;

@Override

public String toString()

{

return "Question [question=" + question + ", possibleAnswers="+ possibleAnswers + ", answer=" + answer + "]";

}

}

class Player

{

int playerNumber;

int points;

}

Function<String, Question> mapLineToQuestion = new Function<String, Question>()

{

public Question apply(String line)

{

Question question = new Question();

List<String> questionPieces = Splitter.on("|").trimResults().omitEmptyStrings().splitToList(line);

question.question = questionPieces.get(0);

question.possibleAnswers = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(questionPieces.get(1));

question.answer = Integer.parseInt(questionPieces.get(2));

return question;

}

};

public List<Question> readQuestions() throws IOException

{

List<Question> questions = Files.lines(Paths.get("src/main/resources/com/levelup/java/exercises/beginner/trivia.txt")).map(mapLineToQuestion).collect(Collectors.toList());

return questions;

}

public static int getRandomQuestionNumber(int numberOfQuestions)

{

Random random = new Random();

OptionalInt questionNumber = random.ints(1, numberOfQuestions).findFirst();

return questionNumber.getAsInt();

}

public static void displayQuestion(Question q, int playerNum)

{

System.out.println("Question for player #" + playerNum);

System.out.println("------------------------");

System.out.println(q.question);

for (int i = 0; i < q.possibleAnswers.size(); i++)

{

System.out.println((i + 1) + ". " + q.possibleAnswers.get(i));

}

}

public static void showGameResults(Player[] players)

{

    System.out.println("Game Over!");

    System.out.println("---------------------");

    System.out.println("Player 1's points: " + players[0].points);

    System.out.println("Player 2's points: " + players[1].points);

    if (players[0].points > players[1].points)

{

System.out.println("Player 1 wins!");

    } else if (players[1].points > players[0].points)

{

System.out.println("Player 2 wins!");

    } else

{

System.out.println("It's a TIE!");

}

}

static int NUMBER_OF_PLAYERS = 2;

static int NUMBER_OF_CHANCES = 5;

public static void main(String args[]) throws IOException

{

TrivaGame trivaGame = new TrivaGame();

int numberOfQuestions = trivaGame.getQuestions().size();

Player[] players = { trivaGame.new Player(), trivaGame.new Player()

};

for (int x = 0; x < players.length; x++)

{

Player currentPlayer = players[x];

for (int i = 0; i < NUMBER_OF_CHANCES; i++)

{

Question question = trivaGame.getQuestions().get(

getRandomQuestionNumber(numberOfQuestions));

displayQuestion(question, x + 1);

System.out.print("Enter the number of the correct answer: ");

int currentAnswer = keyboard.nextInt();

if (currentAnswer == question.answer)

{

System.out.println("Correct! ");

currentPlayer.points += 1;

} else

{

System.out.println("Sorry, that is incorrect. The correct "+ "answer is " + question.answer + ". ");

}

}

}

    keyboard.close();     

showGameResults(players);   

}