diff --git a/pom.xml b/pom.xml index b92b052..34d2708 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,8 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd +http://maven.apache.org/POM/4.0.0 "> 4.0.0 com.zipcodewilmington @@ -11,9 +12,34 @@ 11 11 + 1.9.22 + + kotlin-maven-plugin + org.jetbrains.kotlin + ${kotlin.version} + + + compile + compile + + compile + + + + test-compile + test-compile + + test-compile + + + + + ${maven.compiler.target} + + org.apache.maven.plugins maven-compiler-plugin @@ -21,6 +47,30 @@ 11 + + + default-compile + none + + + default-testCompile + none + + + compile + compile + + compile + + + + testCompile + test-compile + + testCompile + + + @@ -48,6 +98,17 @@ 4.12 test + + kotlin-stdlib-jdk8 + org.jetbrains.kotlin + ${kotlin.version} + + + kotlin-test + org.jetbrains.kotlin + ${kotlin.version} + test + diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/CoreCalculations.java b/src/main/java/com/zipcodewilmington/scientificcalculator/CoreCalculations.java new file mode 100644 index 0000000..417eb60 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/CoreCalculations.java @@ -0,0 +1,55 @@ +package com.zipcodewilmington.scientificcalculator; + +public class CoreCalculations { + + public double addition(double displayNum, double num){ + return displayNum + num; + } + + + public double subtraction(double displayNum, double num) { + return displayNum - num; + } + + public double multiplication(double displayNum, double num) { + return displayNum*num; + } + + public double division(double displayNum, double error) { + if (error != 0) { + displayNum /= error; + + } else { + System.out.println("Err: Cannot divide a number by 0.\n"); + displayNum = Double.NaN; + } + return displayNum; + } + + public double square(double displayNum) { + + return displayNum*displayNum; + } + public double squareRoot(double displayNum) { + + if (displayNum >= 0) { + displayNum = Math.sqrt(displayNum); + } else { + System.out.println("Err: user cannot take the square root of a negative number."); + displayNum = Double.NaN; + } + return displayNum; + } + public double exponent(double displayNum, double exponent) { + return Math.pow(displayNum,exponent); + } + public double inverse(double displayNum) { + if (displayNum != 0) { + displayNum = 1 / displayNum; + } else { + System.out.println("Err: user cannot take the inverse of 0."); + displayNum = Double.NaN; + } + return displayNum; + } +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java b/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java new file mode 100644 index 0000000..73c49ee --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java @@ -0,0 +1,35 @@ +package com.zipcodewilmington.scientificcalculator; + +public class LogarithmicFunctions { + // calculate the logarithmic function of a given value then display it + public double calculateLogarithmic( double value ) { + double k = Math.log(value); + return Math.round(k* 100.0) /100.0; + } + +// calculate the inverse logarithm of a given value then display it + public double calculateInverseLogarithm( double value ) { + double k = Math.exp(value); + return Math.round(k* 100.0) /100.0; + } + +// calculate the natural logarithm of a given value then display it + public double calculateNaturalLog( double value, double base ) { + double k = Math.log((value) /(base)); + return Math.round(k* 100.0) /100.0; + } + +// calculate the inverse natural logarithm of a given value then display it +// public double calculateNaturalInverseLog( double value ) { +// return Math.exp(value); +// } + + public double factorial(double value){ + //5 ->1*2*3*4*5 + int res = 1; + for (int i = 2; i <= value; i++) { + res = res * i; + } + return res; + } +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java deleted file mode 100644 index 5f42132..0000000 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.zipcodewilmington.scientificcalculator; - -/** - * 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); - } -} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java new file mode 100644 index 0000000..ca7ada2 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java @@ -0,0 +1,138 @@ +package com.zipcodewilmington.scientificcalculator; + +import java.util.Scanner; +public class ScientificCalculator { + + public static double displayNum = 0; + public static void main(String[] args) { + + String calculation; + Scanner scanner = new Scanner(System.in); + CoreCalculations cal = new CoreCalculations(); + Trigonometry trig = new Trigonometry(); + LogarithmicFunctions log = new LogarithmicFunctions(); + SwitchDisplay sd = new SwitchDisplay(); + System.out.println("----------------------------"); + System.out.println("Welcome to my calculator!\n"); + System.out.println("----------------------------"); + + while (true) { + System.out.printf("List of Operations:\n 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division\n 5. Square\n 6. Square Root\n 7. Exponent\n 8. Inverse\n 9. Switch\n " + + "10. sin\n 11. cos\n 12. tan\n 13. inverseSin\n 14. inverseCos\n 15. inverseTan\n " + + "16. log\n 17. log-1\n 18. ln\n 19. factorial\n 20. switchBinary\n 21. switchDecimal\n 22. switchOctal \n 23. HexaDecimal\n 24.clear\n Type {exit} to End Program.\n\nYour current value: %2.1f\n\nPlease Choose an Operation:\n", displayNum); + calculation = scanner.next(); + + if (calculation.equalsIgnoreCase("exit")) { + break; + } + + switch (calculation) { + case "1": + System.out.println("Enter Number: "); + displayNum = cal.addition(displayNum,scanner.nextDouble()); + break; + + case "2": + System.out.println("Enter Number: "); + displayNum = cal.subtraction(displayNum, scanner.nextDouble()); + break; + + case "3": + System.out.println("Enter Number: "); + displayNum = cal.multiplication(displayNum,scanner.nextDouble() ); + break; + + case "4": + System.out.println("Enter Number: "); + displayNum = cal.division(displayNum, scanner.nextDouble()); + break; + + case "5": + System.out.println("Enter Number: "); + displayNum = cal.square(displayNum); + break; + + case "6": + System.out.println("Enter Number: "); + displayNum = cal.squareRoot(displayNum); + break; + + case "7": + System.out.println("Enter Exponent: "); + double exponent = scanner.nextDouble(); + displayNum = cal.exponent(displayNum, exponent); + break; + + case "8": + System.out.println("Enter Number: "); + displayNum = cal.inverse(displayNum); + break; + + case "9": + System.out.println("Enter Number: "); + displayNum = -displayNum; + break; + + case "10" : + System.out.println("Enter Number: "); + displayNum = trig.calculateSine(displayNum); + break; + case "11" : + System.out.println("Enter Number: "); + displayNum = trig.calculateCosine(displayNum); + break; + case "12": + System.out.println("Enter Number: "); + displayNum = trig.calculateTangent(displayNum); + break; + case "13": + System.out.println("Enter Number: "); + displayNum = trig.calculateInverseSine(displayNum); + break; + case "14": + System.out.println("Enter Number: "); + displayNum = trig.calculateInverseCosine(displayNum); + break; + case "15": + System.out.println("Enter Number: "); + displayNum = trig.calculateInverseTangent(displayNum); + break; + case "16": + System.out.println("Enter Number: "); + displayNum = log.calculateLogarithmic(displayNum); + break; + case "17": + System.out.println("Enter Number: "); + displayNum = log.calculateInverseLogarithm(displayNum); + break; + case "18": + System.out.println("Enter value: "); + double base = scanner.nextDouble(); + System.out.println("Enter base: "); + displayNum = log.calculateNaturalLog(displayNum, base); + break; + case "19": + displayNum = log.factorial(displayNum); + break; + case "20": + displayNum = Double.parseDouble(sd.switchBinary(displayNum)); + break; + case "21": + displayNum = Double.parseDouble(sd.switchDecimal(displayNum)); + break; + case "22": + displayNum = Double.parseDouble(sd.switchOctal(displayNum)); + break; + case "23": + displayNum = Double.parseDouble(sd.switchHexadecimal(displayNum)); + break; + case "24": + displayNum = 0; + break; + default : + System.out.println("Invalid Selection.\n\n"); + + } + } + } +} \ No newline at end of file diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/SwitchDisplay.java b/src/main/java/com/zipcodewilmington/scientificcalculator/SwitchDisplay.java new file mode 100644 index 0000000..7778f58 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/SwitchDisplay.java @@ -0,0 +1,28 @@ +package com.zipcodewilmington.scientificcalculator; + +public class SwitchDisplay { + public String switchBinary(double input) { + long bits = Double.doubleToLongBits(input); + return Long.toBinaryString(bits); + + } + + // switch double input to octal + public String switchOctal(double input) { + long bits = Double.doubleToLongBits(input); + return Long.toOctalString(bits); + } + + // switch double input to decimal + public String switchDecimal(double input) { + return Double.toString(input); + } + + // switch double input to hexadecimal + public String switchHexadecimal(double input) { + long bits = Double.doubleToLongBits(input); + return Long.toHexString(bits); + } +} + + diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Trigonometry.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Trigonometry.java new file mode 100644 index 0000000..baf4328 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Trigonometry.java @@ -0,0 +1,40 @@ +package com.zipcodewilmington.scientificcalculator; + +public class Trigonometry { + + // calculate the sine of a given value then display it + public double calculateSine(double value) { + double k = Math.sin(Math.toRadians(value)); + return Math.round(k*100.0)/100.0; + } + +// calculate the cosine of a given value then display it + public double calculateCosine(double value) { + double k = Math.cos(Math.toRadians(value)); + return Math.round(k* 100.0) /100.0; + } + +// calculate the tangent of a given value then display it + public double calculateTangent( double value ) { + double k = Math.tan(Math.toRadians(value)); + return Math.round(k* 100.0) /100.0; + } + +// calculate the inverse sine of a given value then display it + public double calculateInverseSine( double value ) { + double k = Math.asin(value); + return Math.round(k* 100.0) /100.0; + } + +// calculate the inverse cosine of a given value then display it + public double calculateInverseCosine( double value ) { + double k = Math.acos(value); + return Math.round(k* 100.0) /100.0; + } + +// calculate the inverse tangent of a given value then display it + public double calculateInverseTangent ( double value ) { + double k = Math.atan(value); + return Math.round(k* 100.0) /100.0; + } +} diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/CoreCalculationsTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/CoreCalculationsTest.java new file mode 100644 index 0000000..51347c3 --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/CoreCalculationsTest.java @@ -0,0 +1,98 @@ +package com.zipcodewilmington.scientific_calculator; + +import com.zipcodewilmington.scientificcalculator.CoreCalculations; +import org.junit.Test; + +import static junit.framework.TestCase.assertEquals; + + +public class CoreCalculationsTest { + CoreCalculations coreTest = new CoreCalculations(); + + @Test + public void testAddition() { + double a = 1.0; + double b = 2.0; + double expected = 3.0; + double actual = coreTest.addition(a, b); + assertEquals(expected, actual); + } + @Test + public void testSubtraction() { + double a = 5.0; + double b = 2.0; + double expected = 3.0; + double actual = coreTest.subtraction(a, b); + assertEquals(expected, actual); + } + @Test + public void testMulti(){ + double a = 5.0; + double b= 5.0; + double expected = 25.0; + double actual = coreTest.multiplication(a,b); + assertEquals(expected,actual); + } + @Test + public void testDivision(){ + double a = 25.0; + double b= 5.0; + double expected = 5.0; + double actual = coreTest.division(a,b); + assertEquals(expected,actual); + } + @Test + public void testDivisionError(){ + double a = 25.0; + double b= 0.0; + double expected = Double.NaN; + double actual = coreTest.division(a,b); + assertEquals(expected,actual); + } + @Test + public void testSquare(){ + double a = 3.0; + double expected = 9.0; + double actual = coreTest.square(a); + assertEquals(expected,actual); + } + @Test + public void testSquareRoot(){ + double a = 9.0; + double expected = 3.0; + double actual = coreTest.squareRoot(a); + assertEquals(expected,actual); + } + @Test + public void testSquareRootError(){ + double a = -9.0; + double expected = Double.NaN; + double actual = coreTest.squareRoot(a); + assertEquals(expected,actual); + } + @Test + public void testExpo(){ + double a = 5.0; + double power= 2.0; + double expected = 25.0; + double actual = coreTest.exponent(a,power); + assertEquals(expected,actual); + } + @Test + public void testInverse(){ + double a = 2; + double expected = 0.5; + double actual = coreTest.inverse(a); + assertEquals(expected,actual); + } + @Test + public void testInverseError(){ + double a = 0; + double expected = Double.NaN; + double actual = coreTest.inverse(a); + assertEquals(expected,actual); + } + +} + + diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/LogarithmicFunctionsTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/LogarithmicFunctionsTest.java new file mode 100644 index 0000000..95e7b0c --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/LogarithmicFunctionsTest.java @@ -0,0 +1,50 @@ +package com.zipcodewilmington.scientific_calculator; + +import com.zipcodewilmington.scientificcalculator.LogarithmicFunctions; +import org.junit.Test; + +import static junit.framework.TestCase.assertEquals; + +public class LogarithmicFunctionsTest { + LogarithmicFunctions log = new LogarithmicFunctions(); + + @Test + public void testLogarithmic() { + double value = 5.0; + double expected = 1.61; + double actual = log.calculateLogarithmic(value); + assertEquals(expected, actual); + } + @Test + public void testInverseLogarithm() { + double value = 1.0; + double expected = 2.72; + double actual = log.calculateInverseLogarithm(value); + assertEquals(expected, actual); + } + @Test + public void testNaturalLog() { + double value = 4.0; + double base = 2.0; + double expected = 0.69; + double actual = log.calculateNaturalLog(value,base); + assertEquals(expected, actual); + } + + @Test + public void testFactorial() { + double value = 5; + double expected = 120; + double actual = log.factorial(value); + assertEquals(expected, actual); + } + @Test + public void testFactorialZero() { + double value = 0; + double expected = 1; + double actual = log.factorial(value); + assertEquals(expected, actual); + } + +} + diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java deleted file mode 100644 index 94e8d98..0000000 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.zipcodewilmington.scientific_calculator; - -/** - * Created by leon on 2/9/18. - */ -public class TestMainApplication { -} diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TrigonometryTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TrigonometryTest.java new file mode 100644 index 0000000..22caf03 --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TrigonometryTest.java @@ -0,0 +1,56 @@ +package com.zipcodewilmington.scientific_calculator; +import com.zipcodewilmington.scientificcalculator.Trigonometry; +import org.junit.Test; + +import static junit.framework.TestCase.assertEquals; + +public class TrigonometryTest { + Trigonometry trig = new Trigonometry(); + @Test + public void testSine() { + double value = 30; + double expected = 0.5; + double actual = trig.calculateSine(value); + assertEquals(expected, actual); + } + @Test + public void testCosine() { + double value = 60; + double expected = 0.5; + double actual = trig.calculateCosine(value); + assertEquals(expected, actual); + + } + @Test + public void testTangent() { + double value = 45; + double expected = 1.0; + double actual = trig.calculateTangent(value); + assertEquals(expected, actual); + } + @Test + public void testInverseSin() { + double value = 1.0; + double expected = 1.57; + double actual = trig.calculateInverseSine(value); + assertEquals(expected, actual); + } + @Test + public void testInverseSCosine() { + double value = 1.0; + double expected = 0.0; + double actual = trig.calculateInverseCosine(value); + assertEquals(expected, actual); + } + @Test + public void testInverseTangent() { + double value = 1.0; + double expected = 0.79; + double actual = trig.calculateInverseTangent(value); + assertEquals(expected, actual); + } + + + + +}