diff --git a/pom.xml b/pom.xml index e7cb4f6b..42867636 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 + + 8 + 8 + + + + + + + junit + junit + 4.12 + test + + + org.junit.jupiter + junit-jupiter + RELEASE + test + + \ No newline at end of file diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java index 83f0e97f..04363c12 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java @@ -15,18 +15,42 @@ public static void println(String output, Object... args) { print(output + "\n", args); } - public static String getStringInput(String prompt) { - Scanner scanner = new Scanner(System.in); - println(prompt); - String userInput = scanner.nextLine(); - return userInput; + public static String getStringInput(String prompt) + { + Scanner username = new Scanner(System.in); + println (prompt); + String user = username.nextLine(); + System.out.println("Hello " + user + "!"); + return user; } - public static Integer getIntegerInput(String prompt) { - return null; + public static Double getDoubleInput(String prompt) + { + Scanner number = new Scanner(System.in); + println (prompt); + Double num = number.nextDouble(); + return num; } - public static Double getDoubleInput(String prompt) { - return null; + public static String getOperationInput(String prompt) + { + Scanner operation = new Scanner(System.in); + println (prompt); + String ops = operation.nextLine(); + return ops; } + + + + + + + + //public static Integer getIntegerInput(String prompt) { + //return null; } + +//public static Double getDoubleInput(String prompt) { +//return null; +//} +//} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Features.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Features.java new file mode 100644 index 00000000..69fd1a98 --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Features.java @@ -0,0 +1,57 @@ +package com.zipcodewilmington.scientificcalculator; + +import java.util.ArrayList; +import java.util.List; + +import static java.lang.Math.pow; +import static java.lang.Math.sqrt; + +public class Features{ + + public Features() { + } + + public double add(double a, double b) { + return a + b; + } + + public double subtract(double a, double b) { + return a - b; + } + + public double multiply(double a, double b) { + return a * b; + } + + public double divide(double a, double b) { + return a / b; + } + + public double square(double a) { + return a * a; + } + + public double root(double a) { + return sqrt(a); + } + + public double exponent(double a, double b) { + return pow(a, b); + } + + public double inverse(double a) { + return 1 / a; + } + + public double invert(double a) { + return a * -1; + } + + public int clear() { + return 0; + } + + + +} + diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java index 5f421325..9d5c0a21 100644 --- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java @@ -1,17 +1,623 @@ package com.zipcodewilmington.scientificcalculator; +import java.util.Scanner; + +import static java.lang.Integer.*; + /** * 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); + + Shapes shapes = new Shapes(); + Features f = new Features(); + SciFeatures sf = new SciFeatures(); + sf.displayMode = "decimal"; + sf.unitsMode = "Degrees"; + + Console.println("Designed by Chris, Maurice, and Ujjwal."); + Console.println("Welcome to the surprisely working calculator!"); + + String s = Console.getStringInput("Please enter your name?"); + boolean eternal = true; + while (eternal) { + int display = 0; + int r = 1; + do { + try { + Double calcDouble = Console.getDoubleInput("Press 1 for base calculator, 2 for scientific calculator, or 3 for shapes!"); + + if (calcDouble == 1) { + Console.println("Current number is: " + display); + + //Double i = Console.getDoubleInput("Please enter another number if you want:"); + + boolean check = true; + while (check) { + do { + try { + r = 1; + Double i = Console.getDoubleInput("Please enter another number if you want:"); + String op = Console.getOperationInput("Enter an operation : +, - , *, /, ^2, ^y, inverse, sqrt, invert, c, end : "); + if (op.equals("+")) { + Double i1 = Console.getDoubleInput("Please enter next number:"); + Console.println("New Value is: " + f.add(i, i1)); + i = f.add(i, i1); + } else if (op.equals("-")) { + Double i1 = Console.getDoubleInput("Please enter next number:"); + Console.println("New Value is: " + f.subtract(i, i1)); + } else if (op.equals("*")) { + Double i1 = Console.getDoubleInput("Please enter next number:"); + Console.println("New Value is: " + f.multiply(i, i1)); + } else if (op.equals("/")) { + Double i1 = Console.getDoubleInput("Please enter next number:"); + if (i1 == 0) { + Console.println("Err"); + } else { + Console.println("New Value is: " + f.divide(i, i1)); + } + } else if (op.equals("^y")) { + Double i1 = Console.getDoubleInput("Please enter next number:"); + Console.println("New Value is: " + f.exponent(i, i1)); + + } else if (op.equals("^2")) { + Console.println("New Value is: " + f.square(i)); + } else if (op.equals("sqrt")) { + if (i < 0) { + Console.println("Err"); + } else { + Console.println("New Value is: " + f.root(i)); + } + } else if (op.equals("inverse")) { + if (i == 0) { + Console.println("Err"); + } else { + Console.println("New Value is: " + f.inverse(i)); + } + } else if (op.equals("invert")) { + Console.println("New Value is: " + f.invert(i)); + } else if (op.equals("c")) { + Console.println("New Value is: " + f.clear()); + i = 0.0; + } else if (op.equals("end")) { + check = false; + r++; + } else { + Console.println("Err"); + } + + } catch (Exception e) { + + } + } while (r == 1); + } + } else if (calcDouble == 2) { + Console.println("The current modes are decimal and Degrees."); + Console.println("Current number is: " + display); + + //Double i = Console.getDoubleInput("Please enter another number if you want:"); + + boolean check = true; + while (check) { + do { + try { + r = 1; + Double i = Console.getDoubleInput("Please enter another number if you want."); + String op = Console.getOperationInput("Enter an operation : +, - , *, /, ^2, ^y, inverse, sqrt, invert, display(), display, M+, MC, MRC, Sine, Cosine,\n" + + " Tangent, invSine, invCosine, invTangent, units(), units, log, 10^x, Ln, e^x, !, c, end(to exit) : "); + if (op.equals("+")) { + Double i1 = Console.getDoubleInput("Please enter next number:"); + if (sf.displayMode.equalsIgnoreCase("decimal")){ + Console.println("New Value is: " + f.add(i, i1)); + i = f.add(i, i1); + } + else if (sf.displayMode.equalsIgnoreCase("binary")){ + int j = (int) Math.round(f.add(i,i1)); + String str = toBinaryString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("hexadecimal")){ + int j = (int) Math.round(f.add(i,i1)); + String str = toHexString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("octal")){ + int j = (int) Math.round(f.add(i,i1)); + String str = toOctalString(j); + Console.println("New Value is: " + str); + } + } else if (op.equals("-")) { + Double i1 = Console.getDoubleInput("Please enter next number:"); + if (sf.displayMode.equalsIgnoreCase("decimal")){ + Console.println("New Value is: " + f.subtract(i, i1)); + i = f.subtract(i, i1); + } + else if (sf.displayMode.equalsIgnoreCase("binary")){ + int j = (int) Math.round(f.subtract(i,i1)); + String str = toBinaryString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("hexadecimal")){ + int j = (int) Math.round(f.subtract(i,i1)); + String str = toHexString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("octal")){ + int j = (int) Math.round(f.subtract(i,i1)); + String str = toOctalString(j); + Console.println("New Value is: " + str); + } + } else if (op.equals("*")) { + Double i1 = Console.getDoubleInput("Please enter next number:"); + if (sf.displayMode.equalsIgnoreCase("decimal")){ + Console.println("New Value is: " + f.multiply(i, i1)); + i = f.multiply(i, i1); + } + else if (sf.displayMode.equalsIgnoreCase("binary")){ + int j = (int) Math.round(f.multiply(i,i1)); + String str = toBinaryString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("hexadecimal")){ + int j = (int) Math.round(f.multiply(i,i1)); + String str = toHexString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("octal")){ + int j = (int) Math.round(f.multiply(i,i1)); + String str = toOctalString(j); + Console.println("New Value is: " + str); + } + } else if (op.equals("/")) { + Double i1 = Console.getDoubleInput("Please enter next number:"); + if (i1 == 0) { + Console.println("Err"); + } else { + if (sf.displayMode.equalsIgnoreCase("decimal")){ + Console.println("New Value is: " + f.divide(i, i1)); + i = f.divide(i,i1); + } + else if (sf.displayMode.equalsIgnoreCase("binary")){ + int j = (int) Math.round(f.divide(i,i1)); + String str = toBinaryString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("hexadecimal")){ + int j = (int) Math.round(f.divide(i,i1)); + String str = toHexString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("octal")){ + int j = (int) Math.round(f.divide(i,i1)); + String str = toOctalString(j); + Console.println("New Value is: " + str); + } + } + } else if (op.equals("^y")) { + Double i1 = Console.getDoubleInput("Please enter next number:"); + if (sf.displayMode.equalsIgnoreCase("decimal")){ + Console.println("New Value is: " + f.exponent(i, i1)); + i = f.exponent(i,i1); + } + else if (sf.displayMode.equalsIgnoreCase("binary")){ + int j = (int) Math.round(f.exponent(i,i1)); + String str = toBinaryString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("hexadecimal")){ + int j = (int) Math.round(f.exponent(i,i1)); + String str = toHexString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("octal")){ + int j = (int) Math.round(f.exponent(i,i1)); + String str = toOctalString(j); + Console.println("New Value is: " + str); + } + } else if (op.equals("^2")) { + if (sf.displayMode.equalsIgnoreCase("decimal")){ + Console.println("New Value is: " + f.square(i)); + i = f.square(i); + } + else if (sf.displayMode.equalsIgnoreCase("binary")){ + int j = (int) Math.round(f.square(i)); + String str = toBinaryString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("hexadecimal")){ + int j = (int) Math.round(f.square(i)); + String str = toHexString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("octal")){ + int j = (int) Math.round(f.square(i)); + String str = toOctalString(j); + Console.println("New Value is: " + str); + } + } else if (op.equals("sqrt")) { + if (i < 0) { + Console.println("Err"); + } else { + if (sf.displayMode.equalsIgnoreCase("decimal")){ + Console.println("New Value is: " + f.root(i)); + i = f.root(i); + } + else if (sf.displayMode.equalsIgnoreCase("binary")){ + int j = (int) Math.round(f.root(i)); + String str = toBinaryString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("hexadecimal")){ + int j = (int) Math.round(f.root(i)); + String str = toHexString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("octal")){ + int j = (int) Math.round(f.root(i)); + String str = toOctalString(j); + Console.println("New Value is: " + str); + } + } + } else if (op.equals("inverse")) { + if (i == 0) { + Console.println("Err"); + } else { + if (sf.displayMode.equalsIgnoreCase("decimal")){ + Console.println("New Value is: " + f.inverse(i)); + i = f.inverse(i); + } + else if (sf.displayMode.equalsIgnoreCase("binary")){ + int j = (int) Math.round(f.inverse(i)); + String str = toBinaryString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("hexadecimal")){ + int j = (int) Math.round(f.inverse(i)); + String str = toHexString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("octal")){ + int j = (int) Math.round(f.inverse(i)); + String str = toOctalString(j); + Console.println("New Value is: " + str); + } + } + } else if (op.equals("invert")) { + if (sf.displayMode.equalsIgnoreCase("decimal")){ + Console.println("New Value is: " + f.invert(i)); + i = f.invert(i); + } + else if (sf.displayMode.equalsIgnoreCase("binary")){ + int j = (int) Math.round(f.invert(i)); + String str = toBinaryString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("hexadecimal")){ + int j = (int) Math.round(f.invert(i)); + String str = toHexString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("octal")){ + int j = (int) Math.round(f.invert(i)); + String str = toOctalString(j); + Console.println("New Value is: " + str); + } + } else if (op.equals("c")) { + Console.println("New Value is: " + f.clear()); + i = 0.0; + } else if (op.equals("end")) { + check = false; + r++; + } else if (op.equals("display()")) { + int t = 1; + do { + try { + String imp = Console.getOperationInput("Type binary, octal, decimal, hexadecimal"); + if (imp.equalsIgnoreCase("binary")) { + Console.println("The display is now binary."); + sf.switchDisplayMode("binary"); + } else if (imp.equalsIgnoreCase("octal")) { + Console.println("The display is now octal."); + sf.switchDisplayMode("octal"); + } else if (imp.equalsIgnoreCase("decimal")) { + Console.println("The display is now decimal."); + sf.switchDisplayMode("decimal"); + } else if (imp.equalsIgnoreCase("hexadecimal")) { + Console.println("The display is now hexadecimal."); + sf.switchDisplayMode("hexadecimal"); + } else { + Console.println("Invalid entry. \nWhy are you the way that you are?\nDo it right next time >:("); + } + t++; + } catch (Exception e) { + Console.println("Why are you the way that you are?"); + } + } while (t == 1); + } else if (op.equals("display")) { + if (sf.displayMode.equalsIgnoreCase("binary")){ + Console.println("The display is now octal."); + sf.switchDisplayMode(); + } + else if (sf.displayMode.equalsIgnoreCase("octal")){ + Console.println("The display is now decimal."); + sf.switchDisplayMode(); + } + else if (sf.displayMode.equalsIgnoreCase("decimal")){ + Console.println("The display is now hexadecimal."); + sf.switchDisplayMode(); + } + else { + Console.println("The display is now binary."); + sf.switchDisplayMode(); + } + } else if (op.equals("M+")) { + sf.storeMemory(i); + } else if (op.equals("MC")) { + sf.clearMemory(); + } else if (op.equals("MRC")) { + Console.println("" + sf.memoryRecall()); + i = sf.memoryRecall(); + } else if (op.equals("Sine")) { + if (sf.unitsMode == "Degrees") { + Console.println("New Value is: " + sf.sine(i)); + i = sf.sine(i); + } else { + Console.println("New Value is: " + sf.sine(Math.toRadians(i))); + i = sf.sine(Math.toRadians(i)); + } + } else if (op.equals("Cosine")) { + if (sf.unitsMode == "Degrees") { + Console.println("New Value is: " + sf.cosine(i)); + i = sf.cosine(i); + } else { + Console.println("New Value is: " + sf.cosine(Math.toRadians(i))); + i = sf.cosine(Math.toRadians(i)); + } + } else if (op.equals("Tangent")) { + if (sf.unitsMode == "Degrees") { + Console.println("New Value is: " + sf.tangent(i)); + i = sf.tangent(i); + } else { + Console.println("New Value is: " + sf.tangent(Math.toRadians(i))); + i = sf.tangent(Math.toRadians(i)); + } + } else if (op.equals("invSine")) { + if (i < -1 || i > 1) { + Console.println("Err"); + } else { + if (sf.unitsMode == "Degrees") { + Console.println("New Value is: " + sf.sineInverse(i)); + i = sf.sineInverse(i); + } else { + Console.println("New Value is: " + sf.sineInverse(Math.toRadians(i))); + i = sf.sineInverse(Math.toRadians(i)); + } + } + } else if (op.equals("invCosine")) { + if (i < -1 || i > 1) { + Console.println("Err"); + } else { + if (sf.unitsMode == "Degrees") { + Console.println("New Value is: " + sf.cosineInverse(i)); + i = sf.cosineInverse(i); + } else { + Console.println("New Value is: " + sf.cosineInverse(Math.toRadians(i))); + i = sf.cosineInverse(Math.toRadians(i)); + } + + } + } else if (op.equals("invTangent")) { + if (sf.unitsMode == "Degrees") { + Console.println("New Value is: " + sf.tangentInverse(i)); + i = sf.tangentInverse(i); + } else { + Console.println("New Value is: " + sf.tangentInverse(Math.toRadians(i))); + i = sf.tangentInverse(Math.toRadians(i)); + } + } else if (op.equals("units()")){ + int p = 1; + do { + try { + String useful = Console.getOperationInput("Enter degrees or radians"); + if (useful.equalsIgnoreCase("degrees")){ + Console.println("The mode is now Radians."); + sf.switchUnitsMode("Degrees"); + } + else if (useful.equalsIgnoreCase("radians")) { + Console.println("The mode is now Degrees."); + sf.switchUnitsMode("Radians"); + } + p++; + } catch (Exception e){ + Console.println("Err"); + } + } while (p == 1); + } else if (op.equals("units")){ + if (sf.unitsMode.equalsIgnoreCase("Degrees")){ + Console.println("The mode is now Radians."); + sf.switchUnitsMode();; + } + else { + Console.println("The mode is now Degrees."); + sf.switchUnitsMode(); + } + } + else if (op.equals("log")) { + if (i <= 0) { + Console.println("Err"); + } else { + if (sf.displayMode.equalsIgnoreCase("decimal")){ + Console.println("New Value is: " + sf.logarithm(i)); + i = sf.logarithm(i); + } + else if (sf.displayMode.equalsIgnoreCase("binary")){ + int j = (int) Math.round(sf.logarithm(i)); + String str = toBinaryString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("hexadecimal")){ + int j = (int) Math.round(sf.logarithm(i)); + String str = toHexString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("octal")){ + int j = (int) Math.round(sf.logarithm(i)); + String str = toOctalString(j); + Console.println("New Value is: " + str); + } + } + } else if (op.equals("10^x")) { + if (sf.displayMode.equalsIgnoreCase("decimal")){ + Console.println("New Value is: " + sf.inverseLog(i)); + i = sf.inverseLog(i); + } + else if (sf.displayMode.equalsIgnoreCase("binary")){ + int j = (int) Math.round(sf.inverseLog(i)); + String str = toBinaryString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("hexadecimal")){ + int j = (int) Math.round(sf.inverseLog(i)); + String str = toHexString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("octal")){ + int j = (int) Math.round(sf.inverseLog(i)); + String str = toOctalString(j); + Console.println("New Value is: " + str); + } + } else if (op.equals("Ln")) { + if (i <= 0) { + Console.println("Err"); + } else { + if (sf.displayMode.equalsIgnoreCase("decimal")){ + Console.println("New Value is: " + sf.naturalLog(i)); + i = sf.naturalLog(i); + } + else if (sf.displayMode.equalsIgnoreCase("binary")){ + int j = (int) Math.round(sf.naturalLog(i)); + String str = toBinaryString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("hexadecimal")){ + int j = (int) Math.round(sf.naturalLog(i)); + String str = toHexString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("octal")){ + int j = (int) Math.round(sf.naturalLog(i)); + String str = toOctalString(j); + Console.println("New Value is: " + str); + } + } + } else if (op.equals("e^x")) { + if (sf.displayMode.equalsIgnoreCase("decimal")){ + Console.println("New Value is: " + sf.inverseNaturalLog(i)); + i = sf.inverseNaturalLog(i); + } + else if (sf.displayMode.equalsIgnoreCase("binary")){ + int j = (int) Math.round(sf.inverseNaturalLog(i)); + String str = toBinaryString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("hexadecimal")){ + int j = (int) Math.round(sf.inverseNaturalLog(i)); + String str = toHexString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("octal")){ + int j = (int) Math.round(sf.inverseNaturalLog(i)); + String str = toOctalString(j); + Console.println("New Value is: " + str); + } + } else if (op.equals("!")) { + int factor = Math.toIntExact(Math.round(i)); + if (sf.displayMode.equalsIgnoreCase("decimal")){ + Console.println("New Value is: " + sf.factorial(factor)); + i = (double) sf.factorial(factor); + } + else if (sf.displayMode.equalsIgnoreCase("binary")){ + int j = (int) Math.round(sf.factorial(factor)); + String str = toBinaryString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("hexadecimal")){ + int j = (int) Math.round(sf.factorial(factor)); + String str = toHexString(j); + Console.println("New Value is: " + str); + } + else if (sf.displayMode.equalsIgnoreCase("octal")){ + int j = (int) Math.round(sf.factorial(factor)); + String str = toOctalString(j); + Console.println("New Value is: " + str); + } + } else { + Console.println("Err"); + } + + } catch (Exception e) { + + } + } while (r == 1); + } + } else if (calcDouble == 3) { + boolean check = true; + while (check) { + do { + try { + r = 1; + String op = Console.getOperationInput("Enter a shape you wish to work with: quad, circle, triangle or end(to exit)"); + if (op.equalsIgnoreCase("quad")) { + String info = Console.getOperationInput("Press r to return or continue to quadrilateral area with any key."); + if (info.equalsIgnoreCase("r")) { + } else { + double w = Console.getDoubleInput("Enter the width"); + double l = Console.getDoubleInput("Enter the length"); + if (w < 0 || l < 0) { + Console.println("Err"); + } else { + Console.println("The area of your quadrilateral is: " + shapes.quadArea(w, l)); + } + } + } else if (op.equalsIgnoreCase("triangle")) { + String info1 = Console.getOperationInput("Press r to return or continue to triangle area with any key."); + if (info1.equalsIgnoreCase("r")) { + } else { + double w = Console.getDoubleInput("Enter the width"); + double h = Console.getDoubleInput("Enter the height"); + if (w < 0 || h < 0) { + Console.println("Err"); + } else { + Console.println("The area of your triangle is: " + shapes.triangleArea(w, h)); + } + } + } else if (op.equalsIgnoreCase("circle")) { + String info2 = Console.getOperationInput(("Press r to return or continue to circle area with any key.")); + if (info2.equalsIgnoreCase("r")) { + } else { + double radius = Console.getDoubleInput("Enter the radius"); + if (radius < 0) { + Console.println("Err"); + } else { + Console.println("The area of your circle is " + shapes.circleArea(radius)); + } + } + } else if (op.equalsIgnoreCase("end")) { + check = false; + r++; + } else { } + + } catch (Exception e) { + Console.println("Err"); + } + } while (r == 1); + } + } + } catch (Exception e) { + Console.println("Err"); + } + } while (r == 1); + } } } diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/SciFeatures.java b/src/main/java/com/zipcodewilmington/scientificcalculator/SciFeatures.java new file mode 100644 index 00000000..057c579a --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/SciFeatures.java @@ -0,0 +1,124 @@ +package com.zipcodewilmington.scientificcalculator; + + +import java.util.Stack; + +import static java.lang.Math.*; + +public class SciFeatures { + + + public SciFeatures(){ + } + + public String unitsMode; + public String displayMode; + + public double p; + + public void setDisplayMode(String a) { + this.displayMode = a; + } + public String getDisplayMode(){ + return this.displayMode; + } + + public void switchDisplayMode() { + if (displayMode.equals("binary")) { + displayMode = "octal"; + } else if (displayMode.equals("octal")) { + displayMode = "decimal"; + } else if (displayMode.equals("decimal")) { + displayMode = "hexadecimal"; + } else { + displayMode = "binary"; + } + } + + + + public void switchDisplayMode(String mode) { + displayMode = mode; + } + + public void storeMemory(double a) { + this.p = a; + } + + public void clearMemory() { + this.p = Double.NaN; + } + + public double memoryRecall(){ + return this.p; + } + + public double sine(double a) { + return sin(a); + } + + public double cosine(double a) { + return cos(a); + } + + public double tangent(double a) { + return tan(a); + } + + public double sineInverse(double a) { + return asin(a); + } + + public double cosineInverse(double a) { + return acos(a); + } + + public double tangentInverse(double a) { + return atan(a); + } + + public double toDegree(double a){ + return toDegrees(a); + } + + public void switchUnitsMode() { + if (unitsMode.equals("Degrees")) { + unitsMode = "Radians"; + } + else { + unitsMode = "Degrees"; + } + } + + public void switchUnitsMode(String mode){ + unitsMode = mode; + } + + public double logarithm(double a) { + return log10(a); + } + + public double inverseLog(double a) { + return Math.pow(10, a); + } + + public double naturalLog(double a) { + return Math.log(a); + } + + public double inverseNaturalLog(double a){ + return exp(a); + } + + + + public int factorial(int a) { + int sum = 1; + for (int i = 2; i <= a; i++){ + sum *= i; + } + return sum; + } + + +} diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Shapes.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Shapes.java new file mode 100644 index 00000000..ef2edefb --- /dev/null +++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Shapes.java @@ -0,0 +1,22 @@ +package com.zipcodewilmington.scientificcalculator; + +public class Shapes { + + public Shapes(){} + + //circle + public double circleArea(double r){ + return Math.PI * Math.pow(r, 2); + } + + //triangle + public double triangleArea(double w, double h){ + return 0.5 * w * h; + } + + //quad + public double quadArea(double w, double l){ + return w * l; + } + +} diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/FeaturesTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/FeaturesTest.java new file mode 100644 index 00000000..222c05a2 --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/FeaturesTest.java @@ -0,0 +1,108 @@ +package com.zipcodewilmington.scientific_calculator; + +import com.zipcodewilmington.scientificcalculator.Features; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class FeaturesTest { + Features test = new Features(); + + @Test + public void addTest1() { + assertEquals(5, test.add(2, 3), 0.01); + } + + @Test + public void addTest2() { + assertEquals(5.5, test.add(3, 2.5), 0.01); + } + + @Test + public void subTest1() { + assertEquals(10, test.subtract(13, 3), 0.01); + } + + @Test + public void subTest2() { + assertEquals(2.2, test.subtract(5.2, 3), 0.01); + } + + @Test + public void multTest1() { + assertEquals(12, test.multiply(6.0, 2), 0.01); + } + + @Test + public void multiTest2() { + assertEquals(100, test.multiply(1, 100), 0.01); + } + + @Test + public void divTest1() { + assertEquals(8, test.divide(32, 4), 0.01); + } + + @Test + public void divTest2(){ + assertEquals(5, test.divide(40, 8), 0.01); + } + @Test + public void squareTest1(){ + assertEquals(25, test.square( 5), 0.01); + } + @Test + public void squareTest2(){ + assertEquals(144, test.square( 12),0.01); + } + @Test + public void rootTest1(){ + assertEquals(8, test.root( 64), 0.01); + } + @Test + public void rootTest2(){ + assertEquals(2, test.root( 4), 0.01); + } + @Test + public void exponentTest1(){ + assertEquals(32, test.exponent(2, 5), 0.01); + } + @Test + public void exponentTest2(){ + assertEquals(282475249, test.exponent(7, 10), 0.01); + } + @Test + public void invertTest1(){ + assertEquals(10, + test.invert( -10), + 0.01); + } + @Test + public void invertTest2(){ + assertEquals(-20, + test.invert( 20), + 0.01); + } + @Test + public void clearTest1(){ + assertEquals(0, + test.clear(), + 0.01); + } + @Test + public void clearTest2(){ + assertEquals(0, + test.clear(), + 0.01); + } + @Test + public void inverseTest1(){ + assertEquals(0.25, test.inverse( 4), 0.01); + } + + @Test + public void inverseTest2(){ + assertEquals(.33, test.inverse(3), 0.01); + } + +} diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/SciFeaturesTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/SciFeaturesTest.java new file mode 100644 index 00000000..e8d44154 --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/SciFeaturesTest.java @@ -0,0 +1,157 @@ +package com.zipcodewilmington.scientific_calculator; + +import com.zipcodewilmington.scientificcalculator.SciFeatures; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +public class SciFeaturesTest { + SciFeatures test = new SciFeatures(); + + @Test + public void switchTest(){ + test.setDisplayMode("decimal"); + test.switchDisplayMode(); + assertEquals("hexadecimal", test.getDisplayMode()); + } + @Test + public void switchTest1(){ + test.setDisplayMode("binary"); + test.switchDisplayMode(); + assertEquals("octal", test.getDisplayMode()); + } + @Test + public void switchTest2(){ + test.setDisplayMode("hexadecimal"); + test.switchDisplayMode(); + assertEquals("binary", test.getDisplayMode()); + } + @Test + public void switchTest3(){ + test.setDisplayMode("octal"); + test.switchDisplayMode(); + assertEquals("decimal", test.getDisplayMode()); + } + @Test + public void switchTest4(){ + test.setDisplayMode("decimal"); + test.switchDisplayMode("hexadecimal"); + assertEquals("hexadecimal", test.getDisplayMode()); + } + + @Test + public void memoryTest(){ + test.storeMemory(2); + assertEquals(2, test.memoryRecall(), 0.01); + } + + @Test + public void sinTest(){ + assertEquals(Math.sin(90), test.sine(90), 0.01); + } + @Test + public void cosTest(){ + assertEquals(Math.cos(90), test.cosine(90), 0.01); + } + @Test + public void tanTest(){ + assertEquals(Math.tan(45), test.tangent(45), 0.01); + } + @Test + public void invSinTest(){ + assertEquals(Math.asin(15), test.sineInverse(15), 0.01); + } + @Test + public void invCosTest(){ + assertEquals(Math.acos(15), test.cosineInverse(15), 0.01); + } + @Test + public void invTanTest(){ + assertEquals(Math.atan(15), test.tangentInverse(15), 0.01); + } + @Test + public void sinTest1(){ + assertEquals(Math.sin(45), test.sine(45), 0.01); + } + @Test + public void cosTest1(){ + assertEquals(Math.cos(45), test.cosine(45), 0.01); + } + @Test + public void tanTest1(){ + assertEquals(Math.tan(15), test.tangent(15), 0.01); + } + @Test + public void invSinTest1(){ + assertEquals(Math.asin(30), test.sineInverse(30), 0.01); + } + @Test + public void invCosTest1(){ + assertEquals(Math.acos(30), test.cosineInverse(30), 0.01); + } + @Test + public void invTanTest1(){ + assertEquals(Math.atan(30), test.tangentInverse(30), 0.01); + } + @Test + public void unitModeTest(){ + test.unitsMode = "Degrees"; + test.switchUnitsMode(); + assertEquals("Radians", test.unitsMode); + } + @Test + public void unitModeTest1(){ + test.unitsMode = "Radians"; + test.switchUnitsMode(); + assertEquals("Degrees", test.unitsMode); + } + @Test + public void unitModeInpTest(){ + test.switchUnitsMode("Degrees"); + assertEquals("Degrees", test.unitsMode); + } + @Test + public void unitModeInpTest1(){ + test.switchUnitsMode("Radians"); + assertEquals("Radians", test.unitsMode); + } + @Test + public void logTest(){ + assertEquals(3, test.logarithm(1000), 0.01); + } + @Test + public void logTest1(){ + assertEquals(4, test.logarithm(10000), 0.01); + } + @Test + public void invLogTest(){ + assertEquals(10, test.inverseLog(1), 0.01); + } + @Test + public void invLogTest1(){ + assertEquals(100, test.inverseLog(2), 0.01); + } + @Test + public void natLogTest(){ + assertEquals(Math.log(3), test.naturalLog(3), 0.01); + } + @Test + public void natLogTest1(){ + assertEquals(Math.log(10), test.naturalLog(10), 0.01); + } + @Test + public void invNatLogTest(){ + assertEquals(Math.exp(5), test.inverseNaturalLog(5), 0.01); + } + @Test + public void invNatLogTest1(){ + assertEquals(Math.exp(2.5), test.inverseNaturalLog(2.5), 0.01); + } + @Test + public void factorialTest(){ + assertEquals(24, test.factorial(4), 0.01); + } + @Test + public void factorialTest1(){ + assertEquals(5040, test.factorial(7), 0.01); + } +} diff --git a/src/test/java/com/zipcodewilmington/scientific_calculator/ShapesTest.java b/src/test/java/com/zipcodewilmington/scientific_calculator/ShapesTest.java new file mode 100644 index 00000000..adbe6d5a --- /dev/null +++ b/src/test/java/com/zipcodewilmington/scientific_calculator/ShapesTest.java @@ -0,0 +1,40 @@ +package com.zipcodewilmington.scientific_calculator; + +import com.zipcodewilmington.scientificcalculator.Shapes; +import org.junit.Test; + + +import static org.junit.Assert.assertEquals; + +public class ShapesTest { + Shapes s = new Shapes(); + public ShapesTest(){} + //quad + @Test + public void quadTest(){ + assertEquals(4, s.quadArea(2,2), 0.01); + } + @Test + public void quadTest1(){ + assertEquals(10, s.quadArea(10, 1), 0.01); + } + + //triangle + @Test + public void triTest1(){ + assertEquals(10, s.triangleArea(2,10), 0.01); + } + @Test + public void triTest2(){ + assertEquals(12, s.triangleArea(3, 8), 0.01); + } + //circle + @Test + public void cirTest1(){ + assertEquals(12.56, s.circleArea(2), 0.01); + } + @Test + public void cirTest2(){ + assertEquals(28.27, s.circleArea(3), 0.01); + } +}