From 76f149ebc537b120b3166b53795f3c9a9dde222c Mon Sep 17 00:00:00 2001 From: oahmed105 Date: Fri, 30 Jun 2023 18:30:13 -0400 Subject: [PATCH 1/9] Test --- .../com/zipcodewilmington/scientificcalculator/Science.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/Science.java diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Science.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Science.java new file mode 100644 index 0000000..6ef8e8c --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Science.java @@ -0,0 +1,5 @@ +package com.zipcodewilmington.scientificcalculator; + +public class Science { + System.out.println("test"); +} From 174604e8082e81bafa1a58e82086dc56aa81492a Mon Sep 17 00:00:00 2001 From: Manny Date: Sat, 1 Jul 2023 23:59:40 -0400 Subject: [PATCH 2/9] added test cases for scientific and core test, still need to add multiple of each tests --- .../scientific_calculator/CoreTest.java | 216 ++++++++++++++++++ .../scientific_calculator/ScientificTest.java | 216 ++++++++++++++++++ .../TestMainApplication.java | 24 +- 3 files changed, 453 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/zipcodewilmington/scientific_calculator/CoreTest.java create mode 100644 src/test/java/com/zipcodewilmington/scientific_calculator/ScientificTest.java diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/CoreTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/CoreTest.java new file mode 100644 index 0000000..a6a98f0 --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/CoreTest.java @@ -0,0 +1,216 @@ +package com.zipcodewilmington.scientific_calculator; + +import com.zipcodewilmington.scientificcalculator.MainApplication; +import org.junit.Assert; +import org.junit.Test; +import Ma + +public class CoreTest { + @Test + public void add(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedSum = x + y; + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedSum, actual); + } + + @Test + public void subtract(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedDifference = x - y; + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedDifference, actual); + } + + @Test + public void divide(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedQuotient = x / y; + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedQuotient, actual); + } + + @Test + public void multiply(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedProduct = x * y; + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedProduct, actual); + } + + @Test + public void square(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + double x; + + double expectedSquare = Math.pow(x); + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedSquare, actual); + } + + @Test + public void squareRoot(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedSquareRoot = Math.sqrt(); + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedSquareRoot, actual); + } + + @Test + public void exponentiation(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedExponentiation = Math.pow(x, y); + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedExponentiation, actual); + } + + @Test + public void inverseOfNumber(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedSum = x + y; + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedSum, actual); + } + + @Test + public void invertSignOfNumber(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedInverseSignOfNumber = Math.abs(); + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedSum, actual); + } + + @Test + public void err(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedSum = x + y; + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedSum, actual); + } + + @Test + public void shi(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedSum = x + y; + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedSum, actual); + } +} 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..6f945dc --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/ScientificTest.java @@ -0,0 +1,216 @@ +package com.zipcodewilmington.scientific_calculator; + +import com.zipcodewilmington.scientificcalculator.MainApplication; +import org.junit.Assert; +import org.junit.Test; + +public class ScientificTest { + @Test + public void sine(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedSine = Math.sin(); + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedSine, actual); + } + + @Test + public void cosine(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedCosine = Math.cos(); + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedCosine, actual); + } + + @Test + public void tangent(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedtan = Math.tan(); + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedtan, actual); + } + + @Test + public void inverseSine(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedInverseSine = Math.asin(); + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedInverseSine, actual); + } + + @Test + public void inverseCosine(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedInverseCosine = Math.acos(); + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedInverseCosine, actual); + } + + @Test + public void inverseTangent(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedInverseTangent = Math.atan(); + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedInverseTangent, actual); + } + + @Test + public void log(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedLog = Math.log(); + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedLog, actual); + } + + @Test + public void inverseLog(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedInverseLog = x + y; + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedSum, actual); + } + + @Test + public void naturalLog(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedSum = x + y; + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedSum, actual); + } + + @Test + public void inverseNaturalLog(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedInverseNaturalLog = Math.exp(); + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedInverseNaturalLog, actual); + } + + @Test + public void factorial(){ + + //Given, this is the expected behavior + //change variables to names of group's methods + int x = 2; + int y = 4; + + int expectedSum = x + y; + + //When + //calling groups class and method to store the value of the actual method + // + int actual = MainApplication.add(4,2); + + //Then + Assert.assertEquals(expectedSum, actual); + } +} diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java index 94e8d98..f6988c6 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java @@ -1,7 +1,25 @@ package com.zipcodewilmington.scientific_calculator; -/** - * Created by leon on 2/9/18. - */ +import java.util.logging.Level; +import java.util.logging.Logger; + +import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; + +import com.zipcodewilmington.scientificcalculator.MainApplication; +import org.junit.Assert; +import org.junit.Test; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) + +@Suite.SuiteClasses({ + CoreTest.class, + ScientificTest.class, +}) public class TestMainApplication { + + } From 1c596c53b292d92a351278001cc17eb758d15104 Mon Sep 17 00:00:00 2001 From: Chanelle Date: Sun, 2 Jul 2023 10:57:58 -0400 Subject: [PATCH 3/9] Core Features changes --- .../scientificcalculator/Calculator.java | 97 +++++++++++++++++++ .../scientificcalculator/CoreFeatures.java | 78 +++++++++++++++ .../scientificcalculator/MainApplication.java | 26 +++-- 3 files changed, 194 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/Calculator.java create mode 100644 src/main/java/com/zipcodewilmington/scientificcalculator/CoreFeatures.java 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..82b9940 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator.java @@ -0,0 +1,97 @@ +package com.zipcodewilmington.scientificcalculator; +import java.lang.Math; +public class Calculator { + + private double display; + + public Calculator() { + this.display = 0; + } + + public double getDisplay() { + return this.display; + } + + public void clearDisplay() { + this.display = 0; + } + + public void setDisplay(double number) { + this.display = number; + } + + public void add(double number) { + this.display += number; + } + + public void subtract(double number) { + this.display -= number; + } + + public void multiply(double number) { + this.display *= number; + } + + public void divide(double number) { + if (number != 0) { + this.display /= number; + } else { + this.display = Double.NaN; // Set display to NaN for division by zero error + } + } + + public void square() { + this.display = Math.pow(this.display, 2); + } + + public void squareRoot() { + this.display = Math.sqrt(this.display); + } + + public void exponentiation(double number) { + this.display = Math.pow(this.display, number); + } + + public void inverse() { + if (this.display != 0) { + this.display = 1 / this.display; + } else { + this.display = Double.NaN; // Set display to NaN for division by zero error + } + } + + public void invertSign() { + this.display = -this.display; + } + + public void clearError() { + if (Double.isNaN(this.display)) { + this.display = 0; + } + } + + public static void main(String[] args) { + Calculator calculator = new Calculator(); + System.out.println(calculator.getDisplay()); // Output: 0.0 + + calculator.setDisplay(5); + System.out.println(calculator.getDisplay()); // Output: 5.0 + + calculator.add(3); + System.out.println(calculator.getDisplay()); // Output: 8.0 + + calculator.square(); + System.out.println(calculator.getDisplay()); // Output: 64.0 + + calculator.squareRoot(); + System.out.println(calculator.getDisplay()); // Output: 8.0 + + calculator.divide(0); + System.out.println(calculator.getDisplay()); // Output: NaN + + calculator.clearError(); + System.out.println(calculator.getDisplay()); // Output: 0.0 + } + } + + diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/CoreFeatures.java b/src/main/java/com/zipcodewilmington/scientificcalculator/CoreFeatures.java new file mode 100644 index 0000000..bce0e02 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/CoreFeatures.java @@ -0,0 +1,78 @@ +package com.zipcodewilmington.scientificcalculator; + +import java.util.Scanner; + +public class CoreFeatures { + public static void main(String[] args) { + Console.println("Welcome to Halictidae Calculator!"); + Console.println("I'm here to help!"); + +// String s = Console.getStringInput("Enter a string"); +// Integer i = Console.getIntegerInput("Enter an integer"); +// Double d = Console.getDoubleInput("Enter a double."); +// String methodName = Console.getStringInput("Enter a function"); +// String firstNumber = Console.getStringInput("Enter the first number"); +// +// 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); + + + int numberOne; + int numberTwo; + int result = 0; + //initialized variable result + Scanner scanObject = new Scanner(System.in); + + + Scanner scan = new Scanner(System.in); + System.out.println("Enter first number:"); + //enter first number + numberOne = (int) scan.nextDouble(); + System.out.println("What operation?"); + System.out.println("( + ) ( - ) ( * ) ( / ) ( ^ ) ( sqr ) ( sqrt )"); + //choose specified operation of choice + String operator = scanObject.next(); + System.out.println("Enter second number"); + //enter second number + numberTwo = scan.nextInt(); + + + + System.out.println(result); + //results printed + + switch (operator) { + case "+": + result = numberOne + numberTwo; + break; + case "-": + result = numberOne - numberTwo; + break; + case "*": + result = numberOne * numberTwo; + break; + case "/": + result = numberOne / numberTwo; + break; + case "^": + result = (int) Math.pow(numberOne, numberTwo); + break; + case "sqrt": + result = (int) Math.sqrt(numberOne); + break; + //switch statement - block of code executed when switch value matches operation chosen + } + System.out.println(numberOne + " " + operator + " " + numberTwo + " = " + result); + // Example: 10 + 5 = 15 (different operations can be chosen) + } +} + + + + + + + + + diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 5f42132..74d49fa 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -5,13 +5,25 @@ */ 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); } + +// +// public int add(int x, int y){ +// return x + y; +// } +// +// public int subtract(int x, int y){ +// return x - y; +// } +// +// public int multiply(int x, int y){ +// return x * y; +// } +// +// public int divide(int x, int y){ +// return x / y; +// } } + + From 4aef3cdedbf260b22aa9cf992a377fd0ed26cd17 Mon Sep 17 00:00:00 2001 From: Manny Date: Sun, 2 Jul 2023 11:27:18 -0400 Subject: [PATCH 4/9] added changes to main and console --- .../scientificcalculator/Console.java | 10 ++++++++-- .../scientificcalculator/MainApplication.java | 7 +++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java index 83f0e97..d3c6550 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java @@ -23,10 +23,16 @@ public static String getStringInput(String prompt) { } public static Integer getIntegerInput(String prompt) { - return null; + Scanner scanner = new Scanner(System.in); + println(prompt); + int userInput = scanner.nextInt(); + return userInput; } public static Double getDoubleInput(String prompt) { - return null; + Scanner scanner = new Scanner(System.in); + 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..ec48920 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -14,4 +14,11 @@ public static void main(String[] args) { Console.println("The user input %s as a integer", i); Console.println("The user input %s as a d", d); } + /* + public static int add(int x, int y){ + int result = x + y; + return result; + } + + */ } From 1c0e2e78b62fec6613203635294f798e8f286209 Mon Sep 17 00:00:00 2001 From: oahmed105 Date: Sun, 2 Jul 2023 11:31:40 -0400 Subject: [PATCH 5/9] What I have so far --- .../scientificcalculator/Console.java | 10 +- .../scientificcalculator/Science.java | 115 +++++++++++++++++- 2 files changed, 122 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java index 83f0e97..d3c6550 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java @@ -23,10 +23,16 @@ public static String getStringInput(String prompt) { } public static Integer getIntegerInput(String prompt) { - return null; + Scanner scanner = new Scanner(System.in); + println(prompt); + int userInput = scanner.nextInt(); + return userInput; } public static Double getDoubleInput(String prompt) { - return null; + Scanner scanner = new Scanner(System.in); + println(prompt); + double userInput = scanner.nextDouble(); + return userInput; } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Science.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Science.java index 6ef8e8c..a0cf0fc 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Science.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Science.java @@ -1,5 +1,118 @@ package com.zipcodewilmington.scientificcalculator; +import java.util.Scanner; + public class Science { - System.out.println("test"); + public static void Binary () { + Scanner binaryInput = new Scanner (System.in); + + } + + + + public static double SinDegree (double input) { + + double sinValue = Math.sin(input); + System.out.println("sin(" + input + ") = " + sinValue); + return sinValue; + } + + public static double CosDegree(double input) { + double cosValue = Math.cos(input); + System.out.println("cos(" + input + ") = " + cosValue); + return cosValue; + + } + + public static double TanDegree (double input) { + double tanValue = Math.tan(input); + System.out.println("tan(" + input + ") = " + tanValue); + return tanValue; + + } + + public static double SinRadian (double input) { + double sinValue = Math.sin(input); + System.out.println("sin(" + input + ") = " + sinValue); + return sinValue; + } + + public static double CosRadian (double input) { + double cosValue = Math.sin(input); + System.out.println("sin(" + input + ") = " + cosValue); + return cosValue; + } + + public static double TanRadian (double input) { + double tanValue = Math.sin(input); + System.out.println("sin(" + input + ") = " + tanValue); + return tanValue; + } + + public static double asinDegree (double input) { + double asinvalue = Math.asin(input); + return asinvalue; + } + public static double acosDegree (double input) { + double acosvalue = Math.acos(input); + return acosvalue; + } + + public static double atanDegree (double input) { + double atanvalue = Math.atan(input); + return atanvalue; + } + + public static double asinRadian (double input) { + double asinvalue = Math.asin(input); + return asinvalue; + } + + public static double acosRadian (double input) { + double asinvalue = Math.acos(input); + return asinvalue; + } + + public static double atanRadian (double input) { + double asinvalue = Math.atan(input); + return asinvalue; + } + + public static double log (double log) { + double logValue = Math.log(log); + return logValue; + } + + public static double invLog (double invLog) { + double invLogValue = Math.log10(invLog); + return invLogValue; + } + + public static double natLog (double natLog) { + double natLogValue = Math.log1p(natLog); + return natLogValue; + } + + public static double invNatLog (double invNatLog) { + double invNatLogValue = Math.log10(Math.log1p(invNatLog)); + return invNatLogValue; + } + + public static void switchUnitsMode () { + String s = Console.getStringInput("Please enter Degrees or Radians: "); + if (s.equals("Degrees")) { + swithUnitsMode(s); + } + else { + + } + + } + + public static String swithUnitsMode(String mode) { + return null; + } + + + } From aaa7124bf379802ac6b4321ef12b83b0881e6873 Mon Sep 17 00:00:00 2001 From: oahmed105 Date: Sun, 2 Jul 2023 11:38:24 -0400 Subject: [PATCH 6/9] Anotha one --- .../zipcodewilmington/scientificcalculator/Science.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Science.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Science.java index a0cf0fc..e28afb3 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Science.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Science.java @@ -98,6 +98,14 @@ public static double invNatLog (double invNatLog) { return invNatLogValue; } + public static int fact (int fact) { + for (int i = 1;i <= fact; i++) { + fact = fact * i; + } + int factValue = fact; + return factValue; + } + public static void switchUnitsMode () { String s = Console.getStringInput("Please enter Degrees or Radians: "); if (s.equals("Degrees")) { From a5df51816157f0fe66100af63de93099e09bdac0 Mon Sep 17 00:00:00 2001 From: Manny Date: Sun, 2 Jul 2023 16:49:17 -0400 Subject: [PATCH 7/9] added changes to calculator and science class implmented everything in mainapplicaiton class --- .../scientificcalculator/Calculator.java | 91 ++++++++--------- .../scientificcalculator/Console.java | 4 +- .../scientificcalculator/CoreFeatures.java | 32 +++--- .../scientificcalculator/MainApplication.java | 98 +++++++++++++++++++ .../scientificcalculator/Science.java | 75 ++++++-------- .../scientific_calculator/CoreTest.java | 9 +- 6 files changed, 201 insertions(+), 108 deletions(-) diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator.java index 82b9940..3e21fc8 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator.java @@ -20,48 +20,74 @@ public void setDisplay(double number) { this.display = number; } - public void add(double number) { - this.display += number; + public static double addTwoNums(double x, double y) { + double sum = x + y; + Console.print("The sum of " + x + " and " + y + " is " + String.valueOf(sum)); + return sum; } - public void subtract(double number) { - this.display -= number; + public static double subtractTwoNums(double x, double y) { + double difference = x - y; + Console.print("The difference of " + x + " and " + y + " is " + String.valueOf(difference)); + return difference; } - public void multiply(double number) { - this.display *= number; + public static double multiplyTwoNums(double x, double y) { + double product = x * y; + Console.print("The product of " + x + " and " + y + " is " + String.valueOf(product)); + return product; } - public void divide(double number) { - if (number != 0) { - this.display /= number; - } else { - this.display = Double.NaN; // Set display to NaN for division by zero error + public static double divide(double x, double y) { + double quotient = 0; + if (y != 0) { + quotient = x /y; + } else { + //this.display = Double.NaN; // Set display to NaN for division by zero error } + Console.print("The quotient of " + x + " / " + y + " is " + String.valueOf(quotient)); + return quotient; } - public void square() { - this.display = Math.pow(this.display, 2); + public static double square(double x) { + double squared = (x * x); + Console.print(x + " :Squared is " + String.valueOf(squared)); + return squared; } - public void squareRoot() { - this.display = Math.sqrt(this.display); + public static double squareRoot(double x) { + double squareRoot = Math.sqrt(x); + Console.print( "The square root of: " + x + " is " + String.valueOf(squareRoot)); + return squareRoot; } - public void exponentiation(double number) { - this.display = Math.pow(this.display, number); + public static double exponentiation(double x, double y) { + double total = Math.pow(x , y); + + Console.print(x + " raised to the power of " + y + " is " + total ); + return total; } public void inverse() { if (this.display != 0) { this.display = 1 / this.display; } else { - this.display = Double.NaN; // Set display to NaN for division by zero error + //this.display = Double.NaN; // Set display to NaN for division by zero error + } } - public void invertSign() { - this.display = -this.display; + public static double invertSign(double x) { + double newNum = 0; + if (x < 0 ){ + //x is less than in this scenario + newNum = (x * -1); + } else { + newNum = -x; + + } + Console.print("This number with it's sign inverted is: " + newNum); + return newNum; } public void clearError() { @@ -69,29 +95,6 @@ public void clearError() { this.display = 0; } } - - public static void main(String[] args) { - Calculator calculator = new Calculator(); - System.out.println(calculator.getDisplay()); // Output: 0.0 - - calculator.setDisplay(5); - System.out.println(calculator.getDisplay()); // Output: 5.0 - - calculator.add(3); - System.out.println(calculator.getDisplay()); // Output: 8.0 - - calculator.square(); - System.out.println(calculator.getDisplay()); // Output: 64.0 - - calculator.squareRoot(); - System.out.println(calculator.getDisplay()); // Output: 8.0 - - calculator.divide(0); - System.out.println(calculator.getDisplay()); // Output: NaN - - calculator.clearError(); - System.out.println(calculator.getDisplay()); // Output: 0.0 - } - } +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java index d3c6550..3c13ccc 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java @@ -24,14 +24,14 @@ public static String getStringInput(String prompt) { public static Integer getIntegerInput(String prompt) { Scanner scanner = new Scanner(System.in); - println(prompt); + print(prompt); int userInput = scanner.nextInt(); return userInput; } public static Double getDoubleInput(String prompt) { Scanner scanner = new Scanner(System.in); - println(prompt); + print(prompt); double userInput = scanner.nextDouble(); return userInput; } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/CoreFeatures.java b/src/main/java/com/zipcodewilmington/scientificcalculator/CoreFeatures.java index bce0e02..25fb561 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/CoreFeatures.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/CoreFeatures.java @@ -4,6 +4,7 @@ public class CoreFeatures { public static void main(String[] args) { + /* Console.println("Welcome to Halictidae Calculator!"); Console.println("I'm here to help!"); @@ -17,14 +18,20 @@ public static void main(String[] args) { // Console.println("The user input %s as a integer", i); // Console.println("The user input %s as a d", d); + int result = 1; + /* + Console.println("Welcome to Halictidae Calculator!"); + Console.println("I'm here to help!"); + int numberOne = Console.getIntegerInput("Enter the first number: "); + int numberTwo = Console.getIntegerInput("Enter the Second number: "); + String chooseOperation = Console.getStringInput("What operation?\n" + + "( + ) ( - ) ( * ) ( / ) ( ^ ) ( sqr ) ( sqrt )\n");*/ - int numberOne; - int numberTwo; - int result = 0; //initialized variable result - Scanner scanObject = new Scanner(System.in); + //Scanner scanObject = new Scanner(System.in); + /* Scanner scan = new Scanner(System.in); System.out.println("Enter first number:"); //enter first number @@ -35,16 +42,16 @@ public static void main(String[] args) { String operator = scanObject.next(); System.out.println("Enter second number"); //enter second number - numberTwo = scan.nextInt(); + numberTwo = scan.nextInt(); */ - - System.out.println(result); + //System.out.println(result); //results printed - switch (operator) { + /* + switch (chooseOperation) { case "+": - result = numberOne + numberTwo; + result = number one + number two; break; case "-": result = numberOne - numberTwo; @@ -61,13 +68,16 @@ public static void main(String[] args) { case "sqrt": result = (int) Math.sqrt(numberOne); break; + //switch statement - block of code executed when switch value matches operation chosen } - System.out.println(numberOne + " " + operator + " " + numberTwo + " = " + result); + //System.out.println(numberOne + " " + chooseOperation + " " + numberTwo + " = " + result); // Example: 10 + 5 = 15 (different operations can be chosen) } -} + */ + } +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index ed633d5..e3119d8 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -5,8 +5,106 @@ */ public class MainApplication { public static void main(String[] args) { + Console.println("Welcome to Halictidae Calculator!"); + Console.println("I'm here to help!"); + String chooseOperation = Console.getStringInput("What operation would you like to perform today?\n" + + "( + ) ( - ) ( * ) ( / ) ( ^ ) ( sqr ) ( sqrt ) (exponentiation) (invert sign)\n" + + "(sine) (cosine) (tangent) (inverseSine) (inverseCosine) (inverseTangent) (log) (inverseLog)" + + " (naturalLog) (inverseNaturalLog) (factorial)\n" + + "(clearError)" + + "\nPlease type it in as you see in the parentheses ---> "); + double numberOne; + double numberTwo; + + switch (chooseOperation) { + case "+": + numberOne = Console.getDoubleInput("Please enter first number: "); + numberTwo = Console.getDoubleInput("Please enter second number: "); + Calculator.addTwoNums(numberOne, numberTwo); + break; + case "-": + numberOne = Console.getDoubleInput("Please enter first number: "); + numberTwo = Console.getDoubleInput("Please enter second number: "); + Calculator.subtractTwoNums(numberOne, numberTwo); + break; + case "*": + numberOne = Console.getDoubleInput("Please enter first number: "); + numberTwo = Console.getDoubleInput("Please enter second number: "); + Calculator.multiplyTwoNums(numberOne, numberTwo); + break; + case "/": + numberOne = Console.getDoubleInput("Please enter first number: "); + numberTwo = Console.getDoubleInput("Please enter second number: "); + Calculator.divide(numberOne, numberTwo); + break; + case "exponentiation": + numberOne = Console.getDoubleInput("Please enter first number: "); + numberTwo = Console.getDoubleInput("Please enter second number: "); + Calculator.exponentiation(numberOne, numberTwo); + break; + case "sqr": + numberOne = Console.getDoubleInput("Please enter the number you want squared: "); + Calculator.square(numberOne); + break; + case "sqrt": + numberOne = Console.getDoubleInput("Please enter the number you want the square root of: "); + Calculator.squareRoot(numberOne); + break; + case "invert sign": + numberOne = Console.getDoubleInput("Please enter the number which sign you want to inverse: "); + Calculator.invertSign(numberOne); + break; + case "sine": + numberOne = Console.getDoubleInput("Please enter the number you want to Sine of: "); + Science.Sin(numberOne); + break; + case "cosine": + numberOne = Console.getDoubleInput("Please enter the number you want to Cosine of: "); + Science.Cos(numberOne); + break; + case "tangent": + numberOne = Console.getDoubleInput("Please enter the number you want to Tangent of: "); + Science.Tan(numberOne); + break; + case "inverseSine": + numberOne = Console.getDoubleInput("Please enter the number you want to Inverse Sine of: "); + Science.aSin(numberOne); + break; + case "inverseCosine": + numberOne = Console.getDoubleInput("Please enter the number you want to Inverse Cosine of: "); + Science.aCos(numberOne); + break; + case "inverseTangent": + numberOne = Console.getDoubleInput("Please enter the number you want to Inverse Tangent of: "); + Science.aTan(numberOne); + break; + case "log": + numberOne = Console.getDoubleInput("Please enter the number you want the log of: "); + Science.log(numberOne); + break; + case "inverseLog": + numberOne = Console.getDoubleInput("Please enter the number you want the inverse log of: "); + Science.invLog(numberOne); + break; + case "naturalLog": + numberOne = Console.getDoubleInput("Please enter the number you want the natural log of: "); + Science.natLog(numberOne); + break; + case "inverseNaturalLog": + numberOne = Console.getDoubleInput("Please enter the number you want the inverse natural log of: "); + Science.invNatLog(numberOne); + break; + case "factorial": + numberOne = Console.getDoubleInput("Please enter the number you want the factorial of: "); + Science.factorial(numberOne); + break; + default: + Console.print("That command does not exist please enter another command: "); + break; + } } } + diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Science.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Science.java index a0cf0fc..6502ea3 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Science.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Science.java @@ -8,96 +8,77 @@ public static void Binary () { } - - - public static double SinDegree (double input) { - + public static double Sin(double input) { double sinValue = Math.sin(input); - System.out.println("sin(" + input + ") = " + sinValue); + Console.println("sin(" + input + ") = " + sinValue); return sinValue; } - public static double CosDegree(double input) { + public static double Cos(double input) { double cosValue = Math.cos(input); - System.out.println("cos(" + input + ") = " + cosValue); + Console.println("sin(" + input + ") = " + cosValue); return cosValue; - } - public static double TanDegree (double input) { + public static double Tan(double input) { double tanValue = Math.tan(input); - System.out.println("tan(" + input + ") = " + tanValue); - return tanValue; - - } - - public static double SinRadian (double input) { - double sinValue = Math.sin(input); - System.out.println("sin(" + input + ") = " + sinValue); - return sinValue; - } - - public static double CosRadian (double input) { - double cosValue = Math.sin(input); - System.out.println("sin(" + input + ") = " + cosValue); - return cosValue; - } - - public static double TanRadian (double input) { - double tanValue = Math.sin(input); - System.out.println("sin(" + input + ") = " + tanValue); + Console.println("sin(" + input + ") = " + tanValue); return tanValue; } - public static double asinDegree (double input) { + public static double aSin(double input) { double asinvalue = Math.asin(input); + Console.println("sin(" + input + ") = " + asinvalue); return asinvalue; } - public static double acosDegree (double input) { + public static double aCos(double input) { double acosvalue = Math.acos(input); + Console.println("sin(" + input + ") = " + acosvalue); return acosvalue; } - public static double atanDegree (double input) { + public static double aTan(double input) { double atanvalue = Math.atan(input); + Console.println("sin(" + input + ") = " + atanvalue); return atanvalue; } - public static double asinRadian (double input) { - double asinvalue = Math.asin(input); - return asinvalue; - } - - public static double acosRadian (double input) { - double asinvalue = Math.acos(input); - return asinvalue; - } - - public static double atanRadian (double input) { - double asinvalue = Math.atan(input); - return asinvalue; - } - public static double log (double log) { double logValue = Math.log(log); + Console.println("log(" + log + ") = " + logValue); return logValue; } public static double invLog (double invLog) { double invLogValue = Math.log10(invLog); + Console.println("sin(" + invLog + ") = " + invLogValue); return invLogValue; } public static double natLog (double natLog) { double natLogValue = Math.log1p(natLog); + Console.println("sin(" + natLog + ") = " + natLogValue); return natLogValue; } public static double invNatLog (double invNatLog) { double invNatLogValue = Math.log10(Math.log1p(invNatLog)); + Console.println("sin(" + invNatLog + ") = " + invNatLogValue); return invNatLogValue; } + public static double factorial(double fact) { + double total = 1; + for (int i = 0; 0 < fact; i++){ + + // add N to the total sum of the number + total *= fact;//3 + fact -= 1; + //have total increment with i till its less than n + } + Console.println("The factorial is: " + total); + return total; + } public static void switchUnitsMode () { String s = Console.getStringInput("Please enter Degrees or Radians: "); if (s.equals("Degrees")) { diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/CoreTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/CoreTest.java index a6a98f0..81eb0ec 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/CoreTest.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/CoreTest.java @@ -1,5 +1,6 @@ package com.zipcodewilmington.scientific_calculator; +import com.zipcodewilmington.scientificcalculator.Calculator; import com.zipcodewilmington.scientificcalculator.MainApplication; import org.junit.Assert; import org.junit.Test; @@ -11,15 +12,15 @@ public void add(){ //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; + double x = 2; + double y = 4; - int expectedSum = x + y; + double expectedSum = x + y; //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Calculator.addTwoNums(4,2); //Then Assert.assertEquals(expectedSum, actual); From b979e38e9e86392828b5756e9dfc7b877357b78e Mon Sep 17 00:00:00 2001 From: Manny Date: Sun, 2 Jul 2023 21:00:41 -0400 Subject: [PATCH 8/9] added test cases for Core and scientific cases --- .../scientific_calculator/CoreTest.java | 386 ++++++++++++++---- .../scientific_calculator/ScientificTest.java | 166 ++++---- 2 files changed, 391 insertions(+), 161 deletions(-) diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/CoreTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/CoreTest.java index 81eb0ec..b6979b9 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/CoreTest.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/CoreTest.java @@ -2,216 +2,458 @@ import com.zipcodewilmington.scientificcalculator.Calculator; import com.zipcodewilmington.scientificcalculator.MainApplication; +import org.apache.commons.io.output.ByteArrayOutputStream; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; -import Ma + +import java.io.PrintStream; + public class CoreTest { - @Test - public void add(){ + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + + @Before + public void setUpStreams() { + // changes "System.out.print" commands to print to this stream, rather than the console + System.setOut(new PrintStream(outContent)); + } + @After + public void restoreStreams() { + // we do this to avoid having any null pointer exceptions + System.setOut(originalOut); + } + + + @Test + public void addTest1() { //Given, this is the expected behavior //change variables to names of group's methods double x = 2; double y = 4; - double expectedSum = x + y; //When //calling groups class and method to store the value of the actual method // - double actual = Calculator.addTwoNums(4,2); + double actual = Calculator.addTwoNums(x,y); //Then - Assert.assertEquals(expectedSum, actual); + Assert.assertEquals(expectedSum, actual, 0.001); } @Test - public void subtract(){ - + public void addTest2() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; + double x = 60; + double y = 40; + double expectedSum = x + y; + + //When + //calling group's class and method to store the value of the actual method + // + double actual = Calculator.addTwoNums(x, y); - int expectedDifference = x - y; + //Then + Assert.assertEquals(expectedSum, actual, 0.001); + } + + @Test + public void addTest3() { + //Given, this is the expected behavior + //change variables to names of group's methods + double x = 50; + double y = -40; + double expectedSum = x + y; //When - //calling groups class and method to store the value of the actual method + //calling group's class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Calculator.addTwoNums(x, y); //Then - Assert.assertEquals(expectedDifference, actual); + Assert.assertEquals(expectedSum, actual, 0.001); } + + @Test - public void divide(){ + public void subtractTest1() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; + double x = 6; + double y = 4; - int expectedQuotient = x / y; + double expectedDifference = x - y; //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Calculator.subtractTwoNums(x,y); + + //Then + Assert.assertEquals(expectedDifference, actual, .001); + } + @Test + public void subtractTest2() { + //Given, this is the expected behavior + //change variables to names of group's methods + double x = -50; + double y = -90; + double expectedDifference = x - y; + + //When + //calling group's class and method to store the value of the actual method + // + double actual = Calculator.subtractTwoNums(x, y); + + //Then + Assert.assertEquals(expectedDifference, actual, 0.001); + } + @Test + public void subtractTest3() { + //Given, this is the expected behavior + //change variables to names of group's methods + double x = -502; + double y = 600; + double expected = x - y; + + //When + //calling group's class and method to store the value of the actual method + // + double actual = Calculator.subtractTwoNums(x, y); //Then - Assert.assertEquals(expectedQuotient, actual); + Assert.assertEquals(expected, actual, 0.001); } + + @Test - public void multiply(){ + public void multiplyTest1() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; + double x = 2; + double y = 4; - int expectedProduct = x * y; + double expected = x * y; //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Calculator.multiplyTwoNums(x,y); + + //Then + Assert.assertEquals(expected, actual, .001); + } + @Test + public void multiplyTest2() { + //Given, this is the expected behavior + //change variables to names of group's methods + double x = 50; + double y = -40; + double expected = x * y; + + //When + //calling group's class and method to store the value of the actual method + // + double actual = Calculator.multiplyTwoNums(x, y); + + //Then + Assert.assertEquals(expected, actual, 0.001); + } + @Test + public void multiplyTest3() { + //Given, this is the expected behavior + //change variables to names of group's methods + double x = -5; + double y = -10; + double expected = x * y; + + //When + //calling group's class and method to store the value of the actual method + // + double actual = Calculator.multiplyTwoNums(x, y); //Then - Assert.assertEquals(expectedProduct, actual); + Assert.assertEquals(expected, actual, 0.001); } + + @Test - public void square(){ + public void divideTest1() { //Given, this is the expected behavior //change variables to names of group's methods - double x; + double x = 40; + double y = 10; + double expected; - double expectedSquare = Math.pow(x); + if (x != 0 && y != 0) { + expected = x /y; + } else { + expected = 0; + } //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Calculator.divide(x,y); //Then - Assert.assertEquals(expectedSquare, actual); + Assert.assertEquals(expected, actual, .001); } - @Test - public void squareRoot(){ + public void divideTest2() { + + //Given, this is the expected behavior + //change variables to names of group's methods + double x = 0; + double y = 50; + double expected; + + if (x != 0 && y != 0) { + expected = x /y; + } else { + expected = 0; + } + //When + //calling groups class and method to store the value of the actual method + // + double actual = Calculator.divide(x,y); + //Then + Assert.assertEquals(expected, actual, .001); + } + @Test + public void divideTest3() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; + double x = 40; + double y = 0; + double expected; - int expectedSquareRoot = Math.sqrt(); + if (x != 0 && y != 0) { + expected = x /y; + } else { + expected = 0; + } //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Calculator.divide(x,y); //Then - Assert.assertEquals(expectedSquareRoot, actual); + Assert.assertEquals(expected, actual, .001); } + + @Test - public void exponentiation(){ + public void exponentiationTest1() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; - - int expectedExponentiation = Math.pow(x, y); + double x = 2; + double y = 4; + double expected = Math.pow(x,y); //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Calculator.exponentiation(x, y); //Then - Assert.assertEquals(expectedExponentiation, actual); + Assert.assertEquals(expected, actual, .001); } - @Test - public void inverseOfNumber(){ + public void exponentiationTest2() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; + double x = 4; + double y = 4; + double expected = Math.pow(x,y); + + //When + //calling groups class and method to store the value of the actual method + // + double actual = Calculator.exponentiation(x, y); + + //Then + Assert.assertEquals(expected, actual, .001); + } + @Test + public void exponentiationTest3() { - int expectedSum = x + y; + //Given, this is the expected behavior + //change variables to names of group's methods + double x = 10; + double y = -10; + double expected = Math.pow(x,y); //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Calculator.exponentiation(x, y); //Then - Assert.assertEquals(expectedSum, actual); + Assert.assertEquals(expected, actual, .001); } + + + @Test + public void sqrTest1() { + + //Given, this is the expected behavior + //change variables to names of group's methods + double x = 2; + double expected = (x * x); + + //When + //calling groups class and method to store the value of the actual method + // + double actual = Calculator.square(x); + + //Then + Assert.assertEquals(expected, actual, .001); + } @Test - public void invertSignOfNumber(){ + public void sqrTest2() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; + double x = 5; + double expected = (x * x); + + //When + //calling groups class and method to store the value of the actual method + // + double actual = Calculator.square(x); + + //Then + Assert.assertEquals(expected, actual, .001); + } + @Test + public void sqrTest3() { - int expectedInverseSignOfNumber = Math.abs(); + //Given, this is the expected behavior + //change variables to names of group's methods + double x = 12; + double expected = (x * x); //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Calculator.square(x); //Then - Assert.assertEquals(expectedSum, actual); + Assert.assertEquals(expected, actual, .001); } + + @Test - public void err(){ + public void sqrtTest1() { + //Given, this is the expected behavior + //change variables to names of group's methods + double x = 16; + double expected = Math.sqrt(x); + + //When + //calling group's class and method to store the value of the actual method + // + double actual = Calculator.squareRoot(x); + //Then + Assert.assertEquals(expected, actual, 0.001); + } + @Test + public void sqrtTest2() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; + double x = 25; + double expected = Math.sqrt(x); + + //When + //calling group's class and method to store the value of the actual method + // + double actual = Calculator.squareRoot(x); - int expectedSum = x + y; + //Then + Assert.assertEquals(expected, actual, 0.001); + } + @Test + public void sqrtTest3() { + //Given, this is the expected behavior + //change variables to names of group's methods + double x = 100; + double expected = Math.sqrt(x); //When - //calling groups class and method to store the value of the actual method + //calling group's class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Calculator.squareRoot(x); //Then - Assert.assertEquals(expectedSum, actual); + Assert.assertEquals(expected, actual, 0.001); } + + @Test - public void shi(){ + public void invertSignTest1() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; + double x = 2; + double expectedSum = x * -1; + + //When + //calling groups class and method to store the value of the actual method + // + double actual = Calculator.invertSign(x); + + //Then + Assert.assertEquals(expectedSum, actual, .001); + } + @Test + public void invertSignTest2() { + //Given, this is the expected behavior + //change variables to names of group's methods + double x = -16; + double expectedSum = x * -1; + + //When + //calling groups class and method to store the value of the actual method + // + double actual = Calculator.invertSign(x); - int expectedSum = x + y; + //Then + Assert.assertEquals(expectedSum, actual, .001); + } + @Test + public void invertSignTest3() { + //Given, this is the expected behavior + //change variables to names of group's methods + double x = 0; + double expectedSum = x * -1; //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Calculator.invertSign(x); //Then - Assert.assertEquals(expectedSum, actual); + Assert.assertEquals(expectedSum, actual, .001); } -} +} \ No newline at end of file diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/ScientificTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/ScientificTest.java index 6f945dc..5275f97 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/ScientificTest.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/ScientificTest.java @@ -1,216 +1,204 @@ package com.zipcodewilmington.scientific_calculator; import com.zipcodewilmington.scientificcalculator.MainApplication; +import com.zipcodewilmington.scientificcalculator.Calculator; +import com.zipcodewilmington.scientificcalculator.Science; +import org.apache.commons.io.output.ByteArrayOutputStream; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import java.io.PrintStream; + public class ScientificTest { - @Test - public void sine(){ + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + + @Before + public void setUpStreams() { + // changes "System.out.print" commands to print to this stream, rather than the console + System.setOut(new PrintStream(outContent)); + } + @After + public void restoreStreams() { + // we do this to avoid having any null pointer exceptions + System.setOut(originalOut); + } + + @Test + public void sinTest1() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; - - int expectedSine = Math.sin(); + double x = 60; + double expected = Math.sin(x); //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Science.Sin(x); //Then - Assert.assertEquals(expectedSine, actual); + Assert.assertEquals(expected, actual, 0.001); } @Test - public void cosine(){ - + public void cosTest1() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; - - int expectedCosine = Math.cos(); + double x = 60; + double expected = Math.cos(x); //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Science.Cos(x); //Then - Assert.assertEquals(expectedCosine, actual); + Assert.assertEquals(expected, actual, 0.001); } @Test - public void tangent(){ - + public void tanTest1() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; - - int expectedtan = Math.tan(); + double x = 60; + double expected = Math.tan(x); //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Science.Tan(x); //Then - Assert.assertEquals(expectedtan, actual); + Assert.assertEquals(expected, actual, 0.001); } @Test - public void inverseSine(){ - + public void asinTest1() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; - - int expectedInverseSine = Math.asin(); + double x = 60; + double expected = Math.asin(x); //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Science.aSin(x); //Then - Assert.assertEquals(expectedInverseSine, actual); + Assert.assertEquals(expected, actual, 0.001); } @Test - public void inverseCosine(){ - + public void acosTest1() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; - - int expectedInverseCosine = Math.acos(); + double x = 60; + double expected = Math.acos(x); //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Science.aCos(x); //Then - Assert.assertEquals(expectedInverseCosine, actual); + Assert.assertEquals(expected, actual, 0.001); } @Test - public void inverseTangent(){ - + public void atanTest1() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; - - int expectedInverseTangent = Math.atan(); + double x = 60; + double expected = Math.atan(x); //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Science.aTan(x); //Then - Assert.assertEquals(expectedInverseTangent, actual); + Assert.assertEquals(expected, actual, 0.001); } @Test - public void log(){ - + public void logTest1() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; - - int expectedLog = Math.log(); + double x = 60; + double expected = Math.log(x); //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Science.log(x); //Then - Assert.assertEquals(expectedLog, actual); + Assert.assertEquals(expected, actual, 0.001); } @Test - public void inverseLog(){ - + public void inverseLogTest1() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; - - int expectedInverseLog = x + y; + double x = 60; + double expected = Math.log10(x); //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Science.invLog(x); //Then - Assert.assertEquals(expectedSum, actual); + Assert.assertEquals(expected, actual, 0.001); } @Test - public void naturalLog(){ - + public void naturalLogTest1() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; - - int expectedSum = x + y; + double x = 60; + double expected = Math.log(x); //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Science.natLog(x); //Then - Assert.assertEquals(expectedSum, actual); + Assert.assertEquals(expected, actual, 0.001); } @Test - public void inverseNaturalLog(){ - + public void inverseNaturalLogTest1() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; - - int expectedInverseNaturalLog = Math.exp(); + double x = 60; + double expected = Math.exp(Math.log(x)); //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Science.invNatLog(x); //Then - Assert.assertEquals(expectedInverseNaturalLog, actual); + Assert.assertEquals(expected, actual, 0.001); } - @Test - public void factorial(){ - + public void factorialTest1() { //Given, this is the expected behavior //change variables to names of group's methods - int x = 2; - int y = 4; - - int expectedSum = x + y; + double x = 60; + double expected = Math.acos(x); //When //calling groups class and method to store the value of the actual method // - int actual = MainApplication.add(4,2); + double actual = Science.aCos(x); //Then - Assert.assertEquals(expectedSum, actual); + Assert.assertEquals(expected, actual, 0.001); } } From 9f1e90af232b89bd0c7fc937caeffc4690c9775e Mon Sep 17 00:00:00 2001 From: Manny Date: Sun, 2 Jul 2023 21:08:26 -0400 Subject: [PATCH 9/9] factorial test --- .../scientific_calculator/ScientificTest.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/ScientificTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/ScientificTest.java index 5275f97..fa51d9c 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/ScientificTest.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/ScientificTest.java @@ -187,16 +187,25 @@ public void inverseNaturalLogTest1() { Assert.assertEquals(expected, actual, 0.001); } + @Test public void factorialTest1() { //Given, this is the expected behavior //change variables to names of group's methods - double x = 60; - double expected = Math.acos(x); - + double storedNum = 5; + double x = storedNum; + double expected = 120; + double total = 1; + for (int i = 0; 0 < x; i++){ + + // add N to the total sum of the number + total *= x;//3 + x -= 1; + //have total increment with i till its less than n + } //When //calling groups class and method to store the value of the actual method // - double actual = Science.aCos(x); + double actual = Science.factorial(storedNum); //Then Assert.assertEquals(expected, actual, 0.001);