Guest User

Untitled

a guest
Mar 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. package game;
  2.  
  3. import game.TimeCounter;
  4. import game.exercise.Examiner;
  5.  
  6. public class Game {
  7. public static void main(String[] args) {
  8. System.out.println("You have to solve as many exercises as you can " +
  9. "within 20 seconds");
  10. Examiner examiner = new Examiner();
  11. examiner.start();
  12. TimeCounter timeCounter = new TimeCounter(examiner, 20);
  13. timeCounter.countTimeAndInterrupt();
  14. examiner.printStats();
  15. }
  16. }
  17.  
  18. package game;
  19.  
  20. import game.exercise.Examiner;
  21.  
  22. public class TimeCounter {
  23. private Examiner examiner;
  24. private int seconds;
  25.  
  26. public TimeCounter(Examiner examiner, int seconds) {
  27. this.examiner = examiner;
  28. this.seconds = seconds;
  29. }
  30.  
  31.  
  32.  
  33. public void countTimeAndInterrupt() {
  34. for(int i = 0; i < seconds; i++) {
  35. try {
  36. Thread.sleep(1000);
  37. } catch (InterruptedException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. examiner.stop();
  42. }
  43. }
  44.  
  45. package game.exercise;
  46.  
  47. import java.util.Scanner;
  48.  
  49. public class Examiner extends Thread {
  50. private int right; // number of exercises solved right
  51. private int wrong;
  52.  
  53. @Override
  54. public void run() {
  55. Scanner scanner = new Scanner(System.in);
  56.  
  57. while (true) {
  58. int input;
  59. Exercise exercise;
  60.  
  61. exercise = Exercise.getRandomExercise();
  62. System.out.print(exercise);
  63. input = scanner.nextInt();
  64. if (exercise.check(input)) {
  65. right++;
  66. System.out.println("Right!");
  67.  
  68. } else {
  69. wrong++;
  70. System.out.println("Wrong!");
  71. }
  72. }
  73. }
  74.  
  75. public void printStats() {
  76. System.out.println();
  77. System.out.println("Number of all exercises: " +
  78. getNumberOfExercises());
  79. System.out.println("Exercises solved right: " + right);
  80. System.out.println("Exercises solved wrong: " + wrong);
  81. int percentage = (int)((float)(right) / getNumberOfExercises() * 100);
  82. System.out.println("Rate: " + percentage + " % ");
  83. }
  84.  
  85. public int getNumberOfExercises() {
  86. return right + wrong;
  87. }
  88. }
  89.  
  90. package game.exercise;
  91.  
  92. import java.util.Random;
  93.  
  94. public class Exercise {
  95. private static Random random = new Random();
  96. private int number1;
  97. private int number2;
  98. private Type type;
  99.  
  100. public Exercise(Type type, int number1, int number2) {
  101. this.type = type;
  102. this.number1 = number1;
  103. this.number2 = number2;
  104. }
  105.  
  106. public static Exercise getRandomExercise() {
  107. return getRandomExercise(Type.random());
  108. }
  109.  
  110. public static Exercise getRandomExercise(Type type) {
  111. switch(type) {
  112. case ADDITION:
  113. case SUBTRACTION:
  114. return new Exercise(type, random.nextInt(40) + 1,
  115. random.nextInt(40) + 1);
  116. case MULTIPLICATION:
  117. return new Exercise(type, random.nextInt(20) + 1,
  118. random.nextInt(20) + 1);
  119. case DIVISION:
  120. return new Exercise(type, random.nextInt(100) + 1,
  121. random.nextInt(15) + 1);
  122. default: throw new IllegalArgumentException("not a valid type");
  123. }
  124. }
  125.  
  126. @Override
  127. public String toString() {
  128. return number1 + " " + getOperator() + " " + number2 + " = ";
  129. }
  130.  
  131. private String getOperator() {
  132. switch(type) {
  133. case ADDITION: return "+";
  134. case SUBTRACTION: return "-";
  135. case MULTIPLICATION: return "*";
  136. case DIVISION: return "/";
  137. default: throw new IllegalArgumentException("not a valid type");
  138. }
  139. }
  140.  
  141. private int getSolution() {
  142. switch(type) {
  143. case ADDITION: return number1 + number2;
  144. case SUBTRACTION: return number1 - number2;
  145. case MULTIPLICATION: return number1 * number2;
  146. case DIVISION: return (int)(number1 / number2);
  147. default: throw new IllegalArgumentException("not a valid type");
  148. }
  149. }
  150.  
  151. public boolean check(int guess) {
  152. return getSolution() == guess;
  153. }
  154. }
  155.  
  156. package game.exercise;
  157.  
  158. import java.util.Random;
  159.  
  160. public enum Type {
  161. ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION;
  162.  
  163. public static Type random() {
  164. Random random = new Random();
  165. int choice = random.nextInt(4);
  166. if(choice == 0) return ADDITION;
  167. else if(choice == 1) return SUBTRACTION;
  168. else if(choice == 2) return MULTIPLICATION;
  169. else if(choice == 3) return DIVISION;
  170. else throw new IllegalArgumentException();
  171. }
  172. }
Add Comment
Please, Sign In to add comment