diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..9a874b5 Binary files /dev/null and b/.DS_Store differ diff --git a/README.md b/README.md index 8402302..f2f084a 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ## Description -In this project your team will build a small app to function as a calculator. This app will be built in Java, and will use the topics and techniques discussed during the week. +In this project your team will build a small app to function as a calculatorEngine. This app will be built in Java, and will use the topics and techniques discussed during the week. Your team should work on this project in a single repository. Click the `fork` button in the top right corner to create a copy of this repository in one of your github accounts. You can go through the [GitHub forking tutorial](https://help.github.com/articles/fork-a-repo/) if you need additional practice with this. @@ -23,7 +23,7 @@ You must produce UML diagrams for your program. All classes (excluding test clas All calculators should have the following features: -- A state, representing the value currently displayed on the calculator (default 0) * +- A state, representing the value currently displayed on the calculatorEngine (default 0) * - Get the current number on the display * - Clear the display * - Change the number on the display * @@ -67,7 +67,7 @@ Each operation should automatically update the display ### Custom Features -In addition to the Core and Scientific features, you are required to create at least two of your own features for the calculator. They can be any two features that are not already covered and that you can implement as you see fit. These features must be properly tested. +In addition to the Core and Scientific features, you are required to create at least two of your own features for the calculatorEngine. They can be any two features that are not already covered and that you can implement as you see fit. These features must be properly tested. ### Hints diff --git a/Scientific-Calc UML.png b/Scientific-Calc UML.png new file mode 100644 index 0000000..a0fedbc Binary files /dev/null and b/Scientific-Calc UML.png differ diff --git a/pom.xml b/pom.xml index e7cb4f6..2d16867 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,26 @@ com.zipcodewilmington scientific_calculator 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + + + + junit + junit + 4.12 + test + + \ No newline at end of file diff --git a/src/.idea/.gitignore b/src/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/src/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/src/.idea/misc.xml b/src/.idea/misc.xml new file mode 100644 index 0000000..8ed5f34 --- /dev/null +++ b/src/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/.idea/modules.xml b/src/.idea/modules.xml new file mode 100644 index 0000000..31eaf94 --- /dev/null +++ b/src/.idea/modules.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/.idea/vcs.xml b/src/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/src/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator.java new file mode 100644 index 0000000..8b5315b --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator.java @@ -0,0 +1,16 @@ +package com.zipcodewilmington.scientificcalculator; + +public class Calculator { + + private static int result; + + public static int sum(int firstNum, int secondNum) { + result = firstNum + secondNum; + return result; + } + + public int subtract(int firstNum, int secondNum) { + result = firstNum - secondNum; + return result; + } +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/CalculatorDisplay.java b/src/main/java/com/zipcodewilmington/scientificcalculator/CalculatorDisplay.java new file mode 100644 index 0000000..ca2252c --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/CalculatorDisplay.java @@ -0,0 +1,32 @@ +package com.zipcodewilmington.scientificcalculator; + +public class CalculatorDisplay { + + public static void menuOption1() { + String prompt; + outerLoop: + do { + prompt = Console.getStringInput(""); + switch (prompt.toLowerCase()) { + case "1": + System.out.println("Here are some Basic Functions. \n(+) - Addition \n(-) - Subtraction \n(/) - Division \n(*) - Multiplication"); + break; + case "2": + System.out.println("Here are some Scientific Functions. \nSine() \nCosine() \nTangent()"); + break; + case "3": + String userInput = Console.getStringInput("Time for some math? \n ( Y / N )"); + if (userInput.equalsIgnoreCase("Y")) { + break outerLoop; + } + break; + default: + Console.println("[ %s ] is not a valid input!", prompt); + break; + } + } while (true); + } + public static void basicOperators(){ + + } +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/CalculatorEngine.java b/src/main/java/com/zipcodewilmington/scientificcalculator/CalculatorEngine.java new file mode 100644 index 0000000..a3669eb --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/CalculatorEngine.java @@ -0,0 +1,69 @@ +package com.zipcodewilmington.scientificcalculator; + +public class CalculatorEngine { + // CalculatorEngine calculatorEngine = new CalculatorEngine(); + private static Double value; + + public static Double sum(Double firstNum, Double secondNum) { + return firstNum + secondNum; + } + + public static Double subtract(Double firstNum, Double secondNum) { + return firstNum - secondNum; + } + + public static Double divide(Double firstNum, Double secondNum) { + return firstNum / secondNum; + } + + public static Double multiply(Double firstNum, Double secondNum) { + return firstNum * secondNum; + } + + public static Double squareRoot(Double firstNum){ + double s; + double value = firstNum / 2; + do { + s = value; + value = (s +(firstNum / s)) / 2; + } + while ((s - value) != 0); + return value; + } + + public static Double square(Double firstNum){ + return firstNum * firstNum; + } + + public static Double squareY(Double firstNum, Double secondNum) { + return Math.pow(firstNum, secondNum); + + } + + public static Double squareRootY(Double firstNum, Double secondNum){ + double xPre = Math.random() % 10; + double eps = 0.001; + double delX = 2147483647; + double xK = 0.00; + double A = firstNum; + double N = secondNum; + + + while (delX > eps){ + xK = ((N - 1.0) * xPre + A / Math.pow(xPre, N - 1)) / N; + delX = Math.abs(xK - xPre); + xPre = xK; + } + double value = Math.round(xK * 1000.00) / 1000.00; + return value; + } + + public static Double inverse(Double firstNum){ + return 1 / firstNum; + } + + public static Double signSwitch(Double firstNum){ + return firstNum * (-1); + } + + } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java index 83f0e97..92dff0f 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java @@ -10,7 +10,6 @@ public class Console { public static void print(String output, Object... args) { System.out.printf(output, args); } - public static void println(String output, Object... args) { print(output + "\n", args); } @@ -23,10 +22,16 @@ public static String getStringInput(String prompt) { } public static Integer getIntegerInput(String prompt) { - return null; + Scanner scanner = new Scanner(System.in); + System.out.println(prompt); + Integer userInput = scanner.nextInt(); + return userInput; } public static Double getDoubleInput(String prompt) { - return null; + Scanner scanner = new Scanner(System.in); + System.out.println(prompt); + Double userInput = scanner.nextDouble(); + return userInput; } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 5f42132..f616e9f 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -1,17 +1,72 @@ package com.zipcodewilmington.scientificcalculator; +import java.util.Scanner; /** * Created by leon on 2/9/18. */ public class MainApplication { - public static void main(String[] args) { - Console.println("Welcome to my calculator!"); - String s = Console.getStringInput("Enter a string"); - Integer i = Console.getIntegerInput("Enter an integer"); - Double d = Console.getDoubleInput("Enter a double."); - - Console.println("The user input %s as a string", s); - Console.println("The user input %s as a integer", i); - Console.println("The user input %s as a d", d); + + private CalculatorEngine engine = new CalculatorEngine(); + private CalculatorDisplay display = new CalculatorDisplay(); + + public static void main(String[] args) { + System.out.println("Welcome! \nTake a look at some of our functions!"); + System.out.println("(1) Basic Functions \n(2) Scientific Functions \n(3) Math time!"); + CalculatorDisplay.menuOption1(); + + System.out.println("Please choose an operator ( +, - , / , * )"); + String operator = Console.getStringInput(""); + Console.println("You entered %s\n", operator); + System.out.println("Enter your first number"); + Double firstNum = Console.getDoubleInput(""); + Console.println("%s", firstNum); + System.out.println("Enter your second number"); + Double secondNum = Console.getDoubleInput(""); + Console.println("%s", secondNum); + + String basicMathOperator = operator; + + switch (basicMathOperator) { + case "+": + System.out.println(CalculatorEngine.sum(firstNum, secondNum)); + break; + case "-": + System.out.println(); + CalculatorEngine.subtract(firstNum, secondNum); + break; + case "/": + System.out.println(CalculatorEngine.divide(firstNum, secondNum)); + break; + case "*": + System.out.println(CalculatorEngine.multiply(firstNum, secondNum)); + break; + default: + System.out.println("Unable to do math."); } + + String scientificOperator = operator; + switch (scientificOperator) { + case "cos": + System.out.println(Math.cos(firstNum)); + break; + case "tan": + System.out.println(Math.sin(firstNum)); + break; + case "sin": + System.out.println(Math.tan(firstNum)); + break; + default: + System.out.println("Unable to do math."); + } + } } + +/* + String s = Console.getStringInput("Enter a string"); + Integer i = Console.getIntegerInput("Enter an integer"); + Double d = Console.getDoubleInput("Enter a double."); + Console.println("The user input %s as a string", s); + Console.println("The user input %s as a integer", i); + Console.println("The user input %s as a double", d); +*/ + diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Scientific.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Scientific.java new file mode 100644 index 0000000..8b8d121 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Scientific.java @@ -0,0 +1,157 @@ + +package com.zipcodewilmington.scientificcalculator; + +public class Scientific { + private static double value; + + public static Double tan (double value) { + value = 0; + value = Math.tan(value); + return value; + } + + public static Double tanR(double value) { + value = 0; + value = Math.atan(value); + return value; + } + + + public static Double cos(double value) { + value = 0; + value = Math.cos(value); + return value; + } + + public static double sin(double value) { + value = 0; + value = Math.sin(value); + return value; + } + + public static double cb(double value) { + value = 0; + value= Math.pow(value, 3); + return value; + } + + public static double sqrt(double value) { + value = 0; + value = Math.sqrt(value); + return value; + } + + public static double squ(double value) { + value = 0; + value = Math.pow(value, 2); + return value; + } + + + public static double sinR(double value) { + value = 0; + value = Math.asin(value); + return value; + } + public static double cosR(double value){ + value = 0; + value = Math.acos(value); + return value; + } +} + /*public double result; + public static void main(String[] args) { + int firstNum = 0; + double value = 0.0; + char operator = 0; + + switch (operator) { + case "cos": + value = Math.cos(firstNum); + break; + case "tan": + value = Math.sin(firstNum); + break; + + case "sin": + value = Math.tan(firstNum); + break; + + case "acos": + value = Math.acos(firstNum); + break; + + case "atan": + value = Math.atan(firstNum); + break; + + case "asin": + value = Math.asin(firstNum); + break; + + default: + System.out.println("ERR"); + } + + switch (operator) { + case "x2": + value = Math.pow(firstNum, 2); + break; + case "x^": + value = Math.sqrt(firstNum); + break; + + case "x3": + value = Math.pow(firstNum, 3); + break; + + case "!x": + long fact = 1; + for (int i = 2; i <= firstNum; i++) { + value = fact * i; + } + break; + default: + System.out.println("ERR"); + } + + switch (operator) { + case ".log": + value = Math.log10(firstNum); + break; + case "e": + value = Math.exp(firstNum); + break; + case "10x": + value = Math.exp(firstNum, 10); + break; + case "-x": + value = firstNum * -1; + break; + case "Ln": + value = Math.log(firstNum); + break; + default + System.out.println("ERR"); + } + public static boolean isRadian ( double input){ + double to180 = (input + ((180 / Math.PI) - input)); + // 57 is 180 degrees rounded to whole number + if (Math.round(to180) == 57) { + return true; + } else { +<<<<<<< HEAD + } + return false; + } + */ + +/*======= + }return false; + + + } + } +} +*/ + diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/CalculatorEngineTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/CalculatorEngineTest.java new file mode 100644 index 0000000..ee9dc2b --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/CalculatorEngineTest.java @@ -0,0 +1,155 @@ +package com.zipcodewilmington.scientific_calculator; + +import com.zipcodewilmington.scientificcalculator.CalculatorEngine; +import org.junit.Assert; +import org.junit.Test; + +import static com.sun.tools.doclint.Entity.divide; +import static com.zipcodewilmington.scientificcalculator.CalculatorEngine.*; + + +public class CalculatorEngineTest { + + @Test + public void testSum1() { + // Given + CalculatorEngine CalculatorEngine = new CalculatorEngine(); + // When + Double result; + result = CalculatorEngine.sum(2.00, 2.00); + // Then + if (result != 4.00) { // if 2 + 2 != 4 + Assert.fail(); + } + } + @Test + public void testSum2() { + // Given + CalculatorEngine CalculatorEngine = new CalculatorEngine(); + // When + Double result; + result = CalculatorEngine.sum(1200.00, 500.00); + // Then + if (result != 1700.00) { // if 1200 + 500 != 1700 + Assert.fail(); + } + } + @Test + public void testSubtract1() { + // Given + CalculatorEngine calculatorEngine = new CalculatorEngine(); + // When + Double result; + result = calculatorEngine.subtract(10.00, 5.00); + // Then + if (result != 5.00) { // 10 - 5 = 5 + Assert.fail(); + } + } + @Test + public void testSubtract2() { + // Given + CalculatorEngine calculatorEngine = new CalculatorEngine(); + // When + Double result; + result = calculatorEngine.subtract(20.00, 3.00); + // Then + if (result != 17.00) { // 20 - 3 = 17 + Assert.fail(); + } + } + + @Test + public void testDivide1() { + Double expected = 100.00; + Double firstNum = 1000.00; + Double secondNum = 10.00; + Double actual = CalculatorEngine.divide(firstNum, secondNum); + Assert.assertEquals(expected, actual); + } + + @Test + public void testDivide2() { + Double expected = 4.00; + Double firstNum = 20.00; + Double secondNum = 5.00; + Double actual = CalculatorEngine.divide(firstNum, secondNum); + Assert.assertEquals(expected, actual); + } + + @Test + public void testMultiply1() { + Double expected = 100.00; + Double firstNum = 20.00; + Double secondNum = 5.00; + Double actual = multiply(firstNum, secondNum); + Assert.assertEquals(expected, actual); + } + + + @Test + public void testMultiply2() { + Double expected = 4.00; + Double firstNum = 2.00; + Double secondNum = 2.0; + Double actual = multiply(firstNum, secondNum); + Assert.assertEquals(expected, actual); + } + + @Test + public void testSquareRoot(){ + Double expected = 2.00; + Double firstNum = 4.00; + Double actual = squareRoot(firstNum); + Assert.assertEquals(expected, actual); + } + + @Test + public void testSquare(){ + Double expected = 16.00; + Double firstNum = 4.00; + Double actual = square(firstNum); + Assert.assertEquals(expected, actual); + } + @Test + public void testSquareY(){ + Double expected = 7776.00; + Double firstNum = 6.00; + Double secondNumb = 5.00; + Double actual = CalculatorEngine.squareY(firstNum, secondNumb); + Assert.assertEquals(expected, actual); + } + + @Test + public void testSquareRootY(){ + Double expected = 1.431; + Double firstNum = 6.00; + Double secondNumb = 5.00; + Double actual = CalculatorEngine.squareRootY(firstNum, secondNumb); + Assert.assertEquals(expected, actual); + } + @Test + public void testInverse(){ + Double expected = 0.125; + Double firstNum = 8.00; + Double actual = CalculatorEngine.inverse(firstNum); + Assert.assertEquals(expected, actual); + } + @Test + public void testSignSwitch(){ + Double expected = (-6.00); + Double firstNum = (6.00); + Double actual = CalculatorEngine.signSwitch(firstNum); + } +} + + + +/* @Test(expected = ArithmeticException.class) + public void testDivideWillThrowExceptionWhenDivideOnZero() { + Calculator calculator = new Calculator(); + calculator.divide(6, 0); + } +} */ + + diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/ScientificTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/ScientificTest.java new file mode 100644 index 0000000..6b20b8c --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/ScientificTest.java @@ -0,0 +1,92 @@ +package com.zipcodewilmington.scientific_calculator; + +import com.zipcodewilmington.scientificcalculator.CalculatorEngine; +import com.zipcodewilmington.scientificcalculator.Scientific; +import org.junit.Assert; +import org.junit.Test; + +public class ScientificTest { + @Test + public void testTan() { + Scientific scientific = new Scientific(); + Double value; + value = scientific.tan(7); + if (value == 0.122784560) + { // + Assert.fail(); + } + } + @Test + public void testTanR() { + Scientific scientific = new Scientific(); + Double value; + value = scientific.tanR(0.10955952677); + if (value == Math.atan(.11)) + { // + Assert.fail(); + } + } + @Test + public void testCos(){ + Scientific scientific = new Scientific(); + Double value; + value = scientific.cos(-0.4480736161291702); + if (value == Math.cos(90.0)){ + Assert.fail(); + } + } + @Test + public void testCosR(){ + Scientific scientific = new Scientific(); + Double value; + value = scientific.cos(1.0); + if (value == Math.acos(-1.99)){ + Assert.fail(); + } + } + @Test + public void testSine (){ + Scientific scientific = new Scientific(); + double value; + value = scientific.sin(0.8939966636005579); + if (value != scientific.sin(90)){ + Assert.fail(); + } + } + @Test + public void testSineR (){ + Scientific scientific = new Scientific(); + double value; + value = scientific.sinR(8); + if (value != scientific.sinR(5.0)){ + Assert.fail(); + } + } + @Test + public void testCubed (){ + Scientific scientific = new Scientific(); + double value; + value = scientific.cb(3); + if (value != scientific.cb(10)){ + Assert.fail(); + } + } + @Test + public void testSqrt (){ + Scientific scientific = new Scientific(); + double value; + value = scientific.sqrt(25); + if (value != scientific.sqrt(5)){ + Assert.fail(); + } + } + @Test + public void testSquare (){ + Scientific scientific = new Scientific(); + double value; + value = scientific.squ(5); + if (value != scientific.squ(25)){ + Assert.fail(); + } + } +}