-
Notifications
You must be signed in to change notification settings - Fork 21
Lab1 #12
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
Open
parfen01
wants to merge
12
commits into
Rrenkens:main
Choose a base branch
from
parfen01:developing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lab1 #12
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6c5544d
All abstract classes
parfen01 2987e10
Lab1: made classes and interfaces
parfen01 bf2ecb9
Lab1: made tests
parfen01 9a34500
Particulary tested
parfen01 8d96440
Lab1 Tested + Nested classes
parfen01 26dd3e5
Lab1 Fixed issues + MathTasks with doubles
parfen01 d7c4911
Lab1 Fixed issues + Made precision
parfen01 e5bf20f
Lab1 Update .gitignore
parfen01 761c109
Lab1 Update .gitignore
parfen01 24e9309
Lab1 Update .gitignore
parfen01 39310cc
Delete lab-01/.idea directory
parfen01 1d55970
Delete lab-01/out/production/lab-01 directory
parfen01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Default ignored files | ||
| /shelf/ | ||
| /workspace.xml | ||
| # Editor-based HTTP Client requests | ||
| /httpRequests/ | ||
| # Datasource local storage ignored files | ||
| /dataSources/ | ||
| /dataSources.local.xml | ||
| # other | ||
| /out/ | ||
| /.idea/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import by.parfen01.quiser.Quiz; | ||
| import by.parfen01.quiser.Result; | ||
| import by.parfen01.quiser.task_generators.GroupTaskGenerator; | ||
| import by.parfen01.quiser.task_generators.PoolTaskGenerator; | ||
| import by.parfen01.quiser.tasks.TextTask; | ||
| import by.parfen01.quiser.tasks.math_tasks.EquationTask; | ||
| import by.parfen01.quiser.tasks.math_tasks.ExpressionTask; | ||
| import by.parfen01.quiser.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 test name..."); | ||
| Scanner scanner = new Scanner(System.in); | ||
| Quiz currentQuiz; | ||
| String nameOfQuiz = scanner.nextLine(); | ||
| while(true) { | ||
| if (!givenQuizzes.containsKey(nameOfQuiz)) { | ||
| System.out.println("Test 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 once more"); | ||
| } | ||
| } | ||
| Formatter formatter = new Formatter(); | ||
| formatter.format("%.1f", currentQuiz.getMark()); | ||
| System.out.println("total correct answer number: " + currentQuiz.getCorrectAnswerNumber()); | ||
| System.out.println("total wrong answer number: " + currentQuiz.getWrongAnswerNumber()); | ||
| System.out.println("total incorrect input number: " + currentQuiz.getIncorrectInputNumber()); | ||
| System.out.println("your final mark is: " + formatter); | ||
| } | ||
|
|
||
| /** | ||
| * @return тесты в {@link Map}, где | ||
| * ключ - название теста {@link String} | ||
| * значение - сам тест {@link Quiz} | ||
| */ | ||
| static Map<String, Quiz> getQuizMap() { | ||
| Map<String, Quiz> result = new TreeMap<>(); | ||
| ExpressionTask.Generator firstTaskGenerator = new ExpressionTask.Generator( | ||
| 1, 100, 2, EnumSet.allOf(MathTask.Operation.class)); | ||
| Quiz firstQuiz = new Quiz(firstTaskGenerator, 10); | ||
| result.put("Expression Test", firstQuiz); | ||
| 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("Equation Test", secondQuiz); | ||
| TextTask firstTextTask = new TextTask("what the highest mountain in the world?", "Everest"); | ||
| TextTask secondTextTask = new TextTask("what the fastest mammal in the world?", "Cheetah"); | ||
| TextTask thirdTextTask = new TextTask("Лектор расписался в журнале." + | ||
| " Там было 3 н-ки." + " Сколько студентов было на паре?", "Один"); | ||
| TextTask fourthTextTask = new TextTask("what the fastest mammal in the world?", "Cheetah"); | ||
| 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("Text Test", thirdQuiz); | ||
| Quiz fourthQuiz = new Quiz(new GroupTaskGenerator(firstTaskGenerator, secondTaskGenerator, | ||
| thirdTaskGenerator.clone()), 10); | ||
| result.put("Mixed Test", fourthQuiz); | ||
| Quiz fifthQuiz = new Quiz(fourthTaskGenerator, 3); | ||
| result.put("Text Test With Repeat", fifthQuiz); | ||
| return result; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| package by.parfen01.quiser; | ||
|
|
||
| /** | ||
| * Class, который описывает один тест | ||
| */ | ||
| public class Quiz { | ||
| private final Task.Generator generator; | ||
| private int taskCount; | ||
| private int correctAnswerNumber = 0; | ||
| private int wrongAnswerNumber = 0; | ||
| private int incorrectInputCount = 0; | ||
| Task currentTask; | ||
| Task nextTask; | ||
| boolean isLastInputIncorrect = false; | ||
| /** | ||
| * @param generator генератор заданий | ||
| * @param taskCount количество заданий в тесте | ||
| */ | ||
| public Quiz(Task.Generator generator, int taskCount) { | ||
| this.generator = generator; | ||
| this.taskCount = taskCount; | ||
| this.nextTask = null; | ||
| } | ||
|
|
||
| /** | ||
| * @return задание, повторный вызов вернет слелующее | ||
| * @see Task | ||
| */ | ||
| public Task nextTask() { | ||
| if (isLastInputIncorrect) { | ||
| return currentTask; | ||
| } | ||
| if (nextTask == null) { | ||
| nextTask = generator.generate(); | ||
| } | ||
| Task result = nextTask; | ||
| currentTask = nextTask; | ||
| if (taskCount > 1) { | ||
| nextTask = generator.generate(); | ||
| } | ||
| 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 -> { | ||
| isLastInputIncorrect = true; | ||
| ++incorrectInputCount; | ||
| } | ||
| case OK -> { | ||
| ++correctAnswerNumber; | ||
| --taskCount; | ||
| isLastInputIncorrect = false; | ||
| } | ||
| case WRONG -> { | ||
| ++wrongAnswerNumber; | ||
| --taskCount; | ||
| isLastInputIncorrect = false; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * @return завершен ли тест | ||
| */ | ||
| public boolean isFinished() { | ||
| return taskCount == 0; | ||
| } | ||
|
|
||
| /** | ||
| * @return количество правильных ответов | ||
| */ | ||
| public int getCorrectAnswerNumber() { | ||
| return this.correctAnswerNumber; | ||
| } | ||
|
|
||
| /** | ||
| * @return количество неправильных ответов | ||
| */ | ||
| public int getWrongAnswerNumber() { | ||
| return this.wrongAnswerNumber; | ||
| } | ||
|
|
||
| /** | ||
| * @return количество раз, когда был предоставлен неправильный ввод | ||
| */ | ||
| public int getIncorrectInputNumber() { | ||
| return this.incorrectInputCount; | ||
| } | ||
|
|
||
| /** | ||
| * @return оценка, которая является отношением количества правильных ответов к количеству всех вопросов. | ||
| * Оценка выставляется только в конце! | ||
| */ | ||
| public double getMark() { | ||
| return (double)correctAnswerNumber / (correctAnswerNumber + wrongAnswerNumber) * 10; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package by.parfen01.quiser; | ||
|
|
||
| /** | ||
| * Enum, который описывает результат ответа на задание | ||
| */ | ||
| public enum Result { | ||
| OK, // Получен правильный ответ | ||
| WRONG, // Получен неправильный ответ | ||
| INCORRECT_INPUT // Некорректный ввод. Например, текст, когда ожидалось число | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package by.parfen01.quiser; | ||
|
|
||
| /** | ||
| * Interface, который описывает одно задание | ||
| */ | ||
| public interface Task { | ||
| /* | ||
| @return текст задания | ||
| */ | ||
|
|
||
| String getText(); | ||
|
|
||
| /* | ||
| * Проверяет ответ на задание и возвращает результат | ||
| * | ||
| * @param answer ответ на задание | ||
| * @return результат ответа | ||
| * @see Result | ||
| */ | ||
| Result validate(String answer); | ||
|
|
||
| /** | ||
| * Interface, который описывает один генератор заданий | ||
| */ | ||
| interface Generator { | ||
| /* | ||
| * Возвращает задание. При этом новый объект может не создаваться, если класс задания иммутабельный | ||
| * | ||
| * @return задание | ||
| * @see Task | ||
| */ | ||
| Task generate(); | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
lab-01/src/by/parfen01/quiser/task_generators/GroupTaskGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package by.parfen01.quiser.task_generators; | ||
|
|
||
| import by.parfen01.quiser.Task; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
|
|
||
| public class GroupTaskGenerator implements Task.Generator { | ||
| private final ArrayList<Task.Generator> generators = new ArrayList<>(); | ||
| /** | ||
| * Конструктор с переменным числом аргументов | ||
| * | ||
| * @param generators генераторы, которые в конструктор передаются через запятую | ||
| */ | ||
| public GroupTaskGenerator(Task.Generator... generators) { | ||
| if (generators == null) { | ||
| throw new NullPointerException(); | ||
| } | ||
| for (Task.Generator generator : generators) { | ||
| if (generator != null) { | ||
| this.generators.add(generator); | ||
| } | ||
| } | ||
| if (this.generators.isEmpty()) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Конструктор, который принимает коллекцию генераторов | ||
| * | ||
| * @param generators генераторы, которые передаются в конструктор в Collection (например, {@link ArrayList}) | ||
| */ | ||
| GroupTaskGenerator(Collection<Task.Generator> generators) { | ||
| if (generators == null) { | ||
| throw new NullPointerException(); | ||
| } | ||
| for (Task.Generator generator : generators) { | ||
| if (generator != null) { | ||
| this.generators.add(generator); | ||
| } | ||
| } | ||
| if (this.generators.isEmpty()) { | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @return результат метода generate() случайного генератора из списка. | ||
| * Если этот генератор выбросил исключение в методе generate(), выбирается другой. | ||
| * Если все генераторы выбрасывают исключение, то и тут выбрасывается исключение. | ||
| */ | ||
| public Task generate() { | ||
| Collections.shuffle(generators); | ||
| for (Task.Generator generator : generators) { | ||
| try { | ||
| return generator.generate(); | ||
| } catch (Exception ignored) { | ||
|
|
||
| } | ||
| } | ||
|
|
||
| throw new RuntimeException(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Все константы лучше выносить в отдельный файлик. Это касается про кол-во тестов и параметры генератора.