Skip to content

Commit

Permalink
Merge pull request #118 from Andy123qq4/master
Browse files Browse the repository at this point in the history
Fix coding standards and add javadoc comments
  • Loading branch information
Andy123qq4 authored Apr 15, 2024
2 parents 84bc888 + 08fb2f6 commit 25e3e45
Show file tree
Hide file tree
Showing 17 changed files with 340 additions and 49 deletions.
63 changes: 58 additions & 5 deletions src/main/java/seedu/duke/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@

import static java.lang.Character.isDigit;

/**
* This class is responsible for calculating the result of the user input.
* It also generates the explanation of the calculation.
*/
public class Calculator {

private static List<String> explanations;

/**
* This method calculates the result of the user input.
*
* @param sb The user input.
* @return The result of the calculation.
*/
public double calculate(StringBuilder sb) {
Stack<Double> numStack = new Stack<>();
Stack<String> opStack = new Stack<>();
Expand All @@ -33,6 +43,15 @@ public double calculate(StringBuilder sb) {
return Math.round(numStack.peek() * 1000.0) / 1000.0;
}

/**
* This method generates the explanation of the calculation.
*
* @param num1 The first number in the calculation.
* @param num2 The second number in the calculation.
* @param op The operator in the calculation.
* @param answer The result of the calculation.
* @return The explanation of the calculation.
*/
private static String getExplanation(double num1, double num2, String op, double answer) {
String start = "The computation of the problem: " +
num1 + " " + op + " " +
Expand Down Expand Up @@ -85,6 +104,14 @@ private static String getExplanation(double num1, double num2, String op, double
return start + alignedProblem + "\n";
}

/**
* This method calculates the result of the two numbers.
*
* @param num1 The first number in the calculation.
* @param num2 The second number in the calculation.
* @param op The operator in the calculation.
* @return The result of the calculation.
*/
private static double calculateTwo(double num1, double num2, String op) {

double answer;
Expand All @@ -107,6 +134,12 @@ private static double calculateTwo(double num1, double num2, String op) {
return answer;
}

/**
* This method converts the formula to suffix notation.
*
* @param formula The formula to be converted.
* @return The formula in suffix notation.
*/
private static ArrayList<Object> toSuffix(ArrayList<Object> formula) {
ArrayList<Object> suffix = new ArrayList<>();
Stack<String> opStack = new Stack<>();
Expand All @@ -130,9 +163,7 @@ private static ArrayList<Object> toSuffix(ArrayList<Object> formula) {
}
}
opStack.push(op);

}

}

while (!opStack.empty()) {
Expand All @@ -141,16 +172,27 @@ private static ArrayList<Object> toSuffix(ArrayList<Object> formula) {
return suffix;
}

/**
* This method checks if the first operator has higher priority than the second operator.
*
* @param op1 The first operator.
* @param op2 The second operator.
* @return True if the first operator has higher priority than the second operator, false otherwise.
*/
private static boolean prior(String op1, String op2) {
int priority1;
int priority2;

priority1 = getPriority(op1);
priority2 = getPriority(op2);

return priority1 > priority2;
}

/**
* This method returns the priority of the operator.
*
* @param op The operator.
* @return The priority of the operator.
*/
static int getPriority(String op) {
int priority = 0;
switch (op) {
Expand All @@ -168,6 +210,12 @@ static int getPriority(String op) {
return priority;
}

/**
* This method converts the formula to separate objects.
*
* @param sb The formula to be converted.
* @return The formula in separate objects.
*/
private static ArrayList<Object> toFormula(StringBuilder sb) {
ArrayList<Object> formula = new ArrayList<>();
for (int i = 0; i < sb.length(); ) { // change to separate objects
Expand All @@ -190,6 +238,11 @@ private static ArrayList<Object> toFormula(StringBuilder sb) {
return formula;
}

/**
* This method returns the explanation of the calculation.
*
* @return The explanation of the calculation.
*/
public String getExplanationsString() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < explanations.size(); i++) {
Expand Down
48 changes: 34 additions & 14 deletions src/main/java/seedu/duke/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
import java.util.ArrayList;
import java.util.List;

/**
* Checker class is used to check the correctness of the user's answer.
* It will store the user's answer and the correctness of the answer.
* It will also store the wrong answer and the corresponding problem.
*/
public class Checker {

private static final double TOLERANCE = 0.01;

private final String[] userAnswer;
private final Test test;
private final Boolean[] isCorrect;
Expand All @@ -14,6 +22,11 @@ public class Checker {
private double accuracy;
private long time;

/**
* Constructor of the Checker class.
*
* @param test the test that the user is taking.
*/
public Checker(Test test) {
assert test != null : "Input null test!";
this.userAnswer = new String[test.getNumber()];
Expand All @@ -24,22 +37,35 @@ public Checker(Test test) {
this.time = 0;
}

/**
* Show the explanation of the problem.
*
* @param problem the problem that the user is solving.
*/
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("Let us calculate it step by step:");
ui.print(explanations);

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

}


/**
* Check the correctness of the user's answer.
*
* @param problem the problem that the user is solving.
* @param answer the answer that the user input.
* @return true if the answer is correct, false otherwise.
*/
Boolean checkCorrectness(Problem problem, double answer) {
return Math.abs(problem.getAnswer() - answer) < 0.01;
return Math.abs(problem.getAnswer() - answer) < TOLERANCE;
}

/**
* Get the user's answer and check the correctness of the answer.
*/
void getUserAnswer() {
long startTime = System.currentTimeMillis();
ui.print(
Expand All @@ -63,14 +89,13 @@ void getUserAnswer() {
ui.print("Exit the test! All the test not finished will be marked as wrong!");
break;
}
while (!isValid) {

while (!isValid) {
try {
answer = Double.parseDouble(userInput);
isValid = true;

} catch (NumberFormatException e) {

ui.print("Invalid Input, please enter a number");
ui.print(problem.unsolved());
userInput = ui.readCommand();
Expand All @@ -94,17 +119,12 @@ void getUserAnswer() {
}

}

// hand with time and acc
long endTime = System.currentTimeMillis();
accuracy = (double) correctNumber / test.getNumber();
//millisecond to second
// millisecond to second
this.time = (endTime - startTime) / 1000;
for (int i = 0; i < test.getNumber(); i++) {
if (isCorrect[i] = false) {
wrongProblem.add(test.getProblem().get(i));
wrongAnswer.add(userAnswer[i]);
}
}
}

public Boolean[] checkAnswer() {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/seedu/duke/DIYProblemSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,37 @@
import java.time.LocalDateTime;
import java.util.ArrayList;

/**
* Represents a DIY problem set.
* A DIYProblemSet object corresponds to a DIY problem set that is created by the user.
*/
public class DIYProblemSet {
ArrayList<Problem> problemSet;

public DIYProblemSet() {
problemSet = new ArrayList<>();
}

/**
* Adds a DIY problem set to the list of problem sets.
*
* @param ui the ui for the user to interact with the program
*/
public void addDIYProblemSet(Ui ui) {
ui.print("Please input your DIY problemSet: ");
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): ");
description = ui.readCommand();
ui.print("input the correct answer of the problem (e.g. 7): ");
correctAnswer = ui.readCommand();

boolean isValidAnswer = false;
while (!isValidAnswer) {
try {
Expand All @@ -32,19 +44,25 @@ public void addDIYProblemSet(Ui ui) {
correctAnswer = ui.readCommand();
}
}

ui.print("Input the explanations of the problem (e.g. 1+2*3=7): ");
explanations = ui.readCommand();
Problem problem = new Problem(description,answer,explanations);

problemSet.add(problem);

ui.print("Have you finished adding problems? y/n: ");
quit = ui.readCommand();

while (!quit.equals("y") && !quit.equals("n")) {
ui.print("input is invalid! Please input 'y' or 'n': ");
quit = ui.readCommand();
}
}

Record record = new Record(LocalDateTime.now(), 0.0, 0.0, problemSet, ProblemSetType.USER_DIY.getValue());
Storage.addRecord(record);

ui.print("Record successfully saved!");
record.print(true);
Ui.showLine();
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/seedu/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package seedu.duke;

/**
* Represents the Duke program.
* A Duke object corresponds to the Duke program that is created by the user.
*/
public class Duke {

private static final String name = "MathGenius";
Expand All @@ -8,7 +12,6 @@ public class Duke {
/**
* Main entry-point for the java.duke.Duke application.
*/

public static void run() {
ui.greet();

Expand Down
31 changes: 30 additions & 1 deletion src/main/java/seedu/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@
import java.time.LocalDateTime;
import java.util.List;

/**
* Represents a parser.
* A Parser object corresponds to a parser that parses the user input.
*/
public class Parser {

/**
* Parses the user input.
*
* @param description the user input
* @param ui the ui for the user to interact with the program
*/
public static void parseRetry(String description, Ui ui) {
try {
int id = Integer.parseInt(description);
Expand All @@ -22,6 +32,12 @@ public static void parseRetry(String description, Ui ui) {
}
}

/**
* Parses the user input.
*
* @param description the user input
* @param ui the ui for the user to interact with the program
*/
public static void parseRecord(String description, Ui ui) {
String[] tokens = description.split(" ");
int spdSortOp = 0;
Expand Down Expand Up @@ -68,12 +84,19 @@ public static void parseRecord(String description, Ui ui) {
ui.printRecords(Storage.sortRecords(dateSortOp, spdSortOp, accSortOp, probSortOp), probShowDetails);
}

/**
* Solves the problem set.
*
* @param test the test to be solved
* @param ui the ui for the user to interact with the program
* @param retry whether the problem set is a retry
* @param id the id of the problem set
*/
public static void solveProbSet(Test test, Ui ui, boolean retry, int id) {
Checker checker = new Checker(test);
checker.getUserAnswer();
// Use the % format to print acc
ui.showTestResult(checker.getAccuracy(), checker.getTime());


// Show the wrong answer
List<String> wrongAnswer = checker.getWrongAnswer();
Expand Down Expand Up @@ -117,6 +140,12 @@ public static void solveProbSet(Test test, Ui ui, boolean retry, int id) {
Storage.writeFile();
}

/**
* Parses the user input.
*
* @param command the user input
* @param ui the ui for the user to interact with the program
*/
public static void parse(String command, Ui ui) {

/*
Expand Down
Loading

0 comments on commit 25e3e45

Please sign in to comment.