Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Code:
- TriviaQuestion.java
- public class TriviaQuestion
- {
- // private attributes
- private String question, answer;
- private int value;
- // constructor
- TriviaQuestion(String question, String answer, int value) {
- this.question = question;
- this.answer = answer;
- this.value = value;
- }
- // accessors
- public String getQuestion() {
- return this.question;
- }
- public String getAnswer() {
- return this.answer;
- }
- public int getValue() {
- return this.value;
- }
- }
- TriviaGame.java
- import java.util.*;
- public class TriviaGame
- {
- // member attributes
- private TriviaQuestion questions[];
- private int score, questionNum;
- // constructor
- TriviaGame(TriviaQuestion questions[]) {
- // to copy data by value
- this.questions = Arrays.copyOf(questions, questions.length);
- // initial values
- this.score = 0;
- this.questionNum = 0;
- }
- // asks the next question and returns true if a question was ansked
- public boolean askNextQuestion() {
- // increase questionNum as this is the last question that was asked
- this.questionNum++;
- // return false if it exceeds the number of questions available
- if(this.questionNum > questions.length)
- return false;
- // input stream
- Scanner sc = new Scanner(System.in);
- // get the next question, answer and points
- // questionNum-1 is used because the array index starts from 0
- String question = questions[questionNum-1].getQuestion();
- String answer = questions[questionNum-1].getAnswer();
- int value = questions[questionNum-1].getValue();
- // prompt to the user
- System.out.println("\nQuestion " + questionNum);
- System.out.print("For " + value + " point... ");
- System.out.println(question);
- // input user answer
- String answer1 = sc.nextLine();
- // check if answer matches
- if(answer1.equalsIgnoreCase(answer)) {
- System.out.println("That is correct!");
- this.score += value;
- }
- else
- System.out.println("Wrong. The correct answer is " + answer);
- // print score and return true
- showScore();
- return true;
- }
- // function to print score
- public void showScore() {
- System.out.println("Your score is " + this.score);
- }
- // reset values for a new game
- public void restart() {
- this.score = 0;
- this.questionNum = 0;
- }
- // accessor to get score
- public int getScore() {
- return this.score;
- }
- }
- TriviaDriver.java
- import java.util.Scanner;
- public class TriviaDriver
- {
- // starter main function
- public static void main(String[] args) {
- // initialize Scanner object and pass it to playGame()
- Scanner sc = new Scanner(System.in);
- new Main().playGame(sc);
- }
- // plays games
- public void playGame(Scanner sc) {
- // to keep record of games played and sum of all the scores
- int gamesPlayed = 0, totalScore = 0;
- double avgScore;
- // define TriviaGame object
- TriviaGame tg = new TriviaGame(populateQuestions());
- // play till player says no
- while(true) {
- // play a game
- gamesPlayed++;
- while(tg.askNextQuestion()){}
- // add to totalScore
- totalScore += tg.getScore();
- // reset game
- tg.restart();
- System.out.println("Game over! Would you like to play again?\n(Yes/No):");
- String choice = sc.next();
- if(choice.equalsIgnoreCase("No") || !choice.equalsIgnoreCase("Yes"))
- {
- // end
- break;
- }
- }
- // print the required data
- avgScore = (double) totalScore/gamesPlayed;
- System.out.println("You played " + gamesPlayed + " games and your average score was " + avgScore
- + ". Thank you for playing, have a great day!");
- }
- // function to populate the TriviaQuestion[] array for TriviaGame
- public TriviaQuestion[] populateQuestions() {
- // create TriviaQuestion objects and make an array of them and return
- TriviaQuestion ob1 = new TriviaQuestion
- ("The first Pokemon that Ash receives from Professfor Oak", "pikachu", 1);
- TriviaQuestion ob2 = new TriviaQuestion
- ("Erling Kagge skiied into here alone on January 7, 1993", "south pole", 2);
- TriviaQuestion ob3 = new TriviaQuestion
- ("1997 British band that produced 'Tub Thumper'", "chumbawumba", 2);
- TriviaQuestion ob4 = new TriviaQuestion
- ("Who is the tallest person on record (8 ft. 11 in) that has lived?", "Robert Wadlow", 3);
- TriviaQuestion ob5 = new TriviaQuestion
- ("PT Barnum said 'This way to the _____' to attract people to the exit.", "egress", 1);
- return new TriviaQuestion[]{ob1, ob2, ob3, ob4, ob5};
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement