diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..32f093e4 Binary files /dev/null and b/.DS_Store differ diff --git a/CalculatorUML.pdf b/CalculatorUML.pdf new file mode 100644 index 00000000..621613c1 Binary files /dev/null and b/CalculatorUML.pdf differ diff --git a/pom.xml b/pom.xml index e7cb4f6b..41afa1a0 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,32 @@ com.zipcodewilmington scientific_calculator 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + + + + junit + junit + 4.12 + test + + + junit + junit + 4.12 + test + + \ No newline at end of file diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 00000000..2a2c3316 Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/main/.DS_Store b/src/main/.DS_Store new file mode 100644 index 00000000..a89f0faa Binary files /dev/null and b/src/main/.DS_Store differ diff --git a/src/main/java/.DS_Store b/src/main/java/.DS_Store new file mode 100644 index 00000000..98fd081d Binary files /dev/null and b/src/main/java/.DS_Store differ diff --git a/src/main/java/com/.DS_Store b/src/main/java/com/.DS_Store new file mode 100644 index 00000000..14332ba4 Binary files /dev/null and b/src/main/java/com/.DS_Store differ diff --git a/src/main/java/com/zipcodewilmington/.DS_Store b/src/main/java/com/zipcodewilmington/.DS_Store new file mode 100644 index 00000000..90dd125b Binary files /dev/null and b/src/main/java/com/zipcodewilmington/.DS_Store differ 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 00000000..2eeede46 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/BasicCalculator.java @@ -0,0 +1,99 @@ +package com.zipcodewilmington.scientificcalculator; + +public class BasicCalculator { + + //fields for storing results + private Double doubleResult; + private String stringResult; + + //initializing an error class + Error error = new Error(); + + //Constructor + public BasicCalculator(){ + doubleResult = null; + stringResult = "0"; + } + + //Basic Operator functions + public void add(Double x, Double y){ + try { + + this.setDoubleResult(x + y); + this.setStringResult(Double.toString(x + y)); + } + + //for non-double inputs + catch(Exception e){ + this.setDoubleResult(null); + this.setStringResult("ERROR"); + } + } + + public void subtract(Double x, Double y){ + try { + this.setDoubleResult(x - y); + this.setStringResult(Double.toString(x - y)); + + } + //for non double inputs + catch(Exception e){ + this.setDoubleResult(null); + this.setStringResult("ERROR"); + } + } + + public void multiply(Double x, Double y){ + try { + this.setDoubleResult(x * y); + this.setStringResult(Double.toString(x * y)); + + } + //for non double inputs + catch(Exception e){ + this.setDoubleResult(null); + this.setStringResult("ERROR"); + } + } + + public void divide(Double x, Double y){ + try { + if (error.IsBadValue(x, 0.0)){ + this.setDoubleResult(null); + this.setStringResult("ERROR"); + } + else { + this.setDoubleResult(x / y); + this.setStringResult(Double.toString(x / y)); + } + } + //for non double inputs. + catch(Exception e){ + this.setDoubleResult(null); + this.setStringResult("ERROR"); + } + } + + + //Getters and Setters for class fields + + public double getDoubleResult() { + return doubleResult; + } + + public void setDoubleResult(Double doubleResult) { + double number = (Math.round(doubleResult * 1000.0))/1000.0; + this.doubleResult = number; + } + + public String getStringResult() { + return stringResult; + } + + public void setStringResult(String stringResult) { + this.stringResult = stringResult; + } + + + +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java index 83f0e97f..90ee9115 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java @@ -7,6 +7,10 @@ */ public class Console { + public void updatedisplay(){ + + } + public static void print(String output, Object... args) { System.out.printf(output, args); } @@ -17,16 +21,22 @@ public static void println(String output, Object... args) { public static String getStringInput(String prompt) { Scanner scanner = new Scanner(System.in); - println(prompt); + print(prompt); String userInput = scanner.nextLine(); return userInput; } public static Integer getIntegerInput(String prompt) { - return null; + Scanner scanner = new Scanner(System.in); + print(prompt); + Integer userInput = Integer.valueOf(scanner.nextLine()); + return userInput; } public static Double getDoubleInput(String prompt) { - return null; + Scanner scanner = new Scanner(System.in); + print(prompt); + Double userInput = Double.valueOf(scanner.nextLine()); + return userInput; } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/ConversionCalculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/ConversionCalculator.java new file mode 100644 index 00000000..34fd477e --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ConversionCalculator.java @@ -0,0 +1,140 @@ +package com.zipcodewilmington.scientificcalculator; + +//import com.sun.jdi.connect.Connector; + +public class ConversionCalculator { + + //fields for storing results + private Double doubleResult; + private String stringResult; + + //Constructor + public ConversionCalculator(){ + doubleResult = 0.0; + stringResult = "0"; + } + + //Scientific Operator Conversion functions + //(binary, octal, decimal, hexadecimal) + public void binary(int x){ + try { + this.setStringResult(Integer.toBinaryString(x)); + } + + //for non-double inputs + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void binaryToInt(String x){ + try { + Integer y = Integer.parseInt(x,2); + + this.setStringResult(y.toString()); + } + + //for non-double inputs + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void octal(int x){ + try { + + //this.setDoubleResult(Math.pow(x)); + this.setStringResult(Integer.toOctalString(x)); + } + + //for non-double inputs + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void octalToInt(String x){ + try { + Integer y = Integer.parseInt(x,8); + + this.setStringResult(y.toString()); + } + + //for non-double inputs + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void decimal(int x){ + try { + + double y = x; + this.setDoubleResult(y); + this.setStringResult(Double.toString(y)); + } + + //for non-double inputs + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void hexadecimal(int x){ + try { + + this.setStringResult(Integer.toHexString(x)); + } + + //for non-double inputs + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void hexadecimalToInt(String x){ + try { + Integer y = Integer.parseInt(x,16); + + this.setStringResult(y.toString()); + } + + //for non-double inputs + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + + + + + //Getters and Setters for class fields + + public double getDoubleResult() { + return doubleResult; + } + + public void setDoubleResult(Double doubleResult) { + double number = (Math.round(doubleResult * 1000.0))/1000.0; + this.doubleResult = number; + } + + public String getStringResult() { + return stringResult; + } + + public void setStringResult(String stringResult) { + this.stringResult = stringResult; + } + + + +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Error.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Error.java new file mode 100644 index 00000000..5a10f00e --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Error.java @@ -0,0 +1,50 @@ +package com.zipcodewilmington.scientificcalculator; + +public class Error { + + //these methods checks an input value against a series of possible + //values which could cause an error. + + public boolean IssBadValue(Double input, Double[] not_this){ + + for (int i = 0 ;i < not_this.length;i++) { + + if (input == not_this[i]) { + return true; + } + } + return false; + } + + public boolean IsBadValue(Double input, Double not_this){ + + if (input == not_this) { + return true; + } + else { + return false; + } + } + + public boolean checkForBadTrigValues(Double input, Double not_this){ //not_this MUST be a positive value + while(input < -not_this){ + input += (2 * not_this); + } + while(input > not_this){ + input -= (2 * not_this); + } + if(input == not_this || input == -not_this){ + return true; + } + else { + return false; + } + } + + public boolean checkForOutsideRange(Double input, Double lowerlimit, Double higherlimit){ + if(input < lowerlimit || input > higherlimit){ + return true; + } + return false; + } +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/GetInputs.java b/src/main/java/com/zipcodewilmington/scientificcalculator/GetInputs.java new file mode 100644 index 00000000..959643e8 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/GetInputs.java @@ -0,0 +1,38 @@ +package com.zipcodewilmington.scientificcalculator; + +public class GetInputs { + + public Double getx(double x, String prompt){ + + int i = 0; + while (i == 0) { + + try { + if (x == 0) { + x = Console.getDoubleInput(prompt); + } + i = 1; + } catch (Exception e) { + Console.println("Please enter a number"); + } + } + return x; + } + + public Double gety(String prompt){ + + double y = 0; + boolean i = true; + + while (i) { + try { + y = Console.getDoubleInput(prompt); + i = false; + } + catch (Exception e) { + Console.println("Please enter a number"); + } + } + return y; + } +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 5f421325..4b954bea 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -5,13 +5,246 @@ */ 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); + + //initial setup of fields + Double currentDouble = 0.00; + String currentString = ""; + Double y; + + //creating instances of classes + BasicCalculator basicCalc = new BasicCalculator(); + ScientificCalculator sciencecalc = new ScientificCalculator(); + MemoryAndSettings memoryandSettings = new MemoryAndSettings(); + GetInputs getInputs = new GetInputs(); + Console console = new Console(); + + String s = ""; + + console.print("Welcome to my calculator!\n" + + "Type 'help' for list of commands.\n" + + "What would you like to do?\n"); + + while (s !="exit") { + + s = console.getStringInput(""); + + switch (s) { + case "add": + + currentDouble = getInputs.getx(currentDouble, "First number: "); + currentString = Double.toString(currentDouble); + y = getInputs.gety(currentString + " + "); + + basicCalc.add(currentDouble, y); + currentDouble = basicCalc.getDoubleResult(); + currentString = basicCalc.getStringResult(); + break; + + case "subtract": + + currentDouble = getInputs.getx(currentDouble,"First number: "); + currentString = Double.toString(currentDouble); + y = getInputs.gety(currentString + " - "); + + basicCalc.subtract(currentDouble, y); + currentDouble = basicCalc.getDoubleResult(); + currentString = basicCalc.getStringResult(); + break; + + case "multiply": + + currentDouble = getInputs.getx(currentDouble,"First Number: "); + currentString = Double.toString(currentDouble); + y = getInputs.gety(currentString + " * "); + + basicCalc.multiply(currentDouble, y); + currentDouble = basicCalc.getDoubleResult(); + currentString = basicCalc.getStringResult(); + break; + + case "divide": + + currentDouble = getInputs.getx(currentDouble,"First number"); + currentString = Double.toString(currentDouble); + y = getInputs.gety(currentString + " / "); + + basicCalc.divide(currentDouble, y); + currentDouble = basicCalc.getDoubleResult(); + currentString = basicCalc.getStringResult(); + break; + + case "sine": + currentDouble = getInputs.getx(currentDouble,"sine of: "); + sciencecalc.sine(currentDouble); + currentDouble = sciencecalc.getDoubleResult(); + currentString = sciencecalc.getStringResult(); + break; + + case "cosine": + currentDouble = getInputs.getx(currentDouble,"cosine of: "); + sciencecalc.cosine(currentDouble); + currentDouble = sciencecalc.getDoubleResult(); + currentString = sciencecalc.getStringResult(); + break; + + case "tangent": + currentDouble = getInputs.getx(currentDouble,"tangent of: "); + sciencecalc.tangent(currentDouble); + currentDouble = sciencecalc.getDoubleResult(); + currentString = sciencecalc.getStringResult(); + break; + + case "arcsine": + currentDouble = getInputs.getx(currentDouble,"Inverse Sine of: "); + sciencecalc.inverseSine(currentDouble); + currentDouble = sciencecalc.getDoubleResult(); + currentString = sciencecalc.getStringResult(); + break; + + case "arccosine": + currentDouble = getInputs.getx(currentDouble,"Inverse Cosine of: "); + sciencecalc.inverseCosine(currentDouble); + currentDouble = sciencecalc.getDoubleResult(); + currentString = sciencecalc.getStringResult(); + break; + + case "arctangent": + currentDouble = getInputs.getx(currentDouble,"Inverse Tangent of: "); + sciencecalc.inverseTangent(currentDouble); + currentDouble = sciencecalc.getDoubleResult(); + currentString = sciencecalc.getStringResult(); + break; + + case "log": + currentDouble = getInputs.getx(currentDouble,"log of: "); + sciencecalc.log(currentDouble); + currentDouble = sciencecalc.getDoubleResult(); + currentString = sciencecalc.getStringResult(); + break; + + case "log10": + currentDouble = getInputs.getx(currentDouble,"natural log of: "); + sciencecalc.log10(currentDouble); + currentDouble = sciencecalc.getDoubleResult(); + currentString = sciencecalc.getStringResult(); + break; + + case "inverselog": + currentDouble = getInputs.getx(currentDouble,"inverse log of: "); + sciencecalc.inverselog(currentDouble); + currentDouble = sciencecalc.getDoubleResult(); + currentString = sciencecalc.getStringResult(); + break; + + case "inverselog10": + currentDouble = getInputs.getx(currentDouble,"inverse natural log of: "); + sciencecalc.inverseLog10(currentDouble); + currentDouble = sciencecalc.getDoubleResult(); + currentString = sciencecalc.getStringResult(); + break; + + case "exponent": + currentDouble = getInputs.getx(currentDouble, "First number: "); + currentString = Double.toString(currentDouble); + y = getInputs.gety(currentString + " to the power of: "); + sciencecalc.exponent(currentDouble, y); + currentDouble = sciencecalc.getDoubleResult(); + currentString = sciencecalc.getStringResult(); + + break; + + case "square": + currentDouble = getInputs.getx(currentDouble,"Square of: "); + currentString = Double.toString(currentDouble); + sciencecalc.square(currentDouble); + currentDouble = sciencecalc.getDoubleResult(); + currentString = sciencecalc.getStringResult(); + break; + + case "sqrt": + currentDouble = getInputs.getx(currentDouble,"Square root of: "); + currentString = Double.toString(currentDouble); + sciencecalc.squareRoot(currentDouble); + currentDouble = sciencecalc.getDoubleResult(); + currentString = sciencecalc.getStringResult(); + break; + + case "inverse": + currentDouble = getInputs.getx(currentDouble,"Inverse of: "); + currentString = Double.toString(currentDouble); + sciencecalc.inverse(currentDouble); + currentDouble = sciencecalc.getDoubleResult(); + currentString = sciencecalc.getStringResult(); + break; + + case "!": + currentDouble = getInputs.getx(currentDouble,"Factorial of: "); + currentString = Double.toString(currentDouble); + sciencecalc.factorial(currentDouble); + currentDouble = sciencecalc.getDoubleResult(); + currentString = sciencecalc.getStringResult(); + break; + + case "M+": + memoryandSettings.rememberthis(currentDouble); + break; + + case "MRC": + currentDouble = memoryandSettings.getRememberedNum(); + currentString = Double.toString(currentDouble); + console.println(currentDouble + " was recalled."); + break; + + case "MC": + memoryandSettings.forgetthis(); + break; + + case "settings": + memoryandSettings.select(); + break; + + case "clear": + currentDouble = 0.0; + currentString = "0"; + break; + + case "exit": + s = "exit"; + currentString = ""; + break; + + default: + + try { + currentDouble = Double.valueOf(s); + currentString = Double.toString(currentDouble); + } catch (Exception e) { + console.println("I do not understand."); + } + break; + + + + } + + + switch (memoryandSettings.getBodh()) { + case "binary": + break; + case "octal": + break; + case "decimal": + break; + case "hexadecimal": + break; + } + + + + console.println(currentString); + + } + System.exit(0); } + } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MemoryAndSettings.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MemoryAndSettings.java new file mode 100644 index 00000000..e574a768 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MemoryAndSettings.java @@ -0,0 +1,188 @@ +package com.zipcodewilmington.scientificcalculator; + +public class MemoryAndSettings { + + //fields + private Double rememberedNum; + private String rememberedStr; + private String bodh; + private String radordeg; + + private String[] displaytype = {"binary", "octal","decimal","hexadecimal"}; + private int currenttypeindex; + + + + //constructor + public MemoryAndSettings() { + rememberedNum = 0.0; + rememberedStr = "0"; + currenttypeindex = 2; + bodh = displaytype[currenttypeindex]; + radordeg = "radians"; + } + + //methods + + //for setting the type (binary, octal, decimal, hexadecimal) + public void switchDisplayMode(){ + if(this.currenttypeindex >= 3){ + this.currenttypeindex = 0; + } + else{ + this.currenttypeindex++; + } + this.bodh = this.displaytype[currenttypeindex]; + Console.println("Current type is " + bodh); + } + + public void switchDisplayMode(String type){ + if (type.equals("binary")){ + this.currenttypeindex = 0; + } + else if(type.equals("octal")){ + this.currenttypeindex = 1; + } + else if(type.equals("decimal")){ + this.currenttypeindex = 2; + } + else if(type.equals("hexadecimal")){ + this.currenttypeindex = 3; + } + this.bodh = this.displaytype[currenttypeindex]; + Console.println("Current type is " + bodh); + + } + + //-------------------------------------------- + + //for setting the units (radians or degrees) + public void switchUnitsMode(){ + if (radordeg.equals("radians")){ + this.setRadordeg("degrees"); + } + else{ + this.setRadordeg("radians"); + } + Console.println("Set units to " + radordeg); + } + + public void switchUnitsMode(String type){ + if (type.equals("degrees")){ + this.setRadordeg("degrees"); + } + else if(type.equals("radians")){ + this.setRadordeg("radians"); + } + else{ + Console.println("We're having some trouble."); + } + Console.println("Set units to " + radordeg); + } + + //-------------------------------------------- + + //for M+, MC, and MRC functions + public void rememberthis(Double m){ + rememberedNum = m; + rememberedStr = Double.toString(m); + Console.println(m + " was committed to memory."); + } + + public void forgetthis(){ + Console.println(rememberedNum + " was forgotten."); + rememberedNum = 0.0; + rememberedStr = "0"; + + } + + //-------------------------------------------- + + //method for the settings menu + public void select() { + + String x = ""; + + while (x != "back") { + + x = Console.getStringInput("What do you want to change?\n" + + "(Type 'help' for a list of commands)\n"); + + switch (x) { + case "help": + Console.println("switch units: cycles between radians and degrees\n" + + "(unit): changes to type specified. Replace (unit) with either 'radians' or 'degrees'\n" + + "switch type: Cycles between displaying results in binary, octal, decimal, and hexadecimal\n " + + "(type): changes to specified type. Replace (unit) with: 'binary', 'octal, 'decimal, or 'hex'\n" + + "back: exits settings"); + break; + + case "switch units": + this.switchUnitsMode(); + break; + + case "radians": + this.switchUnitsMode(x); + break; + + case "degrees": + this.switchUnitsMode(x); + break; + + case "switch type": + this.switchDisplayMode(); + break; + + case "binary": + this.switchDisplayMode(x); + break; + + case "octal": + this.switchDisplayMode(x); + break; + + case "decimal": + this.switchDisplayMode(x); + break; + + case "hex": + this.switchDisplayMode(x); + break; + + case "back": + x = "back"; + break; + + default: + break; + } + } + } + + //getters and Setters + public String getBodh() { + return bodh; + } + public String getRadordeg() { + return radordeg; + } + public void setRadordeg(String x){ + this.radordeg = x; + } + + public Double getRememberedNum() { + return rememberedNum; + } + + public String getRememberedStr() { + return rememberedStr; + } + + public void setRememberedNum(Double rememberedNum) { + this.rememberedNum = rememberedNum; + } + + public void setRememberedStr(String rememberedStr) { + this.rememberedStr = rememberedStr; + } +} 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 00000000..5c27cf0c --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/ScientificCalculator.java @@ -0,0 +1,301 @@ +package com.zipcodewilmington.scientificcalculator; + +public class ScientificCalculator { + + + //fields for storing results + private Double doubleResult; + private String stringResult; + + //initializing an error class + Error error = new Error(); + + //Constructor + public ScientificCalculator(){ + doubleResult = 0.0; + stringResult = "0"; + } + + //Scientific Operator functions + public void exponent(Double x, Double y){ + try { + this.setDoubleResult(Math.pow(x, y)); + this.setStringResult(Double.toString(Math.pow(x,y))); + } + + //for non-double inputs + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void square(Double x){ + try { + + this.setDoubleResult(x * x); + this.setStringResult(Double.toString(x * x)); + } + + //for non-double inputs + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void squareRoot(Double x){ + try { + if(x < 0) { //checks for a non-complex root + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + else{ + this.setDoubleResult(Math.sqrt(x)); + this.setStringResult(Double.toString(Math.sqrt(x))); + } + } + //for non double inputs + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + //natural log + public void log(Double x){ + try { + if(x < 0) { //checks for a negative input + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + else { + this.setDoubleResult(Math.log(x)); + this.setStringResult(Double.toString(Math.log(x))); + } + } + //for non double inputs + catch(Exception e){ + this.setStringResult("ERROR"); + } + } + + //inverse natural log + public void inverselog(Double x){ + try { + this.setDoubleResult(Math.exp(x)); + this.setStringResult(Double.toString(Math.exp(x))); + } + //for non double inputs + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void log10(Double x){ + try { + if(x < 0) { //checks for a negative input + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + else { + this.setDoubleResult(Math.log10(x)); + this.setStringResult(Double.toString(Math.log10(x))); + } + } + //for non double inputs + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void inverseLog10(Double x){ + try { + this.setDoubleResult(Math.pow(10.0, x)); + this.setStringResult(Double.toString(Math.pow(10.0, x))); + } + //for non double inputs + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void inverse(Double x){ + try { + if(x == 0){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + else { + this.setDoubleResult(1 / x); + this.setStringResult(Double.toString(1 / x)); + } + } + //for non double inputs + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void switchSign(Double x){ + try { + + this.setDoubleResult(x * -1); + this.setStringResult(Double.toString(x * -1)); + + } + //for non double inputs and division by zero. + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void sine(Double x){ + try { + + this.setDoubleResult(Math.sin(x)); + this.setStringResult(Double.toString(Math.sin(x))); + + } + //for non double inputs + catch (Exception e) { + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void cosine(Double x){ + try { + this.setDoubleResult(Math.cos(x)); + this.setStringResult(Double.toString(Math.cos(x))); + } + //for non double inputs + catch (Exception e) { + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + + } + + + public void tangent(Double x) { + try { + if(error.checkForBadTrigValues(x, (Math.PI/2))){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + else { + this.setDoubleResult(Math.tan(x)); + this.setStringResult(Double.toString(Math.tan(x))); + } + + } + //for non double inputs + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void inverseSine(Double x){ + try { + if(error.checkForOutsideRange(x, -1., 1.)){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + else { + this.setDoubleResult((Math.asin(x))); + this.setStringResult(Double.toString(Math.asin(x))); + } + } + //for non double inputs and division by zero. + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void inverseCosine(Double x){ + try { + if(error.checkForOutsideRange(x, -1., 1.)){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + else { + this.setDoubleResult(Math.acos(x)); + this.setStringResult(Double.toString(Math.acos(x))); + } + } + //for non double inputs and division by zero. + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + + } + + public void inverseTangent(Double x){ + try { + + this.setDoubleResult(Math.atan(x)); + this.setStringResult(Double.toString(Math.atan(x))); + + } + //for non double inputs and division by zero. + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + public void factorial(Double x){ + try { + + if(x < 0){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + else { + int i, fact = 1; + + for (i = 1; i < x; i++) { + + fact = fact * i; + + + this.setDoubleResult(x * fact); + this.setStringResult(Double.toString(x * fact)); + } + } + } + //for non double inputs + catch(Exception e){ + this.setDoubleResult((double) 0); + this.setStringResult("ERROR"); + } + } + + //Getters and Setters for class fields + + public double getDoubleResult() { + return doubleResult; + } + + public void setDoubleResult(Double doubleResult) { + double number = (Math.round(doubleResult * 1000.0))/1000.0; + this.doubleResult = number; + } + + public String getStringResult() { + return stringResult; + } + + public void setStringResult(String stringResult) { + this.stringResult = stringResult; + } +} \ No newline at end of file diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/BasicCalcTests.java b/src/test/java/com/zipcodewilmington/scientific_calculator/BasicCalcTests.java new file mode 100644 index 00000000..c8b9dd11 --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/BasicCalcTests.java @@ -0,0 +1,117 @@ +package com.zipcodewilmington.scientific_calculator; + +import com.zipcodewilmington.scientificcalculator.BasicCalculator; +import org.junit.Before; +import org.junit.Test; +import org.junit.Assert; + + +public class BasicCalcTests { + + private BasicCalculator calc; + + @Before + public void setup() { + calc = new BasicCalculator(); + } + + + @Test + public void addTest1(){ + + calc.add( 101.75, 78.654); + + Assert.assertEquals(180.404,calc.getDoubleResult() , 0); + } + + @Test + public void addTest2(){ + + calc.add((double) 9, (double) 5); + + Assert.assertEquals(14,calc.getDoubleResult() , 0); + } + + @Test + public void addTest3(){ + + calc.add( 100045.875,438294.54); + + Assert.assertEquals(538340.415,calc.getDoubleResult() , 0); + } + + @Test + public void subtractTest1(){ + + calc.subtract( 690045.875, 438294.54); + + Assert.assertEquals(251751.335,calc.getDoubleResult() , .1); + } + + @Test + public void subtractTest2(){ + + calc.subtract( 100045.875, 438294.54); + + Assert.assertEquals(-338248.665,calc.getDoubleResult() , .1); + + } + + @Test + public void subtractTest3(){ + + calc.subtract(86045.875, 38294.54); + + Assert.assertEquals(47751.335,calc.getDoubleResult() , .1); + } + + @Test + public void multiplyTest1(){ + + calc.multiply( 5.5, 5.5); + + Assert.assertEquals(30.25,calc.getDoubleResult() , 0.1); + + } + + @Test + public void multiplyTest2(){ + + calc.multiply( (double) 786, (double) 384); + + Assert.assertEquals(301824,calc.getDoubleResult() , .1); + + } + + @Test + public void multiplyTest3(){ + + calc.multiply( 4083.6, 3834.7); + + Assert.assertEquals(15659380.9,calc.getDoubleResult() , .1 ); + } + + @Test + public void divideTest1(){ + + calc.divide( 40.6, 34.7); + + Assert.assertEquals(1.17002882,calc.getDoubleResult() , .1); + + } + + @Test + public void divideTest2(){ + calc.divide((double) 25, (double) 5); + + Assert.assertEquals(5,calc.getDoubleResult() , .1); + } + + @Test + public void divideTest3(){ + calc.divide( 386442.6, 29742.2); + + Assert.assertEquals(12.9930738,calc.getDoubleResult() , 0.1); + } + +} diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/ConversionCalcTests.java b/src/test/java/com/zipcodewilmington/scientific_calculator/ConversionCalcTests.java new file mode 100644 index 00000000..d8afcb8e --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/ConversionCalcTests.java @@ -0,0 +1,117 @@ +package com.zipcodewilmington.scientific_calculator; + +import com.zipcodewilmington.scientificcalculator.ConversionCalculator; +import com.zipcodewilmington.scientificcalculator.ScientificCalculator; +import org.junit.Before; +import org.junit.Test; +import org.junit.Assert; + +public class ConversionCalcTests { + + private ConversionCalculator cCalc; + + @Before + public void setup(){ + cCalc = new ConversionCalculator(); + + } + + + @Test + public void binaryTest1(){ + + cCalc.binary(10); + + Assert.assertEquals("test", "1010", cCalc.getStringResult()); + } + + @Test + public void binaryTest2(){ + + cCalc.binary(12); + + Assert.assertEquals("test", "1100", cCalc.getStringResult()); + } + + + @Test + public void binaryTest3(){ + + cCalc.binary(15); + + Assert.assertEquals("test", "1111", cCalc.getStringResult()); + } + + + @Test + public void octalTest1(){ + + cCalc.octal(12); + + Assert.assertEquals("test", "14", cCalc.getStringResult()); + } + + @Test + public void octalTest2(){ + + cCalc.octal(15); + + Assert.assertEquals("test", "17", cCalc.getStringResult()); + } + + @Test + public void octalTest3(){ + + cCalc.octal(18); + + Assert.assertEquals("test", "22", cCalc.getStringResult()); + } + + @Test + public void decimalTest1(){ + + cCalc.decimal(12); + + Assert.assertEquals("test", "12.0", cCalc.getStringResult()); + } + + @Test + public void decimalTest2(){ + + cCalc.decimal(125); + + Assert.assertEquals("test", "125.0", cCalc.getStringResult()); + } + + @Test + public void decimalTest3(){ + + cCalc.decimal(1250); + + Assert.assertEquals("test", "1250.0", cCalc.getStringResult()); + } + + @Test + public void hexaDecimalTest1(){ + + cCalc.hexadecimal(30); + + Assert.assertEquals("test", "1e", cCalc.getStringResult()); + } + + @Test + public void hexaDecimalTest2(){ + + cCalc.hexadecimal(25); + + Assert.assertEquals("test", "19", cCalc.getStringResult()); + } + + @Test + public void hexaDecimalTest3(){ + + cCalc.hexadecimal(30); + + Assert.assertEquals("test", "1e", cCalc.getStringResult()); + } +} diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/ScientificCalcTests.java b/src/test/java/com/zipcodewilmington/scientific_calculator/ScientificCalcTests.java new file mode 100644 index 00000000..c8a30949 --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/ScientificCalcTests.java @@ -0,0 +1,449 @@ +package com.zipcodewilmington.scientific_calculator; + +import com.zipcodewilmington.scientificcalculator.ScientificCalculator; +import org.junit.Before; +import org.junit.Test; +import org.junit.Assert; + + +public class ScientificCalcTests { + + private ScientificCalculator sCalc; + + @Before + public void setup(){ + sCalc = new ScientificCalculator(); + } + + @Test + public void squareTest1(){ + + sCalc.square((double) 100); + + Assert.assertEquals(10000,sCalc.getDoubleResult() , 0); + + } + + @Test + public void squareTest2(){ + + sCalc.square((double) 10); + + Assert.assertEquals(100,sCalc.getDoubleResult() , 0); + + } + + @Test + public void squareTest3(){ + + sCalc.square((double) 5000); + + Assert.assertEquals(25000000,sCalc.getDoubleResult() , 0); + + } + + @Test + public void squareRootTest1(){ + + sCalc.squareRoot((double)(-1)); + + Assert.assertEquals("ERROR", sCalc.getStringResult()); + + } + + @Test + public void squareRootTest2(){ + + sCalc.squareRoot((double) 500); + + Assert.assertEquals(22.36067977,sCalc.getDoubleResult() , .1); + + } + + @Test + public void squareRootTest3(){ + + sCalc.squareRoot((double) 1000); + + Assert.assertEquals(31.6227766,sCalc.getDoubleResult() , .1); + + } + + @Test + public void inverseTest1(){ + + sCalc.inverse((double) 2); + + Assert.assertEquals(.5,sCalc.getDoubleResult() , 0); + + } + + @Test + public void inverseTest2(){ + + sCalc.inverse((double) 10); + + Assert.assertEquals(.1,sCalc.getDoubleResult() , 0); + + } + + @Test + public void inverseTest3(){ + + sCalc.inverse((double) 1000); + + Assert.assertEquals(.001,sCalc.getDoubleResult() , 0); + + } + + @Test + public void switchSignTest1(){ + + sCalc.switchSign((double) 100); + + Assert.assertEquals(-100,sCalc.getDoubleResult() , 0); + + } + + @Test + public void switchSignTest2(){ + + sCalc.switchSign((double) -10); + + Assert.assertEquals(10,sCalc.getDoubleResult() , 0); + + } + + @Test + public void switchSignTest3(){ + + sCalc.switchSign((double) -5000); + + Assert.assertEquals(5000,sCalc.getDoubleResult() , 0); + + } + + @Test + public void sineTest1(){ + + sCalc.sine((double) 10); + + Assert.assertEquals(-0.5440211108893698,sCalc.getDoubleResult() , .1); + + } + + @Test + public void sineTest2(){ + + sCalc.sine((double) 100); + + Assert.assertEquals(-0.50636564111,sCalc.getDoubleResult() , .1); + + } + + @Test + public void sineTest3(){ + + sCalc.sine((double) 50); + + Assert.assertEquals(-0.2623748537,sCalc.getDoubleResult() , .1); + + } + + @Test + public void cosineTest1(){ + + sCalc.cosine((double) 10); + + Assert.assertEquals(-0.83907152907,sCalc.getDoubleResult() , .1); + + } + + @Test + public void cosineTest2(){ + + sCalc.cosine((double) 50); + + Assert.assertEquals(0.96496602849,sCalc.getDoubleResult() , .1); + + } + + @Test + public void cosineTest3(){ + + sCalc.cosine((double) 100); + + Assert.assertEquals(0.86231887228,sCalc.getDoubleResult() , .1); + + } + + @Test + public void tangentTest1(){ + + sCalc.tangent((double) 10); + + Assert.assertEquals(0.64836082745,sCalc.getDoubleResult() , .1); + + } + + @Test + public void tangentTest2(){ + + sCalc.tangent((double) 50); + + Assert.assertEquals(-0.27190061199,sCalc.getDoubleResult() , .1); + + } + + @Test + public void tangentTest3(){ + + sCalc.tangent((double) 100); + + Assert.assertEquals(-0.58721391515,sCalc.getDoubleResult() , 0.1); + + } + + @Test + public void inverseSineTest1(){ + + sCalc.inverseSine((double) 1); + + Assert.assertEquals(1.57079633,sCalc.getDoubleResult() , .1); + + } + + @Test + public void inverseSineTest2(){ + + sCalc.inverseSine((double) .5); + + Assert.assertEquals(0.52359878,sCalc.getDoubleResult() , .1); + + } + + @Test + public void inverseSineTest3(){ + + sCalc.inverseSine((double) .8); + + Assert.assertEquals(0.92729522,sCalc.getDoubleResult() , .1); + + } + + @Test + public void inverseCosineTest1(){ + + sCalc.inverseCosine((double) 1); + + Assert.assertEquals(0,sCalc.getDoubleResult() , .1); + + } + + @Test + public void inverseCosineTest2(){ + + sCalc.inverseCosine((double) .5); + + Assert.assertEquals(1,sCalc.getDoubleResult() , .1); + + } + + @Test + public void inverseCosineTest3(){ + + sCalc.inverseCosine((double) .8); + + Assert.assertEquals(.6,sCalc.getDoubleResult() , .1); + + } + + @Test + public void inverseTangentTest1(){ + + sCalc.inverseTangent((double) .8); + + Assert.assertEquals(0.67474094,sCalc.getDoubleResult() , .1); + + } + + @Test + public void inverseTangentTest2(){ + + sCalc.inverseTangent((double) 1); + + Assert.assertEquals(0.78539816,sCalc.getDoubleResult() , .1); + + } + + @Test + public void inverseTangentTest3(){ + + sCalc.inverseTangent((double) .6); + + Assert.assertEquals(0.5404195,sCalc.getDoubleResult() , .1); + + } + + @Test + public void FactorialTest1(){ + + sCalc.factorial((double) 10); + + Assert.assertEquals(3628800,sCalc.getDoubleResult() , 0); + + } + + @Test + public void FactorialTest2(){ + + sCalc.factorial((double) 5); + + Assert.assertEquals(120,sCalc.getDoubleResult() , 0); + + } + + @Test + public void FactorialTest3(){ + + sCalc.factorial((double) 8); + + Assert.assertEquals(40320,sCalc.getDoubleResult() , 0); + + } + + @Test + public void logTest1(){ + + sCalc.log((double) 10); + + Assert.assertEquals(2.303,sCalc.getDoubleResult() , 0); + + } + + @Test + public void logTest2(){ + + sCalc.log((double) 25); + + Assert.assertEquals(3.219,sCalc.getDoubleResult() , 0); + + } + + @Test + public void logTest3(){ + + sCalc.log((double) 5); + + Assert.assertEquals(1.609,sCalc.getDoubleResult() , 0); + + } + + @Test + public void exponentTest1(){ + + sCalc.exponent((double) 10, 2.1); + + Assert.assertEquals(125.893,sCalc.getDoubleResult() , 0); + + } + + @Test + public void exponentTest2(){ + + sCalc.exponent((double) 10, (double) 0); + + Assert.assertEquals(1,sCalc.getDoubleResult() , 0); + + } + + @Test + public void exponentTest3(){ + + sCalc.exponent((double) 0, (double)( 6)); + + Assert.assertEquals(0,sCalc.getDoubleResult() , 0); + + } + + @Test + public void inverselogTest1(){ + + sCalc.inverselog((double) 10); + + Assert.assertEquals(22026.4658,sCalc.getDoubleResult() , 0.1); + + } + + @Test + public void inverselogTest2(){ + + sCalc.inverselog((double) 15); + + Assert.assertEquals(3269017.37,sCalc.getDoubleResult() , .1); + + } + + @Test + public void inverselogTest3(){ + + sCalc.inverselog((double) 5); + + Assert.assertEquals(148.413,sCalc.getDoubleResult() , .1); + + } + @Test + public void log10Test1(){ + + sCalc.log10((double) 5); + + Assert.assertEquals(0.6989700043360187,sCalc.getDoubleResult() , .1); + + } + + @Test + public void log10Test2(){ + + sCalc.log10((double) 10); + + Assert.assertEquals(1,sCalc.getDoubleResult() , .1); + + } + + @Test + public void log10Test3(){ + + sCalc.log10((double) 15); + + Assert.assertEquals(1.1760912590556811,sCalc.getDoubleResult() , .1); + + } + + @Test + public void inverseLog10Test1(){ + + sCalc.inverseLog10((double) 5); + + Assert.assertEquals(100000,sCalc.getDoubleResult() , .1); + + } + + @Test + public void inverseLog10Test2(){ + + sCalc.inverseLog10((double) 1); + + Assert.assertEquals(10,sCalc.getDoubleResult() , .1); + + } + + @Test + public void inverseLog10Test3(){ + + sCalc.inverseLog10((double) 8); + + Assert.assertEquals(100000000,sCalc.getDoubleResult() , .1); + + } + +}