diff --git a/pom.xml b/pom.xml
index e7cb4f6..709a27c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,6 +7,10 @@
com.zipcodewilmington
scientific_calculator
1.0-SNAPSHOT
+
+ 18
+ 18
+
\ No newline at end of file
diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator.java
new file mode 100644
index 0000000..daf86fd
--- /dev/null
+++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Calculator.java
@@ -0,0 +1,103 @@
+package com.zipcodewilmington.scientificcalculator;
+
+public class Calculator {
+
+ public static void run() {
+
+ boolean calcOn = true;
+
+ double displayVal = 0.0;
+
+ String displayMode = "decimal";
+
+ System.out.println("Current value: " + displayVal);
+
+ while (calcOn == true) {
+ String operator = Console.getStringInput("Enter the operation you wish to perform: ");
+ if (operator.equals("exit")) break;
+ else if (operator.equals("clear")) displayVal = 0.0;
+ else if (operator.equals("help")) printHelp();
+ else if (operator.equals("switch")) {
+ displayMode = switchDisplayMode(displayMode);
+ System.out.println("The display mode is now " + displayMode + ".");
+ }
+ else if (operator.equals("+")) displayVal = Operation.twoNumOp("+", displayVal);
+ else if (operator.equals("-")) displayVal = Operation.twoNumOp("-", displayVal);
+ else if (operator.equals("*")) displayVal = Operation.twoNumOp("*", displayVal);
+ else if (operator.equals("/")) displayVal = Operation.twoNumOp("/", displayVal);
+ else if (operator.equals("^")) displayVal = Operation.twoNumOp("^", displayVal);
+ else if (operator.equals("sq")) displayVal = Operation.oneNumOp("sq", displayVal);
+ else if (operator.equals("sqrt")) displayVal = Operation.oneNumOp("sqrt", displayVal);
+ else if (operator.equals("inverse")) displayVal = Operation.oneNumOp("inverse", displayVal);
+ else if (operator.equals("invert")) displayVal = Operation.oneNumOp("invert", displayVal);
+ else if (operator.equals("sin")) displayVal = Operation.oneNumOp("sin", displayVal);
+ else if (operator.equals("cos")) displayVal = Operation.oneNumOp("cos", displayVal);
+ else if (operator.equals("tan")) displayVal = Operation.oneNumOp("tan", displayVal);
+ else if (operator.equals("arcsin")) displayVal = Operation.oneNumOp("arcsin", displayVal);
+ else if (operator.equals("arccos")) displayVal = Operation.oneNumOp("arccos", displayVal);
+ else if (operator.equals("arctan")) displayVal = Operation.oneNumOp("arctan", displayVal);
+ else if (operator.equals("log")) displayVal = Operation.oneNumOp("log", displayVal);
+ else if (operator.equals("invlog")) displayVal = Operation.oneNumOp("invlog", displayVal);
+ else if (operator.equals("ln")) displayVal = Operation.oneNumOp("ln", displayVal);
+ else if (operator.equals("e")) displayVal = Operation.oneNumOp("e", displayVal);
+ else if (operator.equals("!")) displayVal = Operation.factorial("!", displayVal);
+ else {
+ System.out.println("Invalid operation, please enter help for a list of operations");
+ continue;
+ }
+
+ printDisplay(displayMode, displayVal);
+ }
+ }
+
+ public static String switchDisplayMode(String currentMode) {
+ // Have the user enter the mode they wish to switch to
+ String s = Console.getStringInput("Enter the display mode you wish to switch to (leave blank to auto-rotate): ");
+ if (s.equals("decimal")) return "decimal";
+ else if (s.equals("hexadecimal")) return "hexadecimal";
+ else if (s.equals("octal")) return "octal";
+ else if (s.equals("binary")) return "binary";
+ // If the user enters blank or enters an invalid display mode, then rotate between the display modes
+ else {
+ if (currentMode.equals("decimal")) return "hexadecimal";
+ else if (currentMode.equals("hexadecimal")) return "binary";
+ else if (currentMode.equals("binary")) return "octal";
+ else return "decimal";
+ }
+ }
+
+ public static void printDisplay(String displayMode, double displayVal) {
+ if (displayMode.equals("octal")) System.out.println("Current value: " + Long.toOctalString(Double.doubleToRawLongBits(displayVal)));
+ else if (displayMode.equals("hexadecimal")) System.out.println("Current value: " + Double.toHexString(displayVal));
+ else if (displayMode.equals("binary")) System.out.println("Current value: " + Long.toBinaryString(Double.doubleToRawLongBits(displayVal)));
+ else System.out.println("Current value: " + displayVal);
+ }
+ // Method that prints out a list of operations
+ public static void printHelp() {
+ System.out.println("Here are the list of operations:");
+ System.out.println("Addition: + ");
+ System.out.println("Subtraction: - ");
+ System.out.println("Multiplication: * ");
+ System.out.println("Division: / ");
+ System.out.println("Square: sq ");
+ System.out.println("Square root: sqrt ");
+ System.out.println("Variable Exponentiation: ^ ");
+ System.out.println("Inverse: inverse ");
+ System.out.println("Invert: invert ");
+ System.out.println("Sine: sin ");
+ System.out.println("Cosine: cos ");
+ System.out.println("Tangent: tan ");
+ System.out.println("Inverse Sine: arcsin ");
+ System.out.println("Inverse Cosine: arccos ");
+ System.out.println("Inverse Tangent: arctan ");
+ System.out.println("Log: log ");
+ System.out.println("Inverse Logarithm: invlog ");
+ System.out.println("Natural Logarithm: ln ");
+ System.out.println("Inverse Natural Logarithm: e ");
+ System.out.println("Factorial: ! ");
+ System.out.println("Switch between display mode: switch ");
+ System.out.println("Four display modes: binary, octal, decimal, hexadecimal ");
+ System.out.println("Clear the display: clear ");
+ System.out.println("Turn off the calculator: exit ");
+ }
+}
diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java
index 83f0e97..085e0f2 100644
--- a/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java
+++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Console.java
@@ -17,7 +17,7 @@ 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;
}
@@ -25,8 +25,4 @@ public static String getStringInput(String prompt) {
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/MainApplication.java b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java
index 5f42132..b8a1ee0 100644
--- a/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java
+++ b/src/main/java/com/zipcodewilmington/scientificcalculator/MainApplication.java
@@ -1,17 +1,10 @@
package com.zipcodewilmington.scientificcalculator;
-/**
- * Created by leon on 2/9/18.
- */
+// Group 5: Linda Li, Nick Choi, Randy Reina
+
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 the calculator made by Linda, Nick and Randy! Please enter help for a list of operations. The default display mode is decimal.");
+ Calculator.run();
}
-}
+}
\ No newline at end of file
diff --git a/src/main/java/com/zipcodewilmington/scientificcalculator/Operation.java b/src/main/java/com/zipcodewilmington/scientificcalculator/Operation.java
new file mode 100644
index 0000000..df90d4f
--- /dev/null
+++ b/src/main/java/com/zipcodewilmington/scientificcalculator/Operation.java
@@ -0,0 +1,147 @@
+package com.zipcodewilmington.scientificcalculator;
+import java.lang.Math;
+import java.sql.SQLOutput;
+
+public class Operation {
+ // Two number operations
+ public static double twoNumOp(String operation, double displayVal) {
+ double num1 = 0, num2 = 0, result = 0;
+
+ // Scan first number and store
+ String s1 = Console.getStringInput("Enter the first number to perform operation (leave blank to use current value): ");
+ if (s1.equals("")) num1 = displayVal;
+ else {
+ try {
+ // Try to parse the user input into a double
+ num1 = Double.parseDouble(s1);
+ } catch (NumberFormatException e){
+ System.out.println("You did not input a valid number. The number has been set to the default value of 0.");
+ }
+ }
+
+ // Scan second number and store
+ String s2 = Console.getStringInput("Enter the second number to perform operation (leave blank to use current value): ");
+ if (s2.equals("")) num2 = displayVal;
+ else {
+ try {
+ // Try to parse the user input into a double
+ num2 = Double.parseDouble(s2);
+ } catch (NumberFormatException e){
+ System.out.println("You did not input a valid number. The number has been set to the default value of 0.");
+ }
+ }
+
+ // Perform the operation
+ if (operation.equals("+")) result = add(num1, num2);
+ else if (operation.equals("-")) result = subtract(num1, num2);
+ else if (operation.equals("*")) result = multiply(num1, num2);
+ else if (operation.equals("/")) result = divide(num1, num2, displayVal);
+ else if (operation.equals("^")) result = power(num1, num2);
+
+ return result;
+ }
+
+ public static double oneNumOp(String operation, double displayVal) {
+ double n = 0, result = 0;
+
+ String s1 = Console.getStringInput("Enter the number to perform operation (leave blank to use current value): ");
+ if (s1.equals("")) n = displayVal;
+ else {
+ try {
+ // Try to parse the user input into a double
+ n = Double.parseDouble(s1);
+ } catch (NumberFormatException e){
+ System.out.println("You did not input a valid number. The number has been set to the default value of 0.");
+ }
+ }
+ if (operation.equals("sq")) result = sq(n);
+ else if (operation.equals("sqrt")) result = sqrt(n, displayVal);
+ else if (operation.equals("inverse")) result = inverse(n);
+ else if (operation.equals("invert")) result = invert(n);
+ else if (operation.equals("log")) result = Math.log10(n);
+ else if (operation.equals("invlog")) result = Math.pow(10,n);
+ else if (operation.equals("ln")) result = Math.log(n);
+ else if (operation.equals("e")) result = Math.exp(n);
+ else if (operation.equals("sin")) result = Math.sin(n);
+ else if (operation.equals("cos")) result = Math.cos(n);
+ else if (operation.equals("tan")) result = Math.tan(n);
+ else if (operation.equals("arcsin")) result = Math.asin(n);
+ else if (operation.equals("arccos")) result = Math.acos(n);
+ else if (operation.equals("arctan")) result = Math.atan(n);
+
+ return result;
+ }
+ // Factorial function
+ public static double factorial(String operation, double displayVal) {
+ double n = 0;
+ String s1 = Console.getStringInput("Enter the number to perform operation (leave blank to use current value): ");
+ if (s1.equals("")) n = displayVal;
+ else {
+ try {
+ // Try to parse the user input into a double
+ n = Double.parseDouble(s1);
+ } catch (NumberFormatException e){
+ System.out.println("You did not input a valid number. The number has been set to the default value of 0.");
+ }
+ }
+ long result = 1;
+ for (int i = 1; i <= n; i++) {
+ result = result * i;
+ }
+ return result;
+ }
+
+ // Addition
+ public static double add(double num1, double num2) {
+
+ return num1 + num2;
+ }
+ // Subtraction
+ public static double subtract(double num1, double num2) {
+
+ return num1 - num2;
+ }
+ // Multiplication
+ public static double multiply(double num1, double num2) {
+
+ return num1 * num2;
+ }
+ // Division
+ // We pass displayVal as a method as a way to store displayVal in "memory"
+ // This way, if an error occurs, we can set the current value displayed on the calculator to the displayVal
+ public static double divide(double num1, double num2, double displayVal) {
+
+ if (num2 == 0.0) {
+ System.out.println("Error: cannot divide by zero, please try again.");
+ return displayVal;
+ } else return num1 / num2;
+ }
+ // Variable exponentiation
+ public static double power(double num1, double num2) {
+
+ return Math.pow(num1, num2);
+ }
+ // Square
+ public static double sq(double n) {
+
+ return n * n;
+ }
+ // Inverse of the number
+ public static double inverse(double n) {
+
+ return 1 / n;
+ }
+ // Invert the sign of the number on the display (switch between positive and negative)
+ public static double invert(double n) {
+
+ return n * -1;
+ }
+
+ public static double sqrt(double n, double displayVal) {
+ if (n < 0.0) {
+ System.out.println("Error: cannot take the square root of a negative number, please try again.");
+ return displayVal;
+ } else return Math.sqrt(n);
+ }
+
+}