Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. Code:
  2.  
  3. TriviaQuestion.java
  4.  
  5. public class TriviaQuestion
  6. {
  7. // private attributes
  8. private String question, answer;
  9. private int value;
  10.  
  11. // constructor
  12. TriviaQuestion(String question, String answer, int value) {
  13. this.question = question;
  14. this.answer = answer;
  15. this.value = value;
  16. }
  17.  
  18. // accessors
  19. public String getQuestion() {
  20. return this.question;
  21. }
  22.  
  23. public String getAnswer() {
  24. return this.answer;
  25. }
  26.  
  27. public int getValue() {
  28. return this.value;
  29. }
  30. }
  31.  
  32. TriviaGame.java
  33.  
  34. import java.util.*;
  35.  
  36. public class TriviaGame
  37. {
  38. // member attributes
  39. private TriviaQuestion questions[];
  40. private int score, questionNum;
  41.  
  42. // constructor
  43. TriviaGame(TriviaQuestion questions[]) {
  44. // to copy data by value
  45. this.questions = Arrays.copyOf(questions, questions.length);
  46. // initial values
  47. this.score = 0;
  48. this.questionNum = 0;
  49. }
  50.  
  51. // asks the next question and returns true if a question was ansked
  52. public boolean askNextQuestion() {
  53.  
  54. // increase questionNum as this is the last question that was asked
  55. this.questionNum++;
  56.  
  57. // return false if it exceeds the number of questions available
  58. if(this.questionNum > questions.length)
  59. return false;
  60.  
  61. // input stream
  62. Scanner sc = new Scanner(System.in);
  63.  
  64. // get the next question, answer and points
  65. // questionNum-1 is used because the array index starts from 0
  66. String question = questions[questionNum-1].getQuestion();
  67. String answer = questions[questionNum-1].getAnswer();
  68. int value = questions[questionNum-1].getValue();
  69.  
  70. // prompt to the user
  71. System.out.println("\nQuestion " + questionNum);
  72. System.out.print("For " + value + " point... ");
  73. System.out.println(question);
  74.  
  75. // input user answer
  76. String answer1 = sc.nextLine();
  77. // check if answer matches
  78. if(answer1.equalsIgnoreCase(answer)) {
  79.  
  80. System.out.println("That is correct!");
  81. this.score += value;
  82. }
  83. else
  84. System.out.println("Wrong. The correct answer is " + answer);
  85.  
  86. // print score and return true
  87. showScore();
  88. return true;
  89. }
  90.  
  91. // function to print score
  92. public void showScore() {
  93. System.out.println("Your score is " + this.score);
  94. }
  95.  
  96. // reset values for a new game
  97. public void restart() {
  98.  
  99. this.score = 0;
  100. this.questionNum = 0;
  101. }
  102.  
  103. // accessor to get score
  104. public int getScore() {
  105. return this.score;
  106. }
  107. }
  108.  
  109. TriviaDriver.java
  110.  
  111. import java.util.Scanner;
  112.  
  113. public class TriviaDriver
  114. {
  115. // starter main function
  116. public static void main(String[] args) {
  117.  
  118. // initialize Scanner object and pass it to playGame()
  119. Scanner sc = new Scanner(System.in);
  120. new Main().playGame(sc);
  121. }
  122.  
  123. // plays games
  124. public void playGame(Scanner sc) {
  125.  
  126. // to keep record of games played and sum of all the scores
  127. int gamesPlayed = 0, totalScore = 0;
  128. double avgScore;
  129.  
  130. // define TriviaGame object
  131. TriviaGame tg = new TriviaGame(populateQuestions());
  132.  
  133. // play till player says no
  134. while(true) {
  135.  
  136. // play a game
  137. gamesPlayed++;
  138. while(tg.askNextQuestion()){}
  139.  
  140. // add to totalScore
  141. totalScore += tg.getScore();
  142. // reset game
  143. tg.restart();
  144.  
  145. System.out.println("Game over! Would you like to play again?\n(Yes/No):");
  146. String choice = sc.next();
  147. if(choice.equalsIgnoreCase("No") || !choice.equalsIgnoreCase("Yes"))
  148. {
  149. // end
  150. break;
  151. }
  152. }
  153. // print the required data
  154. avgScore = (double) totalScore/gamesPlayed;
  155. System.out.println("You played " + gamesPlayed + " games and your average score was " + avgScore
  156. + ". Thank you for playing, have a great day!");
  157. }
  158.  
  159. // function to populate the TriviaQuestion[] array for TriviaGame
  160. public TriviaQuestion[] populateQuestions() {
  161.  
  162. // create TriviaQuestion objects and make an array of them and return
  163. TriviaQuestion ob1 = new TriviaQuestion
  164. ("The first Pokemon that Ash receives from Professfor Oak", "pikachu", 1);
  165.  
  166. TriviaQuestion ob2 = new TriviaQuestion
  167. ("Erling Kagge skiied into here alone on January 7, 1993", "south pole", 2);
  168.  
  169. TriviaQuestion ob3 = new TriviaQuestion
  170. ("1997 British band that produced 'Tub Thumper'", "chumbawumba", 2);
  171.  
  172. TriviaQuestion ob4 = new TriviaQuestion
  173. ("Who is the tallest person on record (8 ft. 11 in) that has lived?", "Robert Wadlow", 3);
  174.  
  175. TriviaQuestion ob5 = new TriviaQuestion
  176. ("PT Barnum said 'This way to the _____' to attract people to the exit.", "egress", 1);
  177.  
  178. return new TriviaQuestion[]{ob1, ob2, ob3, ob4, ob5};
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement