From 0b3b202c8d80384720efcfbc8568b5ec648509fb Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Sat, 2 Mar 2024 22:48:08 -0500 Subject: [PATCH 01/10] Updated class ScientificCalculator --- .../scientificcalculator/MainApplication.java | 20 ++- .../ScientificCalculator.java | 167 ++++++++++++++++++ 2 files changed, 178 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 5f42132..49caeaa 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -1,17 +1,19 @@ 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); - } + 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..f116c11 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java @@ -0,0 +1,167 @@ +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); + + System.out.println("Welcome to my calculator!\n"); + + 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. 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 += scanner.nextDouble(); + break; + + case "2": + System.out.println("Enter Number: "); + displayNum -= scanner.nextDouble(); + break; + + case "3": + System.out.println("Enter Number: "); + displayNum *= scanner.nextDouble(); + break; + + case "4": + System.out.println("Enter Number: "); + double error = scanner.nextDouble(); + if (error != 0) { + displayNum /= error; + } else { + System.out.println("Err: Cannot divide a number by 0.\n"); + displayNum = Double.NaN; + } + break; + + case "5": + displayNum = Math.pow(displayNum, 2); + break; + + case "6": + + 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; + } + break; + + case "7": + System.out.println("Enter Exponent: "); + double exponent = scanner.nextDouble(); + displayNum = Math.pow(displayNum, exponent); + break; + + case "8": + if (displayNum != 0) { + displayNum = 1 / displayNum; + } else { + System.out.println("Err: user cannot take the inverse of 0."); + displayNum = Double.NaN; + } + break; + + case "9": + displayNum = -displayNum; + break; + + case "10": + displayNum = 0; + break; + + } + } + } +} + + /*double result; + double num1, num2;*/ + + /*case "1": + /*System.out.println("\nYou have chosen addition!\nEnter First Number: "); + num1 = scanner.nextDouble(); + System.out.println("Enter Second Number: "); + num2 = scanner.nextDouble(); + result = num1 + num2; + displayNum = result; + + System.out.printf("%2.0f +%2.0f = %2.0f\n\n", num1, num2, result); + + + case "2": + System.out.println("\nYou have chosen Subtraction!\nEnter First Number: "); + num1 = scanner.nextDouble(); + System.out.println("Enter Second Number: "); + num2 = scanner.nextDouble(); + result = num1 - num2; + displayNum = result; + + System.out.printf("%2.0f - %2.0f = %2.0f\n", num1, num2, result); + + + case "3": + System.out.println("\nYou have chosen Multiplication!\nEnter First Number: "); + num1 = scanner.nextDouble(); + System.out.println("Enter Second Number: "); + num2 = scanner.nextDouble(); + result = num1 * num2; + displayNum = result; + + System.out.printf("%2.0f * %2.0f = %2.0f\n", num1, num2, result); + + + case "4": + System.out.println("\nYou have chosen Division!\nEnter First Number: "); + num1 = scanner.nextDouble(); + System.out.println("Enter Second Number: "); + num2 = scanner.nextDouble(); + if(num2 == 0){ + System.out.println("Err: Cannot divide a number by 0.\n"); + continue; + } + result = num1 / num2; + displayNum = result; + + System.out.printf("%2.0f / %2.0f = %2.0f\n", num1, num2, result); + + + + case "5": + System.out.println("\nYou have chosen Square Root!\nEnter the Base Number: "); + num1 = scanner.nextDouble(); + System.out.println("Enter the Exponent: "); + num2 = scanner.nextDouble(); + result = Math.pow(num1, num2); + displayNum = result; + + System.out.printf("%2.0f^%2.0f = %2.0f\n", num1, num2, result); + + + + case "6": + System.out.println("\nYou have chosen Square Root!\nEnter First Number: "); + num1 = scanner.nextDouble(); + if(num1 < 0){ + System.out.println("Err: user cannot take the square root of a negative number.\n"); + continue; + } + result = Math.sqrt(num1); + displayNum = result; + + System.out.printf("%2.0f = %2.0f\n", num1, result);*/ + From 896c84edb384e86bc76922cbe4999fa2de173714 Mon Sep 17 00:00:00 2001 From: Angelinamathew Date: Sun, 3 Mar 2024 13:23:54 -0500 Subject: [PATCH 02/10] Added Junits for core calculations --- .../CoreCalculations.java | 55 +++++++++++ .../scientificcalculator/MainApplication.java | 17 ---- .../ScientificCalculator.java | 73 ++++++++++++++ .../CoreCalculationsTest.java | 98 +++++++++++++++++++ .../scientific_calculator/ScTest.java | 12 +++ .../TestMainApplication.java | 36 +++++-- 6 files changed, 267 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/CoreCalculations.java delete mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java create mode 100644 src/test/java/com/zipcodewilmington/scientific_calculator/CoreCalculationsTest.java create mode 100644 src/test/java/com/zipcodewilmington/scientific_calculator/ScTest.java 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/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..ec254b5 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java @@ -0,0 +1,73 @@ +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(); + + System.out.println("Welcome to my calculator!\n"); + + 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. 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": + displayNum = cal.square(displayNum); + break; + + case "6": + displayNum = cal.squareRoot(displayNum); + break; + + case "7": + System.out.println("Enter Exponent: "); + double exponent = scanner.nextDouble(); + displayNum = cal.exponent(displayNum, exponent); + break; + + case "8": + displayNum = cal.inverse(displayNum); + break; + + case "9": + displayNum = -displayNum; + break; + + case "10": + displayNum = 0; + break; + + } + } + } +} \ No newline at end of file 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/ScTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/ScTest.java new file mode 100644 index 0000000..b29e8a9 --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/ScTest.java @@ -0,0 +1,12 @@ +package com.zipcodewilmington.scientific_calculator; +import com.zipcodewilmington.scientificcalculator.CoreCalculations; +import org.junit.Test; + +public class ScTest { + CoreCalculations sc = new CoreCalculations(); +// @Test +// public void testSine(){ +// double expected = 0.5; +// double actual = sc.sine(45.00); +// } +} diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java index 94e8d98..cddd2a6 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java @@ -1,7 +1,29 @@ -package com.zipcodewilmington.scientific_calculator; - -/** - * Created by leon on 2/9/18. - */ -public class TestMainApplication { -} +//package com.zipcodewilmington.scientific_calculator; +// +//import com.zipcodewilmington.scientificcalculator.ScientificCalculator; +//import org.junit.Rule; +//import org.junit.Test; +//import org.junit.contrib.java.lang.system.TextFromStandardInputStream; +// +//import static org.junit.Assert.assertEquals; +//import static org.junit.contrib.java.lang.system.TextFromStandardInputStream.emptyStandardInputStream; +// +///** +// * Created by leon on 2/9/18. +// */ +//public class TestMainApplication { +// +// @Rule +// public final TextFromStandardInputStream systemInMock +// = emptyStandardInputStream(); +// +// @Test +// public void summarizesTwoNumbers() { +// systemInMock.provideLines("1", "2"); +// assertEquals(3, Summarize.sumOfNumbersFromSystemIn()); +// } +// +// ScientificCalculator scientificCalculator = new ScientificCalculator(); +// +// +//} From 3cee13fd076e2f5317e19bd0762ae26cb745362a Mon Sep 17 00:00:00 2001 From: Asia Date: Sun, 3 Mar 2024 19:11:12 -0500 Subject: [PATCH 03/10] these are the changes so far --- pom.xml | 63 ++++++++++++++++++- .../LogarithmicFunctions.java | 23 +++++++ .../scientificcalculator/Trigonometry.java | 34 ++++++++++ 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/Trigonometry.java 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/LogarithmicFunctions.java b/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java new file mode 100644 index 0000000..a4dfd41 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java @@ -0,0 +1,23 @@ +package com.zipcodewilmington.scientificcalculator; + +public class LogarithmicFunctions { + // calculate the logarithmic function of a given value then display it + public static double calculateLogarithmic( double value ) { + return Math.log(value); + } + +// calculate the inverse logarithm of a given value then display it + public static double calculateInverseLogarithm( double value ) { + return Math.exp(value); + } + +// calculate the natural logarithm of a given value then display it + public static double calculateNaturalLog( double value, double base ) { + return Math.log(value) / Math.log(base); + } + +// calculate the inverse natural logarithm of a given value then display it + public static double calculateNaturalInverseLog( double value ) { + return Math.exp(value); + } +} 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..42b0cd7 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Trigonometry.java @@ -0,0 +1,34 @@ +package com.zipcodewilmington.scientificcalculator; + +public class Trigonometry { + + // calculate the sine of a given value then display it + public static double calculateSine(double value) { + return (Math.sin(Math.toRadians(value))); + } + +// calculate the cosine of a given value then display it + public static double calculateCosine(double value) { + return Math.toRadians(value); + } + +// calculate the tangent of a given value then display it + public static double calculateTangent( double value ) { + return Math.toRadians(value); + } + +// calculate the inverse sine of a given value then display it + public static double calculateInverseSine( double value ) { + return Math.asin(value); + } + +// calculate the inverse cosine of a given value then display it + public static double calculateInverseCosine( double value ) { + return Math.acos(value); + } + +// calculate the inverse tangent of a given value then display it + public static double calculateInverseTangent ( double value ) { + return Math.toDegrees(Math.atan(value)); + } +} From 3021ab86eade8847ca4209f86896c21fe64cb64c Mon Sep 17 00:00:00 2001 From: Angelinamathew Date: Sun, 3 Mar 2024 22:22:27 -0500 Subject: [PATCH 04/10] added junits for scientific cal --- .../LogarithmicFunctions.java | 21 ++++--- .../ScientificCalculator.java | 50 ++++++++++++++++- .../scientificcalculator/Trigonometry.java | 30 ++++++---- .../LogarithmicFunctionsTest.java | 35 ++++++++++++ .../scientific_calculator/ScTest.java | 12 ---- .../TrigonometryTest.java | 56 +++++++++++++++++++ 6 files changed, 169 insertions(+), 35 deletions(-) create mode 100644 src/test/java/com/zipcodewilmington/scientific_calculator/LogarithmicFunctionsTest.java delete mode 100644 src/test/java/com/zipcodewilmington/scientific_calculator/ScTest.java create mode 100644 src/test/java/com/zipcodewilmington/scientific_calculator/TrigonometryTest.java diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java b/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java index a4dfd41..969dea5 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java @@ -2,22 +2,25 @@ public class LogarithmicFunctions { // calculate the logarithmic function of a given value then display it - public static double calculateLogarithmic( double value ) { - return Math.log(value); + 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 static double calculateInverseLogarithm( double value ) { - return Math.exp(value); + 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 static double calculateNaturalLog( double value, double base ) { - return Math.log(value) / Math.log(base); + 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 static double calculateNaturalInverseLog( double value ) { - return Math.exp(value); - } +// public double calculateNaturalInverseLog( double value ) { +// return Math.exp(value); +// } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java index ec254b5..5461beb 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java @@ -9,11 +9,15 @@ 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(); System.out.println("Welcome to my calculator!\n"); 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. clear\n Type {exit} to End Program.\n\nYour current value: %2.1f\n\nPlease Choose an Operation:\n", displayNum); + 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. 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")) { @@ -42,10 +46,12 @@ public static void main(String[] args) { 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; @@ -56,14 +62,54 @@ public static void main(String[] args) { 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": + 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 = 0; break; diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Trigonometry.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Trigonometry.java index 42b0cd7..baf4328 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Trigonometry.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Trigonometry.java @@ -3,32 +3,38 @@ public class Trigonometry { // calculate the sine of a given value then display it - public static double calculateSine(double value) { - return (Math.sin(Math.toRadians(value))); + 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 static double calculateCosine(double value) { - return Math.toRadians(value); + 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 static double calculateTangent( double value ) { - return Math.toRadians(value); + 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 static double calculateInverseSine( double value ) { - return Math.asin(value); + 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 static double calculateInverseCosine( double value ) { - return Math.acos(value); + 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 static double calculateInverseTangent ( double value ) { - return Math.toDegrees(Math.atan(value)); + 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/LogarithmicFunctionsTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/LogarithmicFunctionsTest.java new file mode 100644 index 0000000..5f203dd --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/LogarithmicFunctionsTest.java @@ -0,0 +1,35 @@ +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); + } + +} + diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/ScTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/ScTest.java deleted file mode 100644 index b29e8a9..0000000 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/ScTest.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.zipcodewilmington.scientific_calculator; -import com.zipcodewilmington.scientificcalculator.CoreCalculations; -import org.junit.Test; - -public class ScTest { - CoreCalculations sc = new CoreCalculations(); -// @Test -// public void testSine(){ -// double expected = 0.5; -// double actual = sc.sine(45.00); -// } -} 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); + } + + + + +} From b8702a759960497b2c55dc7911e53d3a769d3e42 Mon Sep 17 00:00:00 2001 From: Angelinamathew Date: Sun, 3 Mar 2024 22:28:38 -0500 Subject: [PATCH 05/10] removed unwanted files --- .../ScientificCalculator.java | 4 +-- .../TestMainApplication.java | 29 ------------------- 2 files changed, 2 insertions(+), 31 deletions(-) delete mode 100644 src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java index 5461beb..e3959ac 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java @@ -16,8 +16,8 @@ public static void main(String[] args) { 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. clear\n Type {exit} to End Program.\n\nYour current value: %2.1f\n\nPlease Choose an Operation:\n", displayNum); + "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.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")) { 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 cddd2a6..0000000 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java +++ /dev/null @@ -1,29 +0,0 @@ -//package com.zipcodewilmington.scientific_calculator; -// -//import com.zipcodewilmington.scientificcalculator.ScientificCalculator; -//import org.junit.Rule; -//import org.junit.Test; -//import org.junit.contrib.java.lang.system.TextFromStandardInputStream; -// -//import static org.junit.Assert.assertEquals; -//import static org.junit.contrib.java.lang.system.TextFromStandardInputStream.emptyStandardInputStream; -// -///** -// * Created by leon on 2/9/18. -// */ -//public class TestMainApplication { -// -// @Rule -// public final TextFromStandardInputStream systemInMock -// = emptyStandardInputStream(); -// -// @Test -// public void summarizesTwoNumbers() { -// systemInMock.provideLines("1", "2"); -// assertEquals(3, Summarize.sumOfNumbersFromSystemIn()); -// } -// -// ScientificCalculator scientificCalculator = new ScientificCalculator(); -// -// -//} From 317f306af6c2e92c83b61a30a4c0ed4f55edb3bc Mon Sep 17 00:00:00 2001 From: Angelinamathew Date: Sun, 3 Mar 2024 22:37:41 -0500 Subject: [PATCH 06/10] added factorial --- .../LogarithmicFunctions.java | 8 ++++++++ .../ScientificCalculator.java | 2 +- .../LogarithmicFunctionsTest.java | 17 ++++++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java b/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java index 969dea5..2173cf3 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java @@ -23,4 +23,12 @@ public double calculateNaturalLog( double value, double base ) { // 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/ScientificCalculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java index e3959ac..5bc66b6 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java @@ -17,7 +17,7 @@ public static void main(String[] args) { 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.clear\n Type {exit} to End Program.\n\nYour current value: %2.1f\n\nPlease Choose an Operation:\n", displayNum); + "16. log\n 17. log-1\n 18. ln\n 19. 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")) { diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/LogarithmicFunctionsTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/LogarithmicFunctionsTest.java index 5f203dd..95e7b0c 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/LogarithmicFunctionsTest.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/LogarithmicFunctionsTest.java @@ -30,6 +30,21 @@ public void testNaturalLog() { 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); + } + } From bcb5eeb05a16e3850bec81243efce2ffbb5fda7c Mon Sep 17 00:00:00 2001 From: Angelinamathew Date: Sun, 3 Mar 2024 22:56:52 -0500 Subject: [PATCH 07/10] added default --- .../scientificcalculator/LogarithmicFunctions.java | 3 ++- .../scientificcalculator/ScientificCalculator.java | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java b/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java index 2173cf3..73c49ee 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/LogarithmicFunctions.java @@ -27,8 +27,9 @@ public double calculateNaturalLog( double value, double base ) { public double factorial(double value){ //5 ->1*2*3*4*5 int res = 1; - for (int i = 2; i <= value; i++) + for (int i = 2; i <= value; i++) { res = res * i; + } return res; } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java index 5bc66b6..b732b43 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java @@ -112,6 +112,8 @@ public static void main(String[] args) { case "19": displayNum = 0; break; + default : + System.out.println("Invalid Selection.\n\n"); } } From 596be400ab7914b890ba2de6109e4a6f906e7327 Mon Sep 17 00:00:00 2001 From: Angelinamathew Date: Sun, 3 Mar 2024 23:15:06 -0500 Subject: [PATCH 08/10] removed unwanted files --- .../zipcodewilmington/scientificcalculator/MainApplication.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java 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 e69de29..0000000 From 3cb47a629620ec741338bcb7cb747d38e788f2f6 Mon Sep 17 00:00:00 2001 From: Angelinamathew Date: Mon, 4 Mar 2024 08:39:39 -0500 Subject: [PATCH 09/10] Update --- .../scientificcalculator/ScientificCalculator.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java index b732b43..0f18211 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java @@ -11,13 +11,14 @@ public static void main(String[] args) { CoreCalculations cal = new CoreCalculations(); Trigonometry trig = new Trigonometry(); LogarithmicFunctions log = new LogarithmicFunctions(); - + 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. clear\n Type {exit} to End Program.\n\nYour current value: %2.1f\n\nPlease Choose an Operation:\n", displayNum); + "16. log\n 17. log-1\n 18. ln\n 19. factorial\n 20. 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")) { @@ -110,6 +111,9 @@ public static void main(String[] args) { displayNum = log.calculateNaturalLog(displayNum, base); break; case "19": + displayNum = log.factorial(displayNum); + break; + case "20": displayNum = 0; break; default : From 285c57f902bd83b5408ef3f4fe79596545ac8016 Mon Sep 17 00:00:00 2001 From: Angelinamathew Date: Mon, 4 Mar 2024 13:35:47 -0500 Subject: [PATCH 10/10] Finished --- .../ScientificCalculator.java | 15 +++++++++- .../scientificcalculator/SwitchDisplay.java | 28 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/SwitchDisplay.java diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java index 0f18211..ca7ada2 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java @@ -11,6 +11,7 @@ public static void main(String[] args) { 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("----------------------------"); @@ -18,7 +19,7 @@ public static void main(String[] args) { 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. clear\n Type {exit} to End Program.\n\nYour current value: %2.1f\n\nPlease Choose an Operation:\n", displayNum); + "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")) { @@ -114,6 +115,18 @@ public static void main(String[] args) { 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 : 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); + } +} + +