diff --git a/${project.build.directory}/classes/calculator/Addition.class b/${project.build.directory}/classes/calculator/Addition.class new file mode 100644 index 0000000..ea49bc0 Binary files /dev/null and b/${project.build.directory}/classes/calculator/Addition.class differ diff --git a/${project.build.directory}/classes/calculator/App.class b/${project.build.directory}/classes/calculator/App.class new file mode 100644 index 0000000..1a4f6d1 Binary files /dev/null and b/${project.build.directory}/classes/calculator/App.class differ diff --git a/${project.build.directory}/classes/calculator/BinaryOperation.class b/${project.build.directory}/classes/calculator/BinaryOperation.class new file mode 100644 index 0000000..1b1c630 Binary files /dev/null and b/${project.build.directory}/classes/calculator/BinaryOperation.class differ diff --git a/${project.build.directory}/classes/calculator/Calculator.class b/${project.build.directory}/classes/calculator/Calculator.class new file mode 100644 index 0000000..07d687a Binary files /dev/null and b/${project.build.directory}/classes/calculator/Calculator.class differ diff --git a/${project.build.directory}/classes/calculator/Cosine.class b/${project.build.directory}/classes/calculator/Cosine.class new file mode 100644 index 0000000..3dd5d32 Binary files /dev/null and b/${project.build.directory}/classes/calculator/Cosine.class differ diff --git a/${project.build.directory}/classes/calculator/Division.class b/${project.build.directory}/classes/calculator/Division.class new file mode 100644 index 0000000..51fb6f2 Binary files /dev/null and b/${project.build.directory}/classes/calculator/Division.class differ diff --git a/${project.build.directory}/classes/calculator/Exponential.class b/${project.build.directory}/classes/calculator/Exponential.class new file mode 100644 index 0000000..60a1542 Binary files /dev/null and b/${project.build.directory}/classes/calculator/Exponential.class differ diff --git a/${project.build.directory}/classes/calculator/Multiplication.class b/${project.build.directory}/classes/calculator/Multiplication.class new file mode 100644 index 0000000..5d213f3 Binary files /dev/null and b/${project.build.directory}/classes/calculator/Multiplication.class differ diff --git a/${project.build.directory}/classes/calculator/Operation.class b/${project.build.directory}/classes/calculator/Operation.class new file mode 100644 index 0000000..7a9844d Binary files /dev/null and b/${project.build.directory}/classes/calculator/Operation.class differ diff --git a/${project.build.directory}/classes/calculator/Sine.class b/${project.build.directory}/classes/calculator/Sine.class new file mode 100644 index 0000000..8401148 Binary files /dev/null and b/${project.build.directory}/classes/calculator/Sine.class differ diff --git a/${project.build.directory}/classes/calculator/SquareRoot.class b/${project.build.directory}/classes/calculator/SquareRoot.class new file mode 100644 index 0000000..abe2b95 Binary files /dev/null and b/${project.build.directory}/classes/calculator/SquareRoot.class differ diff --git a/${project.build.directory}/classes/calculator/Subtraction.class b/${project.build.directory}/classes/calculator/Subtraction.class new file mode 100644 index 0000000..fcceca0 Binary files /dev/null and b/${project.build.directory}/classes/calculator/Subtraction.class differ diff --git a/${project.build.directory}/classes/calculator/Tangent.class b/${project.build.directory}/classes/calculator/Tangent.class new file mode 100644 index 0000000..6803b00 Binary files /dev/null and b/${project.build.directory}/classes/calculator/Tangent.class differ diff --git a/${project.build.directory}/classes/calculator/TrigonometricOperation.class b/${project.build.directory}/classes/calculator/TrigonometricOperation.class new file mode 100644 index 0000000..686c49b Binary files /dev/null and b/${project.build.directory}/classes/calculator/TrigonometricOperation.class differ diff --git a/${project.build.directory}/classes/calculator/UnaryOperation.class b/${project.build.directory}/classes/calculator/UnaryOperation.class new file mode 100644 index 0000000..15b8e0c Binary files /dev/null and b/${project.build.directory}/classes/calculator/UnaryOperation.class differ diff --git a/${project.build.directory}/test-classes/calculator/AdditionTest.class b/${project.build.directory}/test-classes/calculator/AdditionTest.class new file mode 100644 index 0000000..7b34e09 Binary files /dev/null and b/${project.build.directory}/test-classes/calculator/AdditionTest.class differ diff --git a/${project.build.directory}/test-classes/calculator/AppTest.class b/${project.build.directory}/test-classes/calculator/AppTest.class new file mode 100644 index 0000000..97076b7 Binary files /dev/null and b/${project.build.directory}/test-classes/calculator/AppTest.class differ diff --git a/${project.build.directory}/test-classes/calculator/CalculatorTest.class b/${project.build.directory}/test-classes/calculator/CalculatorTest.class new file mode 100644 index 0000000..0fa296d Binary files /dev/null and b/${project.build.directory}/test-classes/calculator/CalculatorTest.class differ diff --git a/${project.build.directory}/test-classes/calculator/CosineTest.class b/${project.build.directory}/test-classes/calculator/CosineTest.class new file mode 100644 index 0000000..31336d6 Binary files /dev/null and b/${project.build.directory}/test-classes/calculator/CosineTest.class differ diff --git a/${project.build.directory}/test-classes/calculator/DivisionTest.class b/${project.build.directory}/test-classes/calculator/DivisionTest.class new file mode 100644 index 0000000..4ee7e71 Binary files /dev/null and b/${project.build.directory}/test-classes/calculator/DivisionTest.class differ diff --git a/${project.build.directory}/test-classes/calculator/ExponentialTest.class b/${project.build.directory}/test-classes/calculator/ExponentialTest.class new file mode 100644 index 0000000..5d97ba6 Binary files /dev/null and b/${project.build.directory}/test-classes/calculator/ExponentialTest.class differ diff --git a/${project.build.directory}/test-classes/calculator/MultiplicationTest.class b/${project.build.directory}/test-classes/calculator/MultiplicationTest.class new file mode 100644 index 0000000..679ab94 Binary files /dev/null and b/${project.build.directory}/test-classes/calculator/MultiplicationTest.class differ diff --git a/${project.build.directory}/test-classes/calculator/SineTest.class b/${project.build.directory}/test-classes/calculator/SineTest.class new file mode 100644 index 0000000..6972024 Binary files /dev/null and b/${project.build.directory}/test-classes/calculator/SineTest.class differ diff --git a/${project.build.directory}/test-classes/calculator/SquareRootTest.class b/${project.build.directory}/test-classes/calculator/SquareRootTest.class new file mode 100644 index 0000000..fbd8e4a Binary files /dev/null and b/${project.build.directory}/test-classes/calculator/SquareRootTest.class differ diff --git a/${project.build.directory}/test-classes/calculator/SubtractionTest.class b/${project.build.directory}/test-classes/calculator/SubtractionTest.class new file mode 100644 index 0000000..7f2651c Binary files /dev/null and b/${project.build.directory}/test-classes/calculator/SubtractionTest.class differ diff --git a/${project.build.directory}/test-classes/calculator/TangentTest.class b/${project.build.directory}/test-classes/calculator/TangentTest.class new file mode 100644 index 0000000..86a00e5 Binary files /dev/null and b/${project.build.directory}/test-classes/calculator/TangentTest.class differ diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..9f714fc --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +devops_calculator \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..ee128bc --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..f1755fe --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5f18975 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/devops_calculator.iml b/devops_calculator.iml new file mode 100644 index 0000000..d740aba --- /dev/null +++ b/devops_calculator.iml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/calculator/Addition.java b/src/main/java/calculator/Addition.java new file mode 100644 index 0000000..65a6576 --- /dev/null +++ b/src/main/java/calculator/Addition.java @@ -0,0 +1,29 @@ +package calculator; + +import java.text.DecimalFormat; + +/** + * The Addition class represents the operation of addition. + */ +public class Addition extends BinaryOperation { + + public Addition() { + this.name = "Addition"; + this.operator = "+"; + } + + public Addition(double a, double b) { + this(); + this.firstOperand = a; + this.secondOperand = b; + } + + public double perform() { + return this.firstOperand + this.secondOperand; + } + + public String showExpression() { + return simplifyNumber(this.firstOperand) + " + " + (this.secondOperand < 0? "(": "") + simplifyNumber(this.secondOperand) + (this.secondOperand < 0? ")": ""); + } + +} diff --git a/src/main/java/calculator/App.java b/src/main/java/calculator/App.java index 0ae3ba7..6e69137 100644 --- a/src/main/java/calculator/App.java +++ b/src/main/java/calculator/App.java @@ -2,57 +2,103 @@ import java.util.*; public class App { - public static void main(String args[]) + public static void main(String[] args) { - int choice, flag=0; - double num1,num2; - Scanner scan = new Scanner(System.in); + Calculator calculator = new Calculator(); + + int choice; + boolean validInput; + double num1 = 0, num2 = 0; System.out.println("Calculator"); + do { - System.out.println("1 for Addition\n2 for Subtraction\n3 for Multiplication\n4 for Division\n"); - System.out.print("Enter your choice: "); - choice = scan.nextInt(); - if (choice > 4 || choice <1) { - flag = 1; - System.out.println("Invalid choice, exiting\n"); + // Choose an action + choice = calculator.chooseAction(); + + if (choice == 0) { + System.out.println("Exiting, Bye"); + return; } - else if (flag != 1){ - System.out.println("Enter two numbers"); - System.out.print("Enter number 1: "); - num1 = scan.nextDouble(); - System.out.print("Enter number 2: "); - num2 = scan.nextDouble(); - - switch(choice) { - case 1: System.out.println(num1+" + "+num2+" = "+Add(num1, num2)); - break; - case 2: System.out.println(num1+" - "+num2+" = "+Subtract(num1, num2)); - break; - case 3: System.out.println(num1+" * "+num2+" = "+Multiply(num1, num2)); - break; - case 4: System.out.println(num1+" / "+num2+" = "+Divide(num1, num2)); - break; - default: System.out.println("Exiting, Bye"); - flag=1; + + Operation chosenOperation = calculator.getOperations().get(choice - 1); + System.out.println(chosenOperation.getName()); + Scanner scan = new Scanner(System.in).useLocale(Locale.US); + + // Scan operand(s) of the chosen operation + + // BINARY OPERATIONS (+, -, *, /) + if (chosenOperation instanceof BinaryOperation) { + System.out.println("Enter two numbers"); + + // first operand + validInput = false; + while (!validInput) { + try { + System.out.print("Enter number 1: "); + num1 = scan.nextDouble(); + } catch (Exception e) { + System.out.println("Invalid number, please try again."); + scan.nextLine(); + continue; + } + validInput = true; + } + + // second operand + validInput = false; + while (!validInput) { + try { + System.out.print("Enter number 2: "); + num2 = scan.nextDouble(); + } catch (Exception e) { + System.out.println("Invalid number, please try again."); + scan.nextLine(); + continue; + } + validInput = true; } } - System.out.println("\n"); - }while(flag==0); - } - - static double Add(double a, double b) { - return a + b; - } - static double Subtract(double a, double b) { - return a - b; - } - static double Multiply(double a, double b) { - return a * b; - } - static double Divide(double a, double b) { - if (b == 0) { - throw new IllegalArgumentException("Divisor cannot be zero, Exiting"); - } - else return a / b; + + // UNARY OPERATIONS (sin, cos, tan, sqrt, exp) + if (chosenOperation instanceof UnaryOperation) { + validInput = false; + while (!validInput) { + try { + System.out.print("Enter number" + ((chosenOperation instanceof TrigonometricOperation)? " (in radians)": "") + ": "); + num1 = scan.nextDouble(); + } catch (Exception e) { + System.out.println("Invalid number, please try again."); + scan.nextLine(); + continue; + } + validInput = true; + } + } + + // Set operand(s) to the chosen operation, + // perform the operation and + // print the results. + + double result; + chosenOperation = calculator.getOperations().get(choice - 1); + if (chosenOperation instanceof UnaryOperation) { + ((UnaryOperation) chosenOperation).setOperand(num1); + + result = chosenOperation.perform(); + + System.out.println(chosenOperation.showExpression() + " = " + chosenOperation.simplifyNumber(result)); + + } else if (chosenOperation instanceof BinaryOperation) { + ((BinaryOperation) chosenOperation).setFirstOperand(num1); + ((BinaryOperation) chosenOperation).setSecondOperand(num2); + + result = chosenOperation.perform(); + + System.out.println(chosenOperation.showExpression() + " = " + chosenOperation.simplifyNumber(result)); + } + + } while (choice != 0); + } -} \ No newline at end of file + +} diff --git a/src/main/java/calculator/BinaryOperation.java b/src/main/java/calculator/BinaryOperation.java new file mode 100644 index 0000000..64f1adf --- /dev/null +++ b/src/main/java/calculator/BinaryOperation.java @@ -0,0 +1,26 @@ +package calculator; + +/** + * The BinaryOperation class represents a binary operation. + */ +abstract public class BinaryOperation extends Operation { + protected double firstOperand; + protected double secondOperand; + + public double getFirstOperand() { + return firstOperand; + } + + public void setFirstOperand(double firstOperand) { + this.firstOperand = firstOperand; + } + + public double getSecondOperand() { + return secondOperand; + } + + public void setSecondOperand(double secondOperand) { + this.secondOperand = secondOperand; + } + +} diff --git a/src/main/java/calculator/Calculator.java b/src/main/java/calculator/Calculator.java new file mode 100644 index 0000000..1534fe7 --- /dev/null +++ b/src/main/java/calculator/Calculator.java @@ -0,0 +1,122 @@ +package calculator; + +import java.util.ArrayList; +import java.util.Locale; +import java.util.Scanner; + +/** + * The Calculator class represents a calculator. + */ +public class Calculator { + private ArrayList operations; + + public Calculator() { + this.operations = new ArrayList<>(); + this.initialise(); + } + + public ArrayList getOperations() { + return operations; + } + + /** + * Adds a new operation to the list of operations of the calculator. + * @param operation the operation to be added + */ + public void addOperation(Operation operation) { + this.operations.add(operation); + } + + /** + * Initialises the calculator by adding all the operations the calculator can perform. + * Available operations are: + * + */ + private void initialise() { + Addition addition = new Addition(); + this.addOperation(addition); + + Subtraction subtraction = new Subtraction(); + this.addOperation(subtraction); + + Multiplication multiplication = new Multiplication(); + this.addOperation(multiplication); + + Division division = new Division(); + this.addOperation(division); + + Sine sine = new Sine(); + this.addOperation(sine); + + Cosine cosine = new Cosine(); + this.addOperation(cosine); + + Tangent tangent = new Tangent(); + this.addOperation(tangent); + + SquareRoot squareRoot = new SquareRoot(); + this.addOperation(squareRoot); + + Exponential exponential = new Exponential(); + this.addOperation(exponential); + } + + /** + * This function prints all the actions the calculator can perform, + * along with the number that corresponds to each action. + */ + public void showActions() { + System.out.println("0 to Exit"); + + Calculator calculator = new Calculator(); + int i = 1; + for (Operation operation : calculator.getOperations()) { + System.out.println(i + " for " + operation.getName()); + i++; + } + } + + /** + * This function receives user input (an integer >= 0) about which action to perform. + * @return the choice of the user (integer >= 0) + */ + public int chooseAction() { + Scanner scan = new Scanner(System.in).useLocale(Locale.US); + int choice = -1; + boolean validChoice = false; + + while (!validChoice) { + try { + System.out.println(); + showActions(); + System.out.print("Enter your choice: "); + choice = scan.nextInt(); + } + catch (Exception e) { + System.out.println("Invalid choice, please try again."); + scan.nextLine(); + continue; + } + + Calculator calculator = new Calculator(); + + if (choice > calculator.getOperations().size() || choice < 0) { + System.out.println("Invalid choice, please try again."); + scan.nextLine(); + continue; + } + validChoice = true; + } + return choice; + } +} diff --git a/src/main/java/calculator/Cosine.java b/src/main/java/calculator/Cosine.java new file mode 100644 index 0000000..7a02a5f --- /dev/null +++ b/src/main/java/calculator/Cosine.java @@ -0,0 +1,26 @@ +package calculator; + +/** + * The Cosine class represents the cosine function. + */ +public class Cosine extends TrigonometricOperation { + + public Cosine() { + this.name = "Cosine"; + this.operator = "cos"; + } + + public Cosine(double a) { + this(); + this.operand = a; + } + + public double perform() { + return Math.cos(this.operand); + } + + public String showExpression() { + return "cos(" + simplifyNumber(this.operand) + ")"; + } + +} diff --git a/src/main/java/calculator/Division.java b/src/main/java/calculator/Division.java new file mode 100644 index 0000000..bd642ba --- /dev/null +++ b/src/main/java/calculator/Division.java @@ -0,0 +1,27 @@ +package calculator; + +/** + * The Division class represents the operation of division. + */ +public class Division extends BinaryOperation { + + public Division() { + this.name = "Division"; + this.operator = "/"; + } + + public Division(double a, double b) { + this(); + this.firstOperand = a; + this.secondOperand = b; + } + + public double perform() { + return this.firstOperand / this.secondOperand; + } + + public String showExpression() { + return simplifyNumber(this.firstOperand) + " / " + (this.secondOperand < 0? "(": "") + simplifyNumber(this.secondOperand) + (this.secondOperand < 0? ")": ""); + } + +} diff --git a/src/main/java/calculator/Exponential.java b/src/main/java/calculator/Exponential.java new file mode 100644 index 0000000..e1ea9f0 --- /dev/null +++ b/src/main/java/calculator/Exponential.java @@ -0,0 +1,26 @@ +package calculator; + +/** + * The Exponential class represents the exponential operation. + */ +public class Exponential extends UnaryOperation { + + public Exponential() { + this.name = "Exponential"; + this.operator = "exp"; + } + + public Exponential(double a) { + this(); + this.operand = a; + } + + public double perform() { + return Math.exp(this.operand); + } + + public String showExpression() { + return "exp(" + simplifyNumber(this.operand) + ")"; + } + +} diff --git a/src/main/java/calculator/Multiplication.java b/src/main/java/calculator/Multiplication.java new file mode 100644 index 0000000..4c15184 --- /dev/null +++ b/src/main/java/calculator/Multiplication.java @@ -0,0 +1,27 @@ +package calculator; + +/** + * The Multiplication class represents the operation of multiplication. + */ +public class Multiplication extends BinaryOperation { + + public Multiplication() { + this.name = "Multiplication"; + this.operator = "*"; + } + + public Multiplication(double a, double b) { + this(); + this.firstOperand = a; + this.secondOperand = b; + } + + public double perform() { + return this.firstOperand * this.secondOperand; + } + + public String showExpression() { + return simplifyNumber(this.firstOperand) + " * " + (this.secondOperand < 0? "(": "") + simplifyNumber(this.secondOperand) + (this.secondOperand < 0? ")": ""); + } + +} diff --git a/src/main/java/calculator/Operation.java b/src/main/java/calculator/Operation.java new file mode 100644 index 0000000..a7efa69 --- /dev/null +++ b/src/main/java/calculator/Operation.java @@ -0,0 +1,50 @@ +package calculator; + +/** + * The Operation class represents an arithmetic operation. + */ +abstract public class Operation { + protected String name; + protected String operator; + + public String getName() { + return this.name; + } + + public String getOperator() { + return operator; + } + + /** + * Shows the numeric expression produced by the operation. + * Something like toString(). + * For example, if the operation is addition, the function will print + * first_operand + second_operand + * Another example: if the operation is sine, the function will print + * sin(operand) + */ + abstract public String showExpression(); + + /** Performs the operation. + * @return the result of the operation + */ + abstract public double perform(); + + /** + * Simplifies the number given. Specifically: + * + */ + public String simplifyNumber(double number) { + String resultString; + if (number == (int) number) { + resultString = String.valueOf((int) number); + } else { + resultString = String.valueOf(number); + } + return resultString; + } + +} diff --git a/src/main/java/calculator/Sine.java b/src/main/java/calculator/Sine.java new file mode 100644 index 0000000..f407bf3 --- /dev/null +++ b/src/main/java/calculator/Sine.java @@ -0,0 +1,26 @@ +package calculator; + +/** + * The Sine class represents the sine function. + */ +public class Sine extends TrigonometricOperation { + + public Sine() { + this.name = "Sine"; + this.operator = "sin"; + } + + public Sine(double a) { + this(); + this.operand = a; + } + + public double perform() { + return Math.sin(this.operand); + } + + public String showExpression() { + return "sin(" + simplifyNumber(this.operand) + ")"; + } + +} diff --git a/src/main/java/calculator/SquareRoot.java b/src/main/java/calculator/SquareRoot.java new file mode 100644 index 0000000..0dc5c37 --- /dev/null +++ b/src/main/java/calculator/SquareRoot.java @@ -0,0 +1,26 @@ +package calculator; + +/** + * The SquareRoot class represents the square root operation. + */ +public class SquareRoot extends UnaryOperation { + + public SquareRoot() { + this.name = "Square Root"; + this.operator = "√"; + } + + public SquareRoot(double a) { + this(); + this.operand = a; + } + + public double perform() { + return Math.sqrt(this.operand); + } + + public String showExpression() { + return "√" + (this.operand < 0? "(": "") + simplifyNumber(this.operand) + (this.operand < 0? ")": ""); + } + +} diff --git a/src/main/java/calculator/Subtraction.java b/src/main/java/calculator/Subtraction.java new file mode 100644 index 0000000..0871faf --- /dev/null +++ b/src/main/java/calculator/Subtraction.java @@ -0,0 +1,27 @@ +package calculator; + +/** + * The Subtraction class represents the operation of subtraction. + */ +public class Subtraction extends BinaryOperation { + + public Subtraction() { + this.name = "Subtraction"; + this.operator = "-"; + } + + public Subtraction(double a, double b) { + this(); + this.firstOperand = a; + this.secondOperand = b; + } + + public double perform() { + return this.firstOperand - this.secondOperand; + } + + public String showExpression() { + return simplifyNumber(this.firstOperand) + " - " + (this.secondOperand < 0? "(": "") + simplifyNumber(this.secondOperand) + (this.secondOperand < 0? ")": ""); + } + +} diff --git a/src/main/java/calculator/Tangent.java b/src/main/java/calculator/Tangent.java new file mode 100644 index 0000000..21d1dcd --- /dev/null +++ b/src/main/java/calculator/Tangent.java @@ -0,0 +1,26 @@ +package calculator; + +/** + * The Tangent class represents the tangent function. + */ +public class Tangent extends TrigonometricOperation { + + public Tangent() { + this.name = "Tangent"; + this.operator = "tan"; + } + + public Tangent(double a) { + this(); + this.operand = a; + } + + public double perform() { + return Math.tan(this.operand); + } + + public String showExpression() { + return "tan(" + simplifyNumber(this.operand) + ")"; + } + +} diff --git a/src/main/java/calculator/TrigonometricOperation.java b/src/main/java/calculator/TrigonometricOperation.java new file mode 100644 index 0000000..7240f11 --- /dev/null +++ b/src/main/java/calculator/TrigonometricOperation.java @@ -0,0 +1,7 @@ +package calculator; + +/** + * The TrigonometricOperation class represents a trigonometric operation. + */ +abstract public class TrigonometricOperation extends UnaryOperation { +} diff --git a/src/main/java/calculator/UnaryOperation.java b/src/main/java/calculator/UnaryOperation.java new file mode 100644 index 0000000..dfdcf22 --- /dev/null +++ b/src/main/java/calculator/UnaryOperation.java @@ -0,0 +1,17 @@ +package calculator; + +/** + * The UnaryOperation class represents a unary operation. + */ +abstract public class UnaryOperation extends Operation { + protected double operand; + + public double getOperand() { + return operand; + } + + public void setOperand(double operand) { + this.operand = operand; + } + +} diff --git a/src/test/java/calculator/AdditionTest.java b/src/test/java/calculator/AdditionTest.java new file mode 100644 index 0000000..5d6f611 --- /dev/null +++ b/src/test/java/calculator/AdditionTest.java @@ -0,0 +1,50 @@ +package calculator; + +import static org.junit.jupiter.api.Assertions.*; + +class AdditionTest { + + @org.junit.jupiter.api.Test + void performTest1() { + double a = 15.0; + double b = 20.0; + Addition addition = new Addition(a, b); + double expectedResult = 35.0; + double result = addition.perform(); + assertEquals(expectedResult, result, 0); + } + + @org.junit.jupiter.api.Test + void performTest2() { + double a = 15.0; + double b = -20.0; + Addition addition = new Addition(a, b); + double expectedResult = -5.0; + double result = addition.perform(); + assertEquals(expectedResult, result, 0); + } + + @org.junit.jupiter.api.Test + void showOperationExpression() { + Addition addition = new Addition(45, 75); + String expectedResult = "45 + 75"; + String result = addition.showExpression(); + assertEquals(expectedResult, result); + + addition = new Addition(45, -75.3); + expectedResult = "45 + (-75.3)"; + result = addition.showExpression(); + assertEquals(expectedResult, result); + + addition = new Addition(-45.5, 75); + expectedResult = "-45.5 + 75"; + result = addition.showExpression(); + assertEquals(expectedResult, result); + + addition = new Addition(0, 0.0); + expectedResult = "0 + 0"; + result = addition.showExpression(); + assertEquals(expectedResult, result); + + } +} \ No newline at end of file diff --git a/src/test/java/calculator/CalculatorTest.java b/src/test/java/calculator/CalculatorTest.java index e02740f..9576e32 100644 --- a/src/test/java/calculator/CalculatorTest.java +++ b/src/test/java/calculator/CalculatorTest.java @@ -13,46 +13,4 @@ public void setUp() { calculator = new App(); } - @Test - public void testAdd() { - double a = 15.0; - double b = 20.0; - double expectedResult = 35.0; - double result = calculator.Add(a, b); - Assert.assertEquals(expectedResult, result, 0); - } - - @Test - public void testSubtract() { - double a = 34; - double b = 12; - double expectedResult = 22; - double result = calculator.Subtract(a, b); - Assert.assertEquals(expectedResult, result, 0); - } - - @Test - public void testMultiply() { - double a = 18.5; - double b = 12.6; - double expectedResult = 233.1; - double result = calculator.Multiply(a, b); - Assert.assertEquals(expectedResult, result, 0); - } - - @Test - public void testDivide() { - double a = 60.0; - double b = 12.0; - double expectedResult = 5.0; - double result = calculator.Divide(a, b); - Assert.assertEquals(expectedResult, result, 0.0001); - } - - @Test(expected = IllegalArgumentException.class) - public void testDivideByZero() { - int a = 25; - int b = 0; - calculator.Divide(a, b); - } } diff --git a/src/test/java/calculator/CosineTest.java b/src/test/java/calculator/CosineTest.java new file mode 100644 index 0000000..a87333d --- /dev/null +++ b/src/test/java/calculator/CosineTest.java @@ -0,0 +1,51 @@ +package calculator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class CosineTest { + + @BeforeEach + void setUp() { + } + + @Test + void perform() { + double a = 0.0; + Cosine cosine = new Cosine(a); + double expectedResult = 1.0; + double result = cosine.perform(); + assertEquals(expectedResult, result, 0); + + a = 89.0; + cosine = new Cosine(a); + expectedResult = 0.51017704494; + result = cosine.perform(); + assertEquals(expectedResult, result, 0.00000000001); + } + + @Test + void showOperationExpression() { + Cosine cosine = new Cosine(75); + String expectedResult = "cos(75)"; + String result = cosine.showExpression(); + assertEquals(expectedResult, result); + + cosine = new Cosine(-75.57); + expectedResult = "cos(-75.57)"; + result = cosine.showExpression(); + assertEquals(expectedResult, result); + + cosine = new Cosine(-0.0); + expectedResult = "cos(0)"; + result = cosine.showExpression(); + assertEquals(expectedResult, result); + + cosine = new Cosine(0); + expectedResult = "cos(0)"; + result = cosine.showExpression(); + assertEquals(expectedResult, result); + } +} \ No newline at end of file diff --git a/src/test/java/calculator/DivisionTest.java b/src/test/java/calculator/DivisionTest.java new file mode 100644 index 0000000..65c317d --- /dev/null +++ b/src/test/java/calculator/DivisionTest.java @@ -0,0 +1,46 @@ +package calculator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class DivisionTest { + + @BeforeEach + void setUp() { + } + + @Test + void perform() { + double a = 60.0; + double b = 12.0; + Division division = new Division(a, b); + double expectedResult = 5.0; + double result = division.perform(); + assertEquals(expectedResult, result, 0.0001); + } + + @Test + void showOperationExpression() { + Division division = new Division(45, 75); + String expectedResult = "45 / 75"; + String result = division.showExpression(); + assertEquals(expectedResult, result); + + division = new Division(45, -75.3); + expectedResult = "45 / (-75.3)"; + result = division.showExpression(); + assertEquals(expectedResult, result); + + division = new Division(-45.5, 75); + expectedResult = "-45.5 / 75"; + result = division.showExpression(); + assertEquals(expectedResult, result); + + division = new Division(0, 0.0); + expectedResult = "0 / 0"; + result = division.showExpression(); + assertEquals(expectedResult, result); + } +} \ No newline at end of file diff --git a/src/test/java/calculator/ExponentialTest.java b/src/test/java/calculator/ExponentialTest.java new file mode 100644 index 0000000..50c2886 --- /dev/null +++ b/src/test/java/calculator/ExponentialTest.java @@ -0,0 +1,52 @@ +package calculator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ExponentialTest { + + @BeforeEach + void setUp() { + } + + @Test + void perform() { + double a = 0; + Exponential exponential = new Exponential(a); + double expectedResult = 1.0; + double result = exponential.perform(); + assertEquals(expectedResult, result, 0); + + a = 2.4; + exponential = new Exponential(a); + expectedResult = 11.0231763806; + result = exponential.perform(); + assertEquals(expectedResult, result, 0.0000000001); + } + + @Test + void showOperationExpression() { + Exponential exponential = new Exponential(75); + String expectedResult = "exp(75)"; + String result = exponential.showExpression(); + assertEquals(expectedResult, result); + + exponential = new Exponential(-75.57); + expectedResult = "exp(-75.57)"; + result = exponential.showExpression(); + assertEquals(expectedResult, result); + + exponential = new Exponential(-0.0); + expectedResult = "exp(0)"; + result = exponential.showExpression(); + assertEquals(expectedResult, result); + + exponential = new Exponential(0); + expectedResult = "exp(0)"; + result = exponential.showExpression(); + assertEquals(expectedResult, result); + + } +} \ No newline at end of file diff --git a/src/test/java/calculator/MultiplicationTest.java b/src/test/java/calculator/MultiplicationTest.java new file mode 100644 index 0000000..7368e6f --- /dev/null +++ b/src/test/java/calculator/MultiplicationTest.java @@ -0,0 +1,46 @@ +package calculator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class MultiplicationTest { + + @BeforeEach + void setUp() { + } + + @Test + void perform() { + double a = 18.5; + double b = 12.6; + Multiplication multiplication = new Multiplication(a, b); + double expectedResult = 233.1; + double result = multiplication.perform(); + assertEquals(expectedResult, result, 0); + } + + @Test + void showOperationExpression() { + Multiplication multiplication = new Multiplication(45, 75); + String expectedResult = "45 * 75"; + String result = multiplication.showExpression(); + assertEquals(expectedResult, result); + + multiplication = new Multiplication(45, -75.3); + expectedResult = "45 * (-75.3)"; + result = multiplication.showExpression(); + assertEquals(expectedResult, result); + + multiplication = new Multiplication(-45.5, 75); + expectedResult = "-45.5 * 75"; + result = multiplication.showExpression(); + assertEquals(expectedResult, result); + + multiplication = new Multiplication(0, 0.0); + expectedResult = "0 * 0"; + result = multiplication.showExpression(); + assertEquals(expectedResult, result); + } +} \ No newline at end of file diff --git a/src/test/java/calculator/SineTest.java b/src/test/java/calculator/SineTest.java new file mode 100644 index 0000000..6b3b9da --- /dev/null +++ b/src/test/java/calculator/SineTest.java @@ -0,0 +1,51 @@ +package calculator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class SineTest { + + @BeforeEach + void setUp() { + } + + @Test + void perform() { + double a = 0.0; + Sine sine = new Sine(a); + double expectedResult = 0.0; + double result = sine.perform(); + assertEquals(expectedResult, result, 0); + + a = 52.0; + sine = new Sine(a); + expectedResult = 0.98662759204; + result = sine.perform(); + assertEquals(expectedResult, result, 0.00000000001); + } + + @Test + void showOperationExpression() { + Sine sine = new Sine(75); + String expectedResult = "sin(75)"; + String result = sine.showExpression(); + assertEquals(expectedResult, result); + + sine = new Sine(-75.57); + expectedResult = "sin(-75.57)"; + result = sine.showExpression(); + assertEquals(expectedResult, result); + + sine = new Sine(-0.0); + expectedResult = "sin(0)"; + result = sine.showExpression(); + assertEquals(expectedResult, result); + + sine = new Sine(0); + expectedResult = "sin(0)"; + result = sine.showExpression(); + assertEquals(expectedResult, result); + } +} \ No newline at end of file diff --git a/src/test/java/calculator/SquareRootTest.java b/src/test/java/calculator/SquareRootTest.java new file mode 100644 index 0000000..44d9a3d --- /dev/null +++ b/src/test/java/calculator/SquareRootTest.java @@ -0,0 +1,52 @@ +package calculator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class SquareRootTest { + + @BeforeEach + void setUp() { + } + + @Test + void perform() { + double a = 4.0; + SquareRoot squareRoot = new SquareRoot(a); + double expectedResult = 2.0; + double result = squareRoot.perform(); + assertEquals(expectedResult, result, 0); + + a = 2.5; + squareRoot = new SquareRoot(a); + expectedResult = 1.58113883008; + result = squareRoot.perform(); + assertEquals(expectedResult, result, 0.00000000001); + } + + @Test + void showOperationExpression() { + SquareRoot squareRoot = new SquareRoot(75); + String expectedResult = "√75"; + String result = squareRoot.showExpression(); + assertEquals(expectedResult, result); + + squareRoot = new SquareRoot(-75.57); + expectedResult = "√(-75.57)"; + result = squareRoot.showExpression(); + assertEquals(expectedResult, result); + + squareRoot = new SquareRoot(-0.0); + expectedResult = "√0"; + result = squareRoot.showExpression(); + assertEquals(expectedResult, result); + + squareRoot = new SquareRoot(0); + expectedResult = "√0"; + result = squareRoot.showExpression(); + assertEquals(expectedResult, result); + + } +} \ No newline at end of file diff --git a/src/test/java/calculator/SubtractionTest.java b/src/test/java/calculator/SubtractionTest.java new file mode 100644 index 0000000..aa7472c --- /dev/null +++ b/src/test/java/calculator/SubtractionTest.java @@ -0,0 +1,47 @@ +package calculator; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.Assert.assertEquals; + +class SubtractionTest { + + @BeforeEach + void setUp() { + } + + @Test + void perform() { + double a = 34; + double b = 12; + Subtraction subtraction = new Subtraction(a, b); + double expectedResult = 22; + double result = subtraction.perform(); + Assertions.assertEquals(expectedResult, result, 0); + } + + @Test + void showOperationExpression() { + Subtraction subtraction = new Subtraction(45, 75); + String expectedResult = "45 - 75"; + String result = subtraction.showExpression(); + Assertions.assertEquals(expectedResult, result); + + subtraction = new Subtraction(45, -75.3); + expectedResult = "45 - (-75.3)"; + result = subtraction.showExpression(); + Assertions.assertEquals(expectedResult, result); + + subtraction = new Subtraction(-45.5, 75); + expectedResult = "-45.5 - 75"; + result = subtraction.showExpression(); + Assertions.assertEquals(expectedResult, result); + + subtraction = new Subtraction(0, 0.0); + expectedResult = "0 - 0"; + result = subtraction.showExpression(); + Assertions.assertEquals(expectedResult, result); + } +} \ No newline at end of file diff --git a/src/test/java/calculator/TangentTest.java b/src/test/java/calculator/TangentTest.java new file mode 100644 index 0000000..24bb907 --- /dev/null +++ b/src/test/java/calculator/TangentTest.java @@ -0,0 +1,51 @@ +package calculator; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class TangentTest { + + @BeforeEach + void setUp() { + } + + @Test + void perform() { + double a = 0.0; + Tangent tangent = new Tangent(a); + double expectedResult = 0.0; + double result = tangent.perform(); + assertEquals(expectedResult, result, 0); + + a = 89.9; + tangent = new Tangent(a); + expectedResult = -2.62003383987; + result = tangent.perform(); + assertEquals(expectedResult, result, 0.00000000001); + } + + @Test + void showOperationExpression() { + Tangent tangent = new Tangent(75); + String expectedResult = "tan(75)"; + String result = tangent.showExpression(); + assertEquals(expectedResult, result); + + tangent = new Tangent(-75.57); + expectedResult = "tan(-75.57)"; + result = tangent.showExpression(); + assertEquals(expectedResult, result); + + tangent = new Tangent(-0.0); + expectedResult = "tan(0)"; + result = tangent.showExpression(); + assertEquals(expectedResult, result); + + tangent = new Tangent(0); + expectedResult = "tan(0)"; + result = tangent.showExpression(); + assertEquals(expectedResult, result); + } +} \ No newline at end of file diff --git a/target/classes/META-INF/MANIFEST.MF b/target/classes/META-INF/MANIFEST.MF deleted file mode 100644 index 92de182..0000000 --- a/target/classes/META-INF/MANIFEST.MF +++ /dev/null @@ -1,5 +0,0 @@ -Manifest-Version: 1.0 -Built-By: swatinmurthy -Build-Jdk: 11.0.4 -Created-By: Maven Integration for Eclipse - diff --git a/target/classes/META-INF/maven/calculator/devops_calculator/pom.properties b/target/classes/META-INF/maven/calculator/devops_calculator/pom.properties deleted file mode 100644 index cfb1f88..0000000 --- a/target/classes/META-INF/maven/calculator/devops_calculator/pom.properties +++ /dev/null @@ -1,7 +0,0 @@ -#Generated by Maven Integration for Eclipse -#Sat May 09 08:02:23 IST 2020 -m2e.projectLocation=/Users/swatinmurthy/eclipse-workspace/devops_calculator -m2e.projectName=devops_calculator -groupId=calculator -artifactId=devops_calculator -version=1.0-SNAPSHOT diff --git a/target/classes/META-INF/maven/calculator/devops_calculator/pom.xml b/target/classes/META-INF/maven/calculator/devops_calculator/pom.xml deleted file mode 100644 index 5bd1034..0000000 --- a/target/classes/META-INF/maven/calculator/devops_calculator/pom.xml +++ /dev/null @@ -1,48 +0,0 @@ - - - - 4.0.0 - - calculator - devops_calculator - 1.0-SNAPSHOT - - devops_calculator - - http://www.example.com - - - UTF-8 - 1.8 - 1.8 - - - - - junit - junit - 3.8.1 - test - - - junit - junit - 4.12 - test - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.7.0 - - 1.8 - 1.8 - - - - - diff --git a/target/classes/calculator/App.class b/target/classes/calculator/App.class deleted file mode 100644 index 6c45bf4..0000000 Binary files a/target/classes/calculator/App.class and /dev/null differ