+1 (315) 557-6473 

Java Program to Create Trivia Question Sytem Assignment Solution.


Instructions

Objective
Write a java assignment program program to create trivia question sytem.

Requirements and Specifications

program to create trivia question sytem in java

Source Code

import java.util.concurrent.*;

/**

   This program tests the TriviaQuestion class.

   @author Maggie

   1/8/2018

*/

public class TriviaQuiz

{

 // Question data (to facilitate testing)

 public static String q2 = "Who was Alan Turing? ";

 public static String q2ans1 = "first-team midfielder for Manchester United";

 public static String q2ans2 = "the father of theoretical computer science";

 public static String q2ans3 = "the true name of the Red Baron";

 public static String q2ans4 = "19th century Scottish playwright";

 public static int q2correct = 2;

 public static int q2points = 10;

 public static String q3 = "Which 2017 release starred Gal Gadot? ";

 public static String q3ans1 = "The Last Jedi";

 public static String q3ans2 = "Wonder Woman";

 public static String q3ans3 = "Blade Runner 2049";

 public static String q3ans4 = "Molly's Game";

 public static int q3correct = 2;

 public static int q3points = 10;

   public static int getAnswerCountdown() throws InterruptedException {

    String answer;

    ConsoleInput reader = new ConsoleInput(1000,5,TimeUnit.SECONDS);

    answer = reader.readLine();

    return Integer.parseInt(answer);

   }

   /**

       A method to exercise the TriviaQuestion class.

 * @throws InterruptedException

   */

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

   {

   // Make three questions

  TriviaQuestion question1, question2, question3;

   // Create a variable for user input.

   int answer;

   // Variable for receiving user's score.

   double score;

      // Create three questions.

      question1 = new TriviaQuestion();

   question2 = new TriviaQuestion(q2, q2ans1, q2ans2, q2ans3, q2ans4, q2correct, q2points);

   question3 = new TriviaQuestion(q3, q3ans1, q3ans2, q3ans3, q3ans4, q3correct, q3points);

   // Pose each question, get an answer, report points.

   System.out.println(question1.poseQuestion());

   answer = getAnswerCountdown();

   score = question1.getScore(answer);

   System.out.println("You scored " + score + " points.\n");

   System.out.println(question2.poseQuestion());

   answer = getAnswerCountdown();

   score = question2.getScore(answer);

   System.out.println("You scored " + score + " points.\n");

   System.out.println(question3.poseQuestion());

   answer = getAnswerCountdown();

   score = question3.getScore(answer);

   System.out.println("You scored " + score + " points.");

   }

}

TRIVIA QUESTIONS

public class TriviaQuestion {

    // Fields

    private String question;

    private String answer1;

    private String answer2;

    private String answer3;

    private String answer4;

    private int correctAnswer;

    private int points;

    private boolean posed;

    // Getters and setters

    public String getQuestion() {

        return this.question;

    }

    public void setQuestion(String question) {

        if(!posed)

            this.question = question;

    }

    public String getAnswer1() {

        return this.answer1;

    }

    public void setAnswer1(String answer1) {

        if(!posed)

            this.answer1 = answer1;

    }

    public String getAnswer2() {

        return this.answer2;

    }

    public void setAnswer2(String answer2) {

        if(!posed)

            this.answer2 = answer2;

    }

    public String getAnswer3() {

        return this.answer3;

    }

    public void setAnswer3(String answer3) {

        if(!posed)

            this.answer3 = answer3;

    }

    public String getAnswer4() {

        return this.answer4;

    }

    public void setAnswer4(String answer4) {

        if(!posed)

            this.answer4 = answer4;

    }

    public int getPoints() {

        return this.points;

    }

    public void setPoints(int points) {

        if(!posed)

            this.points = points;

    }

    // Default constructor

    public TriviaQuestion() {

        // Initialize fields

        question = "What is the first letter of the alphabet?";

        answer1 = "J";

        answer2 = "Z";

        answer3 = "M";

        answer4 = "A";

        correctAnswer = 4;

        points = 10;

        posed = false;

    }

    // Overloaded constructor

    public TriviaQuestion(String q, String qans1, String qans2, String qans3, String qans4, int qcorrect, int qpoints)

    {

        question = q;

        answer1 = qans1;

        answer2 = qans2;

        answer3 = qans3;

        answer4 = qans4;

        correctAnswer = qcorrect;

        points = qpoints;

        posed = false;

    }

    public String poseQuestion()

    {

        // This function displays the questions with its choices

        String ret = question + "\n";

        ret += "1) " + answer1 + "\n";

        ret += "2) " + answer2 + "\n";

        ret += "3) " + answer3 + "\n";

        ret += "4) " + answer4 + "\n";

        posed = true;

        return ret;

    }

    public double getScore(int answer_idx)

    {

        int score = 0;

        if(answer_idx == correctAnswer && posed)

            score = points;

        return score;

    }

}