Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test1 #4

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<groupId>com.zipcodewilmington</groupId>
<artifactId>scientific_calculator</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
</properties>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.zipcodewilmington.scientificcalculator;

public class Calculator {

public static void run() {

boolean calcOn = true;

double displayVal = 0.0;

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("+")) 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("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("nlog")) displayVal = Operation.oneNumOp("nlog", displayVal);
else if (operator.equals("invnlog")) displayVal = Operation.oneNumOp("invnlog", displayVal);
else if (operator.equals("!")) displayVal = Operation.factorial("!", displayVal);
else {
System.out.println("Invalid operation, please enter help for a list of operations");
continue;
}

System.out.println("Current value: " + displayVal);
}


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ 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;
public static Integer getIntegerInput(String prompt) { return null;
}

public static Double getDoubleInput(String prompt) {
return null;
Scanner scanner = new Scanner(System.in);
print(prompt);
double userInput = scanner.nextDouble();
return userInput;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
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);
Calculator.run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
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);
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 = Math.sqrt(n);
else if (operation.equals("log")) result = Math.log10(n);
else if (operation.equals("invlog")) result = Math.pow(10,n);
else if (operation.equals("nlog")) result = Math.log(n);
else if (operation.equals("invnlog")) 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
public static double divide(double num1, double num2) {
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;
}

}