fork(13) download
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import java.util.Random;
  5.  
  6. public class Main {
  7.  
  8. private static final int NUMBER_OF_QUESTIONS = 10;
  9. private static final int MIN_QUESTION_ELEMENTS = 2;
  10. private static final int MAX_QUESTION_ELEMENTS = 4;
  11. private static final int MIN_QUESTION_ELEMENT_VALUE = 1;
  12. private static final int MAX_QUESTION_ELEMENT_VALUE = 100;
  13. private final Random randomGenerator = new Random();
  14.  
  15. public static void main(String[] args) {
  16. Main questionGenerator = new Main();
  17. List<Question> randomQuestions = questionGenerator.getGeneratedRandomQuestions();
  18. for (Question question : randomQuestions) {
  19. System.out.println(question);
  20. }
  21. }
  22.  
  23. public List<Question> getGeneratedRandomQuestions() {
  24. List<Question> randomQuestions = new ArrayList<Question>(NUMBER_OF_QUESTIONS);
  25. for (int i = 0; i < NUMBER_OF_QUESTIONS; i++) {
  26. int randomQuestionElementsCapacity = getRandomQuestionElementsCapacity();
  27. Question question = new Question(randomQuestionElementsCapacity);
  28. for (int j = 0; j < randomQuestionElementsCapacity; j++) {
  29. boolean isLastIteration = j + 1 == randomQuestionElementsCapacity;
  30.  
  31. QuestionElement questionElement = new QuestionElement();
  32. questionElement.setValue(getRandomQuestionElementValue());
  33. questionElement.setOperator(isLastIteration ? null
  34. : Operator.values()[randomGenerator.nextInt(Operator.values().length)]);
  35.  
  36. question.addElement(questionElement);
  37. }
  38. randomQuestions.add(question);
  39. }
  40. return randomQuestions;
  41. }
  42.  
  43. private int getRandomQuestionElementsCapacity() {
  44. return getRandomIntegerFromRange(MIN_QUESTION_ELEMENTS, MAX_QUESTION_ELEMENTS);
  45. }
  46.  
  47. private int getRandomQuestionElementValue() {
  48. return getRandomIntegerFromRange(MIN_QUESTION_ELEMENT_VALUE, MAX_QUESTION_ELEMENT_VALUE);
  49. }
  50.  
  51. private int getRandomIntegerFromRange(int min, int max) {
  52. return randomGenerator.nextInt(max - min + 1) + min;
  53. }
  54. }
  55.  
  56. class Question {
  57.  
  58. private List<QuestionElement> questionElements;
  59.  
  60. public Question(int sizeOfQuestionElemets) {
  61. questionElements = new ArrayList<QuestionElement>(sizeOfQuestionElemets);
  62. }
  63.  
  64. public void addElement(QuestionElement questionElement) {
  65. questionElements.add(questionElement);
  66. }
  67.  
  68. public List<QuestionElement> getElements() {
  69. return questionElements;
  70. }
  71.  
  72. public int size() {
  73. return questionElements.size();
  74. }
  75.  
  76. @Override
  77. public String toString() {
  78. StringBuilder sb = new StringBuilder();
  79. for (QuestionElement questionElement : questionElements) {
  80. sb.append(questionElement);
  81. }
  82. return sb.toString().trim();
  83. }
  84. }
  85.  
  86. class QuestionElement {
  87.  
  88. private int value;
  89. private Operator operator;
  90.  
  91. public int getValue() {
  92. return value;
  93. }
  94.  
  95. public void setValue(int value) {
  96. this.value = value;
  97. }
  98.  
  99. public Operator getOperator() {
  100. return operator;
  101. }
  102.  
  103. public void setOperator(Operator operator) {
  104. this.operator = operator;
  105. }
  106.  
  107. @Override
  108. public String toString() {
  109. return value + (operator == null ? "" : " " + operator.getDisplayValue()) + " ";
  110. }
  111. }
  112.  
  113. enum Operator {
  114.  
  115. PLUS("+"), MINUS("-"), MULTIPLIER("*"), DIVIDER("/");
  116. private String displayValue;
  117.  
  118. private Operator(String displayValue) {
  119. this.displayValue = displayValue;
  120. }
  121.  
  122. public String getDisplayValue() {
  123. return displayValue;
  124. }
  125. }
Success #stdin #stdout 0.08s 213440KB
stdin
Standard input is empty
stdout
88 + 27 - 37
95 / 66 - 41 / 61
66 - 69 / 10 - 67
10 * 7 + 20 / 97
79 - 31 / 72 + 28
3 - 78 / 76
19 + 42 + 5 * 70
65 / 56 + 19 / 56
89 - 28 / 50 + 46
85 - 16 * 3 * 75