diff --git a/pom.xml b/pom.xml index e7cb4f6..34ff6f4 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,24 @@ com.zipcodewilmington scientific_calculator 1.0-SNAPSHOT + + + junit + junit + 4.13.2 + test + + + org.junit.jupiter + junit-jupiter-api + 5.9.0-M1 + test + + + + 18 + 18 + \ No newline at end of file diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java new file mode 100644 index 0000000..29777a9 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java @@ -0,0 +1,21 @@ +package com.zipcodewilmington.scientificcalculator; + +public class BasicCalculator { + String name; + + public double add(double screen, double num2 ) { + return screen + num2; + } + public double subtract(double screen, double num2 ) { + return screen - num2; + } + public double multiply(double screen, double num2 ) { + return screen * num2; + } + public double divide(double screen, double num2 ) { + return screen / num2; + } + public double modulus(double screen, double num2 ) { + return screen % num2; + } +} 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..07b67ec 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -1,17 +1,162 @@ 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); + Console.println("Welcome to my calculator! Default units are degrees, display mode is Decimal."); +// 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); + Scanner input = new Scanner(System.in); + ScientificFunctionality sf = new ScientificFunctionality(); + BasicCalculator bc = new BasicCalculator(); + + boolean loop = true; + double screen = 0; + + do { + + if (screen == 0) { + System.out.print("Enter number: "); + screen = input.nextDouble(); + } else { + + System.out.print("Enter operation: "); + String operation = input.next(); + String operator = operation.toLowerCase(); + + switch (operator) { + case "clear": + screen = 0; + Console.println("Answer: %s", screen); + break; + case "add": + System.out.println("Enter number: "); + double num2 = input.nextDouble(); + screen = bc.add(screen, num2); + Console.println("Answer: %s", screen); + break; + case "subtract": + System.out.println("Enter number: "); + num2 = input.nextDouble(); + screen = bc.subtract(screen, num2); + Console.println("Answer: %s", screen); + break; + case "multiply": + System.out.println("Enter number: "); + num2 = input.nextDouble(); + screen = bc.multiply(screen, num2); + Console.println("Answer: %s", screen); + break; + case "divide": + System.out.println("Enter number: "); + num2 = input.nextDouble(); + if (num2 == 0) { + System.out.println("Divide by 0 error"); + } else { + screen = bc.divide(screen, num2); + Console.println("Answer: %s", screen); + } + break; + case "modulus": + System.out.println("Enter number: "); + num2 = input.nextDouble(); + if (num2 == 0) { + System.out.println("Divide by 0 error"); + } else { + screen = bc.modulus(screen, num2); + Console.println("Answer: %s", screen); + } + break; + case "square": + screen *= screen; + Console.println("Answer: %s", screen); + break; + case "squareroot": + screen = Math.sqrt(screen); + Console.println("Answer: %s", screen); + break; + case "exponent": + System.out.println("Enter number: "); + num2 = input.nextDouble(); + screen = Math.pow(screen, num2); + Console.println("Answer: %s", screen); + break; + case "inverse": + screen = 1 / screen; + Console.println("Answer: %s", screen); + break; + case "negative": + screen = -screen; + Console.println("Answer: %s", screen); + break; + case "sin": + screen = sf.calcSine(screen); + Console.println("Answer: %s", screen); + break; + case "cos": + screen = sf.calcCoSine(screen); + Console.println("Answer: %s", screen); + break; + case "tan": + screen = sf.calcTangent(screen); + Console.println("Answer: %s", screen); + break; + case "arcsin": + screen = sf.calcInverseSine(screen); + Console.println("Answer: %s", screen); + break; + case "arccos": + screen = sf.calcInverseCosine(screen); + Console.println("Answer: %s", screen); + break; + case "arctan": + screen = sf.calcInverseTangent(screen); + Console.println("Answer: %s", screen); + break; + case "log": + screen = sf.calcLog(screen); + Console.println("Answer: %s", screen); + break; + case "inverselog": + screen = sf.calcInverseLog(screen); + Console.println("Answer: %s", screen); + break; + case "ln": + screen = sf.calcNaturalLog(screen); + Console.println("Answer: %s", screen); + break; + case "inverseln": + screen = sf.calcInverseNaturalLog(screen); + Console.println("Answer: %s", screen); + break; + + case "units": + System.out.println("Degrees or Radians? "); + String units = input.next(); + System.out.println(sf.switchUnitsMode(units)); + break; + case "mode": + System.out.println("Binary, Octal, Decimal or Hexadecimal? "); + String mode = input.next(); + System.out.println(sf.switchDisplayMode(mode)); + break; + default: + System.out.println("Command not recognized."); + break; + } + System.out.println("Continue? Y or N: "); + String more = input.next(); + if (more.equals("N")) { + loop = false; + }} + } while(loop); } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificFunctionality.java b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificFunctionality.java new file mode 100644 index 0000000..4615110 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificFunctionality.java @@ -0,0 +1,198 @@ +package com.zipcodewilmington.scientificcalculator; + + +public class ScientificFunctionality { + + //Instance variables + private String unitsMode; + private String displayMode; + + private int decValue; + private String binValue; + + private String octValue; + + private String hexValue; + private String memValue; + + // Creating a constructor so that unit & display modes MUST be set + public ScientificFunctionality(){ + unitsMode = "Degrees"; + displayMode = "Decimal"; + } + + //Logarithmic functions + public Double calcLog(double input) { + return Math.log10(input); + } + + public Double calcInverseLog(double input) { + return Math.pow(10, input); + } + + public Double calcNaturalLog(double input) { + return Math.log(input); + } + + public Double calcInverseNaturalLog(double input) { + return Math.exp(input); + } + + //Trig Functions + public Double calcSine(double input) { + // if my units are degrees convert to radians then return answer + // Because Math trig functions only take radians not degrees + if (unitsMode.equals("Degrees")) { + input = Math.toRadians(input); + } + return Math.sin(input); + } + + public Double calcCoSine(double input) { + if (unitsMode.equals("Degrees")) { + input = Math.toRadians(input); + } + return Math.cos(input); + } + + public Double calcTangent(double input) { + if (unitsMode.equals("Degrees")) { + input = Math.toRadians(input); + } + return Math.tan(input); + } + + public Double calcInverseSine(double input) { + if (unitsMode.equals("Degrees")) { + input = Math.toRadians(input); + } + return Math.asin(input); + } + + public Double calcInverseCosine(double input) { + if (unitsMode.equals("Degrees")) { + input = Math.toRadians(input); + } + return Math.acos(input); + } + + public Double calcInverseTangent(double input) { + if (unitsMode.equals("Degrees")) { + input = Math.toRadians(input); + } + return Math.atan(input); + } + //Memory functions + + + //switch units functions + public String switchUnitsMode() { + if (unitsMode.equals("Degrees")) { + unitsMode = "Radians"; + } else { + unitsMode = "Degrees"; + } + return unitsMode; + } + + public String switchUnitsMode(String mode) { + if (mode.equals("Degrees") || mode.equals("Radians")) { + unitsMode = mode; + } else { + System.out.println("Invalid units mode, setting units mode to Degrees"); + unitsMode = "Degrees"; + } + return unitsMode; + } + + public String getUnitsMode() { + return unitsMode; + } + + public void displayUnitsMode(){ + System.out.println(unitsMode); + } + + //switch display mode functions + // Should be used like switchDisplayMode THEN use + // ConvertFromDecimal THEN getDisplayModeValue + public String switchDisplayMode(){ + if(displayMode.equals("Binary")){ + displayMode = "Octal"; + } else if (displayMode.equals("Octal")) { + displayMode = "Decimal"; + } else if (displayMode.equals("Decimal")) { + displayMode = "Hexadecimal"; + } else if (displayMode.equals("Hexadecimal")) { + displayMode = "Binary"; + } + return displayMode; + } + + public String switchDisplayMode(String mode) { + if (mode.equals("Binary") || mode.equals("Octal") || mode.equals("Decimal") || mode.equals("Hexadecimal")) { + displayMode = mode; + } else { + System.out.println("Invalid display mode, setting display mode to Decimal"); + displayMode = "Decimal"; + } + return displayMode; + } + + // Converting results to Decimal so that things can be computed + public String getDisplayModeValue(){ + String result; + + if(displayMode.equals("Binary")){ + result = getBinValue(); + } else if(displayMode.equals("Octal")){ + result = getOctValue(); + } else if(displayMode.equals("Decimal")){ + result = String.valueOf(getDecValue()); + } else { + result = getHexValue(); + } + return result; + } + + public void showDisplayModeValue(){ + String value = getDisplayModeValue(); + System.out.println(value); + } + + public void convertFromDecimal(int input){ + binValue = Integer.toBinaryString(input); + octValue = Integer.toOctalString(input); + hexValue = Integer.toHexString(input); + decValue = input; + } + + public String storeMemoryValue(String value){ + memValue = value; + return memValue; + } + + public void clearMemoryValue(){ + memValue = null; + } + + public String getMemoryValue(){ + return memValue; + } + + public String getBinValue(){ + return binValue; + } + + public String getOctValue(){ + return octValue; + } + + public String getHexValue(){ + return hexValue; + } + + public int getDecValue(){ + return decValue; + } +} diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java index 94e8d98..5db736e 100644 --- a/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestMainApplication.java @@ -1,7 +1,58 @@ package com.zipcodewilmington.scientific_calculator; -/** - * Created by leon on 2/9/18. - */ +import com.zipcodewilmington.scientificcalculator.BasicCalculator; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + public class TestMainApplication { -} + + BasicCalculator calc = new BasicCalculator(); + + @Test + public void testAddition() { + double expected = 21; + int screen = 12; + int num2 = 9; + double actual = calc.add(screen, num2); + Assertions.assertEquals(expected, actual); + + } + + @Test + public void testSubtraction() { + double expected = 15; + int screen = 20; + int num2 = 5; + double actual = calc.subtract(screen, num2); + Assertions.assertEquals(expected, actual); + + } + + @Test + public void testMultiplication() { + double expected = 40; + int screen = 10; + int num2 = 4; + double actual = calc.multiply(screen, num2); + Assertions.assertEquals(expected, actual); + + } + + @Test + public void testDivision() { + double expected = 27; + int screen = 54; + int num2 = 2; + double actual = calc.divide(screen, num2); + Assertions.assertEquals(expected, actual); + } + + @Test + public void testModulus() { + double expected = 0; + int screen = 50; + int num2 = 5; + double actual = calc.modulus(screen, num2); + Assertions.assertEquals(expected, actual); + } +} \ No newline at end of file diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/TestScientificFunctionality.java b/src/test/java/com/zipcodewilmington/scientific_calculator/TestScientificFunctionality.java new file mode 100644 index 0000000..891d388 --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/TestScientificFunctionality.java @@ -0,0 +1,104 @@ + +package com.zipcodewilmington.scientific_calculator; + + +import com.zipcodewilmington.scientificcalculator.ScientificFunctionality; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class TestScientificFunctionality { + + ScientificFunctionality sciFunction = new ScientificFunctionality(); + @Test + public void testCalcLog() { + double expected = 0.6020599913279624; + double input = 4; + double actual = sciFunction.calcLog(input); + Assertions.assertEquals(expected, actual); + } + + @Test + public void testCalcInverseLog() { + double expected = 1000; + double input = 3; + double actual = sciFunction.calcInverseLog(input); + Assertions.assertEquals(expected, actual); + } + + @Test + public void testCalcNaturalLog() { + double expected = 1.6094379124341003; + double input = 5; + double actual = sciFunction.calcNaturalLog(input); + Assertions.assertEquals(expected, actual); + } + + @Test + public void testCalcInverseNaturalLog() { + + double expected = 20.085536923187668; + double input = 3; + double actual = sciFunction.calcInverseNaturalLog(input); + Assertions.assertEquals(expected, actual); + } + + @Test + public void testCalcSine() { + double expected = 0; + //input in radians + double input = 0; + double actual = sciFunction.calcSine(input); + Assertions.assertEquals(expected, actual); + } + + @Test + public void testCalcCoSine() { + double expected = 0.87758256189; + //input in radians + double input = 0.5; + double actual = sciFunction.calcCoSine(input); + Assertions.assertEquals(expected, actual); + } + + @Test + public void testCalcTangent() { + double expected = 0.54630248984; + //input in radians + double input =0.5; + double actual = sciFunction.calcTangent(input); + Assertions.assertEquals(expected, actual); + } + + @Test + public void testCalcInverseSine() { + double expected = 0; + //input in radians + double input = 0; + double actual = sciFunction.calcInverseCosine(input); + Assertions.assertEquals(expected, actual); + } + + @Test + public void testCalcInverseCosine() { + double expected = 1.5707963267948966; + //input in radians + double input = 0; + double actual = sciFunction.calcInverseCosine(input); + Assertions.assertEquals(expected, actual); + } + + @Test + public void testCalcInverseTangent() { + double expected = 0.78539816339; + //input in radians + double input = 1; + double actual = sciFunction.calcInverseTangent(input); + Assertions.assertEquals(expected, actual); + } + @Test + public void testSwitchUnitsMode(){ + String expected = "Degrees"; + + } + +}