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

Create a simple trivia game for 2 players. The program will work like this: Desi

ID: 3567938 • Letter: C

Question

Create a simple trivia game for 2 players. The program will work like this:

Design a Question class to hold the data for a trivia question. The Question class should have String fields for the following data:

The Question class should have appropriate constructor(s), accessors, and mutator methods. The program should create an ArrayList of 10 Question objects, one for each trivia question. Make up your own trivia questions on the subject of your choice.

Explanation / Answer

public List getQuestions() { return questions; } TrivaGame() throws IOException { questions = readQuestions(); } /** * This class represents question object */ class Question { private String question; private List possibleAnswers; private int answer; @Override public String toString() { return "Question [question=" + question + ", possibleAnswers=" + possibleAnswers + ", answer=" + answer + "]"; } } /** * This class represents player object */ class Player { int playerNumber; int points; } /** * Function will accept a string based on the following format and transform * it into a Question object */ Function mapLineToQuestion = new Function() { public Question apply(String line) { Question question = new Question(); List 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; } }; /** * Method will read each line of a file then mapping it to a question * object. * * @return * @throws IOException */ public List readQuestions() throws IOException { List questions = Files .lines(Paths .get("src/main/resources/com/levelup/java/exercises/beginner/trivia.txt")) .map(mapLineToQuestion).collect(Collectors.toList()); return questions; } /** * Method should generate random number based total number of questions * * @param numberOfQuestions * @return */ public static int getRandomQuestionNumber(int numberOfQuestions) { Random random = new Random(); OptionalInt questionNumber = random.ints(1, numberOfQuestions) .findFirst(); return questionNumber.getAsInt(); } /** * Method should display a question passed * * @param q * @param playerNum */ public static void displayQuestion(Question q, int playerNum) { // Display the player number. System.out.println("Question for player #" + playerNum); System.out.println("------------------------"); // Display the question. System.out.println(q.question); for (int i = 0; i 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 { // Initiate trivia game TrivaGame trivaGame = new TrivaGame(); Scanner keyboard = new Scanner(System.in); // how many total questions exist int numberOfQuestions = trivaGame.getQuestions().size(); // create array of players Player[] players = { trivaGame.new Player(), trivaGame.new Player() }; // Play the game for each player defined and number of chances for (int x = 0; x
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote