Skip to content

Commit

Permalink
update the DIY user guide
Browse files Browse the repository at this point in the history
  • Loading branch information
Celineyaa committed Apr 13, 2024
2 parents 2c5918c + 420e7cc commit a02a0d0
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 96 deletions.
17 changes: 17 additions & 0 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,25 @@ type the answer in the terminal and press ENTER/RETURN
After finishing all the problem sets, the program will automate judged the correctness and output the accuracy and speed.

### DIY: `DIY`

Add user DIY problem sets into our problem set datasets. The DIY problem set can also be saved and retried.

**Format:** `DIY`

- after input `DIY`:
1. System will output
`Please input your DIY problemSet:
input the description of the problem (e.g. 1+2*3): `
user can input a user-defined problem into the system.
2. Then the System will ask for input:
`input the correct answer of the problem (e.g. 7): `
user should input the correct answer of the user-defined problem.
3. Then the System will ask:
`Do you finish adding problems? y/n: `
if user input `y`, which means no more problems will be added.
if user input `n`, the system will repeat step 1 to 3 until user input `y`.
- user can see the type of the problem set, i.e. user - DIY or auto generated, in the records.

### Exit: `exit`

User can use this to exit the program.
Expand Down
4 changes: 1 addition & 3 deletions recordList.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
2024-04-12 04:43:40 7.5 1.0 2094411618 auto-generated 8+3+4,15.0
2024-04-12 04:44:30 0.0 0.0 540585600 user-DIY 1+2+3*4,15.0
2024-04-12 04:45:04 10.0 1.0 540585600 user-DIY 1+2+3*4,15.0

70 changes: 68 additions & 2 deletions src/main/java/seedu/duke/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package seedu.duke;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import static java.lang.Character.isDigit;

public class Calculator {

private static List<String> explanations = new ArrayList<>();

public static double calculate(StringBuilder sb) {
public double calculate(StringBuilder sb) {
Stack<Double> numStack = new Stack<>();
Stack<String> opStack = new Stack<>();

Expand All @@ -21,11 +24,62 @@ public static double calculate(StringBuilder sb) {
double num2 = numStack.pop();
double num1 = numStack.pop();
double result = calculateTwo(num1, num2, (String) object);
explanations.add(getExplanation(num1, num2, (String) object, result));
numStack.push(result);
}
}
assert numStack.size() == 1 : "wrong formula";
return numStack.pop();
// round to 3 decimal places
Double result = Math.round(numStack.peek() * 1000.0) / 1000.0;
return result;
}

private static String getExplanation(double num1, double num2, String op, double answer) {
String Start = "The computation of the problem: "+ String.valueOf(num1) + " " + op + " " + String.valueOf(num2) + " = " + String.valueOf(answer) + "\n\n";
List<String> explanation = new ArrayList<>();
StringBuilder builder = new StringBuilder();
String alignedProblem = "";
if (op.equals("/")){
alignedProblem = "The division of " + num1 + " and " + num2 + " is " + answer + "\n";
}
else {
String firstString = String.valueOf(num1);
String secondString = String.valueOf(num2);
String firstIntergerPart = firstString.split("\\.")[0];
String secondIntergerPart = secondString.split("\\.")[0];
if (firstIntergerPart.length() < secondIntergerPart.length()) {
String temp = firstString;
firstString = secondString;
secondString = temp;
}
if(op.equals("*")){
op = "x";
}
// align the problem to the . place
String firstDecimalPart = firstString.split("\\.")[1];
String secondDecimalPart = secondString.split("\\.")[1];

if (firstDecimalPart.length() < secondDecimalPart.length()) {
firstString = firstString + new String(new char[secondDecimalPart.length() - firstDecimalPart.length()]).replace("\0", "0");
} else {
secondString = secondString + new String(new char[firstDecimalPart.length() - secondDecimalPart.length()]).replace("\0", "0");
}

explanation.add(firstString.trim());
explanation.add(op + " " + secondString.trim());
explanation.add("---------------------------------------");
explanation.add(String.valueOf(answer));
for (String element : explanation) {
builder.append(String.format("%30s%n", element));
}
alignedProblem = builder.toString();





}
return Start + alignedProblem + "\n";
}

private static double calculateTwo(double num1, double num2, String op) {
Expand Down Expand Up @@ -132,4 +186,16 @@ private static ArrayList<Object> toFormula(StringBuilder sb) {
}
return formula;
}

public List<String> getExplanations() {
return explanations;
}

public String getExplanationsString() {
StringBuilder builder = new StringBuilder();
for (String explanation : explanations) {
builder.append(explanation);
}
return builder.toString();
}
}
89 changes: 16 additions & 73 deletions src/main/java/seedu/duke/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,85 +24,25 @@ public Checker(Test test) {
this.time = 0;
}

