-
Notifications
You must be signed in to change notification settings - Fork 21
Lab1 #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Lab1 #46
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <module type="JAVA_MODULE" version="4"> | ||
| <component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
| <exclude-output /> | ||
| <content url="file://$MODULE_DIR$"> | ||
| <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
| </content> | ||
| <orderEntry type="inheritedJdk" /> | ||
| <orderEntry type="sourceFolder" forTests="false" /> | ||
| </component> | ||
| </module> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import by.kalllegar.quizer.Quiz; | ||
| import by.kalllegar.quizer.Result; | ||
| import by.kalllegar.quizer.task_generators.GroupTaskGenerator; | ||
| import by.kalllegar.quizer.task_generators.PoolTaskGenerator; | ||
| import by.kalllegar.quizer.tasks.TextTask; | ||
| import by.kalllegar.quizer.tasks.math_tasks.EquationTask; | ||
| import by.kalllegar.quizer.tasks.math_tasks.ExpressionTask; | ||
| import by.kalllegar.quizer.tasks.math_tasks.MathTask; | ||
| import java.util.EnumSet; | ||
| import java.util.Map; | ||
| import java.util.Scanner; | ||
| import java.util.TreeMap; | ||
| import java.util.Formatter; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] argv) { | ||
| Map<String, Quiz> givenQuizzes = getQuizMap(); | ||
| System.out.println("enter the name of the test:"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| Quiz currentQuiz; | ||
| String nameOfQuiz = scanner.nextLine(); | ||
| while(true) { | ||
| if (!givenQuizzes.containsKey(nameOfQuiz)) { | ||
| System.out.println("The test was not found. Please try another name:"); | ||
| nameOfQuiz = scanner.nextLine(); | ||
| } else { | ||
| currentQuiz = givenQuizzes.get(nameOfQuiz); | ||
| break; | ||
| } | ||
| } | ||
| while (!currentQuiz.isFinished()) { | ||
| System.out.println(currentQuiz.nextTask().getText()); | ||
| String answer = scanner.nextLine(); | ||
| Result result = currentQuiz.provideAnswer(answer); | ||
| if (result == Result.OK) { | ||
| System.out.println("Right"); | ||
| continue; | ||
| } | ||
| if (result == Result.WRONG) { | ||
| System.out.println("Wrong"); | ||
| continue; | ||
| } | ||
| if (result == Result.INCORRECT_INPUT) { | ||
| System.out.println("Incorrect input. Try again"); | ||
| } | ||
| } | ||
| Formatter formatter = new Formatter(); | ||
| formatter.format("%.1f", currentQuiz.getMark()); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. почему 1 знака достаточно? |
||
| System.out.println("correct answers number: " + currentQuiz.getCorrectAnswerCount()); | ||
| System.out.println("wrong answers number: " + currentQuiz.getWrongAnswerCount()); | ||
| System.out.println("incorrect inputs number: " + currentQuiz.getIncorrectInputNumber()); | ||
| System.out.println("mark: " + formatter); | ||
| } | ||
|
|
||
| /** | ||
| * @return тесты в {@link Map}, где | ||
| * ключ - название теста {@link String} | ||
| * значение - сам тест {@link Quiz} | ||
| */ | ||
| static Map<String, Quiz> getQuizMap() { | ||
| Map<String, Quiz> result = new TreeMap<>(); | ||
| //ExpressionTest usage | ||
| ExpressionTask.Generator firstTaskGenerator = new ExpressionTask.Generator( | ||
| 1, 100, 2, EnumSet.allOf(MathTask.Operation.class)); | ||
| Quiz firstQuiz = new Quiz(firstTaskGenerator, 10); | ||
| result.put("ExpressionTest", firstQuiz); | ||
| //EquationTest usage | ||
| EquationTask.Generator secondTaskGenerator = new EquationTask.Generator( | ||
| 1, 100, 3, EnumSet.allOf(MathTask.Operation.class), new int[]{0, 1}); | ||
| Quiz secondQuiz = new Quiz(secondTaskGenerator, 10); | ||
| result.put("EquationTest", secondQuiz); | ||
| //TextTest usage | ||
| TextTask firstTextTask = new TextTask("what the best array sorting asymptotics?", "O(nlogn)"); | ||
| TextTask secondTextTask = new TextTask("how many people are in our group?", "26"); | ||
| TextTask thirdTextTask = new TextTask("the asymptotics with which the insertion into the map is performed?", "O(1)"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А это точно правда? |
||
| TextTask fourthTextTask = new TextTask("what the best array sorting asymptotics?", "O(nlogn)"); | ||
| PoolTaskGenerator thirdTaskGenerator = new PoolTaskGenerator(false, secondTextTask, | ||
| thirdTextTask, fourthTextTask, firstTextTask); | ||
| PoolTaskGenerator fourthTaskGenerator = new PoolTaskGenerator(true, secondTextTask, | ||
| thirdTextTask, fourthTextTask, firstTextTask); | ||
| Quiz thirdQuiz = new Quiz(thirdTaskGenerator, 3); | ||
| result.put("TextTest", thirdQuiz); | ||
| //MixedTest usage | ||
| Quiz fourthQuiz = new Quiz(new GroupTaskGenerator(firstTaskGenerator, secondTaskGenerator, | ||
| thirdTaskGenerator.clone()), 10); | ||
| result.put("GroupTest", fourthQuiz); | ||
| Quiz fifthQuiz = new Quiz(fourthTaskGenerator, 3); | ||
| result.put("Text Test With Repetition", fifthQuiz); | ||
| return result; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package by.kalllegar.quizer; | ||
|
|
||
| import by.kalllegar.quizer.task_generators.TaskGenerator; | ||
|
|
||
| /** | ||
| * Class, который описывает один тест | ||
| */ | ||
| public class Quiz { | ||
| private final TaskGenerator generator; | ||
| private int taskCount; | ||
| private int correctAnswerCount = 0; | ||
| private int wrongAnswerCount = 0; | ||
| private int incorrectInputCount = 0; | ||
| Task currentTask; | ||
| Task nextTask; | ||
| boolean isInputIncorrect = false; | ||
|
|
||
| /** | ||
| * @param generator генератор заданий | ||
| * @param taskCount количество заданий в тесте | ||
| */ | ||
| public Quiz(TaskGenerator generator, int taskCount) { | ||
| this.generator = generator; | ||
| this.taskCount = taskCount; | ||
| this.nextTask = null; | ||
| } | ||
|
|
||
| /** | ||
| * @return задание, повторный вызов вернет слелующее | ||
| * @see Task | ||
| */ | ||
| public Task nextTask() { | ||
| if (isInputIncorrect) { | ||
| return currentTask; | ||
| } | ||
| if (nextTask == null) { | ||
| nextTask = generator.generate(); | ||
| } | ||
| Task result = nextTask; | ||
| currentTask = nextTask; | ||
| if (taskCount > 1) { | ||
| nextTask = generator.generate(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему нам необходимо хранить текущую и следующую таску? |
||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Предоставить ответ ученика. Если результат {@link Result#INCORRECT_INPUT}, то счетчик неправильных | ||
| * ответов не увеличивается, а {@link #nextTask()} в следующий раз вернет тот же самый объект {@link Task}. | ||
| */ | ||
| public Result provideAnswer(String answer) { | ||
| Result result = currentTask.validate(answer); | ||
| switch (result) { | ||
| case INCORRECT_INPUT -> { | ||
| isInputIncorrect = true; | ||
| incorrectInputCount++; | ||
| } | ||
| case OK -> { | ||
| correctAnswerCount++; | ||
| taskCount--; | ||
| isInputIncorrect = false; | ||
| } | ||
| case WRONG -> { | ||
| wrongAnswerCount++; | ||
| taskCount--; | ||
| isInputIncorrect = false; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * @return завершен ли тест | ||
| */ | ||
| public boolean isFinished() { | ||
| return taskCount == 0; | ||
| } | ||
|
|
||
| /** | ||
| * @return количество правильных ответов | ||
| */ | ||
| public int getCorrectAnswerCount() { | ||
| return this.correctAnswerCount; | ||
| } | ||
|
|
||
| /** | ||
| * @return количество неправильных ответов | ||
| */ | ||
| public int getWrongAnswerCount() { | ||
| return this.wrongAnswerCount; | ||
| } | ||
|
|
||
| /** | ||
| * @return количество раз, когда был предоставлен неправильный ввод | ||
| */ | ||
| public int getIncorrectInputNumber() { | ||
| return this.incorrectInputCount; | ||
| } | ||
|
|
||
| /** | ||
| * @return оценка, которая является отношением количества правильных ответов к количеству всех вопросов. | ||
| * Оценка выставляется только в конце! | ||
| */ | ||
| public double getMark() { | ||
| return (double) correctAnswerCount / (correctAnswerCount + wrongAnswerCount) * 10; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package by.kalllegar.quizer; | ||
|
|
||
| /** | ||
| * Enum, который описывает результат ответа на задание | ||
| */ | ||
| public enum Result { | ||
| OK, // Получен правильный ответ | ||
| WRONG, // Получен неправильный ответ | ||
| INCORRECT_INPUT // Некорректный ввод. Например, текст, когда ожидалось число | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package by.kalllegar.quizer; | ||
|
|
||
| /** | ||
| * Interface, который описывает одно задание | ||
| */ | ||
| public interface Task { | ||
| /* | ||
| @return текст задания | ||
| */ | ||
|
|
||
| String getText(); | ||
|
|
||
| /* | ||
| * Проверяет ответ на задание и возвращает результат | ||
| * | ||
| * @param answer ответ на задание | ||
| * @return результат ответа | ||
| * @see Result | ||
| */ | ||
| Result validate(String answer); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package by.kalllegar.quizer.task_generators; | ||
|
|
||
| import by.kalllegar.quizer.Task; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
|
|
||
| public class GroupTaskGenerator implements TaskGenerator { | ||
| private final ArrayList<TaskGenerator> generators = new ArrayList<>(); | ||
|
|
||
| /** | ||
| * Конструктор с переменным числом аргументов | ||
| * | ||
| * @param generators генераторы, которые в конструктор передаются через запятую | ||
| */ | ||
| public GroupTaskGenerator(TaskGenerator... generators) { | ||
| if (generators == null) { | ||
| throw new NullPointerException(); | ||
| } | ||
| for (var generator : generators) { | ||
| if (generator != null) { | ||
| this.generators.add(generator); | ||
| } | ||
| } | ||
| if (this.generators.isEmpty()) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Конструктор, который принимает коллекцию генераторов | ||
| * | ||
| * @param generators генераторы, которые передаются в конструктор в Collection (например, {@link ArrayList}) | ||
| */ | ||
| GroupTaskGenerator(Collection<TaskGenerator> generators) { | ||
| if (generators == null) { | ||
| throw new NullPointerException(); | ||
| } | ||
| this.generators.addAll(generators); | ||
| if (this.generators.isEmpty()) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @return результат метода generate() случайного генератора из списка. | ||
| * Если этот генератор выбросил исключение в методе generate(), выбирается другой. | ||
| * Если все генераторы выбрасывают исключение, то и тут выбрасывается исключение. | ||
| */ | ||
| public Task generate() { | ||
| Collections.shuffle(generators); | ||
| for (var generator : generators) { | ||
| try { | ||
| return generator.generate(); | ||
| } catch (Exception ignore) { | ||
| } | ||
| } | ||
| throw new RuntimeException(); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А как я его узнаю без кода?