public static String getExplanation(Problem problem) {
String description = problem.getDescription();
String start = "Let's explain this problem by the following format: " + "\n\n";
List<String> explanation = new ArrayList<>();
StringBuilder builder = new StringBuilder();
if (description.contains("+")) {
String[] parser = description.split("\\+");
double firstNumber = Double.parseDouble(parser[0]);
double secondNumber = Double.parseDouble(parser[1]);
String answerString = String.valueOf(problem.getAnswer());
String firstString = String.valueOf(firstNumber);
String secondString = String.valueOf(secondNumber);
//Shift the longer number to the first
if (firstString.length() < secondString.length()) {
String temp = firstString;
firstString = secondString;
secondString = temp;
}
explanation.add(firstString);
explanation.add("+" + " " + secondString);

} else if (description.contains("-")) {
String[] parser = description.split("-");
double firstNumber = Double.parseDouble(parser[0]);
double secondNumber = Double.parseDouble(parser[1]);
String answerString = String.valueOf(problem.getAnswer());
String firstString = String.valueOf(firstNumber);
String secondString = String.valueOf(secondNumber);
//Shift the longer number to the first
if (firstString.length() < secondString.length()) {
String temp = firstString;
firstString = secondString;
secondString = temp;
}
explanation.add(firstString);
explanation.add("-" + " " + secondString);

} else if (description.contains("*")) {
String[] parser = description.split("\\*");
double firstNumber = Double.parseDouble(parser[0]);
double secondNumber = Double.parseDouble(parser[1]);
String answerString = String.valueOf(problem.getAnswer());
String firstString = String.valueOf(firstNumber);
String secondString = String.valueOf(secondNumber);
//Shift the longer number to the first
if (firstString.length() < secondString.length()) {
String temp = firstString;
firstString = secondString;
secondString = temp;
}
explanation.add(firstString);
explanation.add("X" + " " + secondString);
} else if (description.contains("/")) {
return "We do not support the explanation with division now!";
} else {
return "The format of the problem do not provide any explanation now!";
}
explanation.add("---------------------------------------");
explanation.add(String.valueOf(problem.getAnswer()));
for (String element : explanation) {
builder.append(String.format("%" + 20 + "s%n", element));
}
String alignedProblem = builder.toString();
String end = "You can compare your answer with the above function\n";
return start + alignedProblem + "\n" + end;
}
public static void showExplanation(Problem problem) {
String explanations = problem.getExplanations();
Ui ui = new Ui("");
ui.print("The explanation of the problem: " + problem.solved());
ui.print("Let us caculate it step by step:");
ui.print(explanations);

ui.print("From all the steps above, we can get the answer: " + problem.solved()+"\n");

public static void testExplanation() {
Problem testProblem = new Problem("99+9", 108);
System.out.println(getExplanation(testProblem));
}


Boolean checkCorrectness(Problem problem, double answer) {
return Math.abs(problem.getAnswer() - answer) < 0.01;
}

void getUserAnswer() {
long startTime = System.currentTimeMillis();
ui.print("Press Enter to start answering the questions...");
ui.print("Press Enter to start answering the questions, you can type \"exit\" to quit the test when answering the question...");
String userInput = ui.readCommand();

for (int i = 0; i < test.getNumber(); i++) {
Expand All @@ -112,16 +52,19 @@ void getUserAnswer() {
userAnswer[i] = userInput;
double answer = Double.NEGATIVE_INFINITY;
boolean isValid = false;
if (userInput.equals("exit")) {
ui.print("Exit the test! All the test not finished will be marked as wrong!");
break;
}
while (!isValid) {

try {
answer = Double.parseDouble(userInput);
isValid = true;
} catch (NumberFormatException e) {

ui.print("Invalid Input, please enter a number");
//wrongAnswer.add(userInput);
//wrongProblem.add(problem);
//continue;
ui.print(problem.unsolved());
userInput = ui.readCommand();

}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/seedu/duke/DIYProblemSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public void addDIYProblemSet(Ui ui) {
String description;
String correctAnswer;
double answer = 0.0;
String explanations = "";
String quit = "";
while (!quit.equals("y")) {
ui.print("input the description of the problem (e.g. 1+2*3): ");
Expand All @@ -26,7 +27,7 @@ public void addDIYProblemSet(Ui ui) {
} catch (NumberFormatException e) {
ui.print("Invalid answer! Please input a number.");
}
Problem problem = new Problem(description,answer);
Problem problem = new Problem(description,answer,explanations);
problemSet.add(problem);
ui.print("Do you finish adding problems? y/n: ");
quit = scanner.nextLine();
Expand All @@ -40,6 +41,7 @@ public void addDIYProblemSet(Ui ui) {
ui.print("\nSuccessfully save the DIY problem set!");
record.print(true);
ui.print("\n");
scanner.close();
}


Expand Down
15 changes: 9 additions & 6 deletions src/main/java/seedu/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,16 @@ public static void solveProbSet(Test test, Ui ui, boolean retry, int id) {

for (int i = 0; i < wrongProblem.size(); i++) {
Problem problem = wrongProblem.get(i);
ui.print("The "+ String.valueOf(i+1)+"th wrong answer of you: ");
ui.print("Your answer: " + problem.getDescription() + " = " + wrongAnswer.get(i));
ui.print("Correct Answer: " + problem.solved());
// need further implementation for 3 more operators
// ui.print("If you want to see the explanation, type exp or explanation, else just type enter");
// String userInput = ui.readCommand();
// if(userInput.equals("exp")||userInput.equals("explanation"))
// System.out.println(Checker.getExplanation(problem));
ui.print("If you want to see the explanation, type exp or explanation, else just type enter, type exit to stop showing the answer");
String userInput = ui.readCommand();
if(userInput.equals("exit"))
break;
if(userInput.equals("exp")||userInput.equals("explanation"))
Checker.showExplanation(problem);
}

// Storage write to file
Expand Down Expand Up @@ -142,8 +145,8 @@ public static void parse(String command, Ui ui) {
parseRetry(description, ui);
break;
case "DIY":
DIYProblemSet DIY = new DIYProblemSet();
DIY.addDIYProblemSet(ui);
DIYProblemSet diy = new DIYProblemSet();
diy.addDIYProblemSet(ui);
break;
case "help":
ui.help(description);
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/seedu/duke/Problem.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package seedu.duke;

import java.util.List;

public class Problem {

private final String description;
private final double answer;
private String explanations;

public Problem(String description, double answer) {
public Problem(String description, double answer, String explanations2) {
this.description = description;
this.answer = answer;
this.explanations = explanations2;


}

public String solved() {
Expand All @@ -25,4 +31,8 @@ public double getAnswer() {
public String getDescription() {
return description;
}

public String getExplanations() {
return explanations;
}
}
10 changes: 7 additions & 3 deletions src/main/java/seedu/duke/ProblemGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static java.lang.Character.isDigit;

Expand Down Expand Up @@ -176,6 +177,7 @@ private Test generate(HashMap<String, String> parameter) {

StringBuilder descriptionBuilder = new StringBuilder();
double answer;
String explanations;
int max = (int) Math.pow(MAXIMUM_LENGTH, maxDigit);

for (int j = 0; j < length; j++) {
Expand All @@ -189,12 +191,14 @@ private Test generate(HashMap<String, String> parameter) {
}

descriptionBuilder = division_check(descriptionBuilder);

answer = Calculator.calculate(descriptionBuilder);
Calculator calculator = new Calculator();
answer = calculator.calculate(descriptionBuilder);
explanations = calculator.getExplanationsString();
String description = descriptionBuilder.toString();


Problem p = new Problem(description, answer);

Problem p = new Problem(description, answer, explanations);
System.out.println((i + 1) + ". " + p.unsolved());
test.addToTest(p);

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static void processLine(String line) throws Exception {

for (int i = minimumLength + 1; i < words.length; i++) {
String[] term = words[i].split(",");
probSet.add(new Problem(term[0], Double.parseDouble(term[1])));
probSet.add(new Problem(term[0], Double.parseDouble(term[1]), null));
}


Expand Down Expand Up @@ -139,6 +139,7 @@ public static void writeFile() {
writer.flush();
System.out.println("Record successfully saved!");
Ui.showLine();
writer.close();

} catch (IOException e) {
System.out.println("Error when saving record!");
Expand Down
Loading

0 comments on commit a02a0d0

Please sign in to comment.