Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.zipcodewilmington.scientificcalculator;
public class Arithmetic {

//fields
private double result;

//constructor
//Sets the starting value for the calculation.
public Arithmetic(double initialValue) {
this.result = initialValue;
}

//methods

//addtion
public Arithmetic add(double num) {
this.result = this.result + num;
return this; // returns the object itself, enabling chaining like add(5).multiply(2)
}

//subtraction
public Arithmetic subtract(double num) {
this.result = this.result - num;
return this;
}

//multiplication
public Arithmetic multiply(double num) {
this.result = this.result * num;
return this;
}

//division
public Arithmetic divide(double num) {
if (num == 0) {
throw new ArithmeticException("Denominator cannot be zero");
}
this.result = this.result / num;
return this;
}

//square root, tehcnically redunant but it's functional.

public Arithmetic squareRoot(double num) {
this.result = Math.sqrt(num);
return this;
}


//power
public Arithmetic power(double num) {
this.result = Math.pow(this.result, num);
return this;
}

//modulo
public Arithmetic modulo(double num) {
this.result = this.result % num;
return this;
}

public Arithmetic sine() {
this.result = Math.sin(this.result);
return this;

}

public Arithmetic cosine() {
this.result = Math.cos(this.result);
return this;

}

public Arithmetic tangent() {
this.result = Math.tan(this.result);
return this;

}

public Arithmetic cosecant() {
this.result = (1/(Math.sin(this.result)));
return this;

}

public Arithmetic secant() {
this.result = (1/(Math.cos(this.result)));
return this;
}

public Arithmetic cotangent() {
this.result = (1/(Math.tan(this.result)));
return this;
}



//getResult() - will retrieve the final value after chaining
public double getResult() {
return this.result;
}

}




Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.zipcodewilmington.scientificcalculator;

public class Calculator {



}
Original file line number Diff line number Diff line change
@@ -1,32 +1,97 @@
package com.zipcodewilmington.scientificcalculator;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
* Created by leon on 2/9/18.
*/
public class Console {

// Prints the given output without a newline character
public static void print(String output, Object... args) {
System.out.printf(output, args);
}

// Prints the given output followed by a newline character
public static void println(String output, Object... args) {
print(output + "\n", args);
}

// Gets a string input from the user
public static String getStringInput(String prompt) {
Scanner scanner = new Scanner(System.in);
println(prompt);
String userInput = scanner.nextLine();
return userInput;
}

// Gets an integer input from the user
// TODO: implement using scanner + Integer.parseInt (handle NumberFormatException)
public static Integer getIntegerInput(String prompt) {
return null;
// return null;

Scanner scanner = new Scanner(System.in);
println(prompt);

try {
Integer userInput = scanner.nextInt();
scanner.nextLine();
return userInput;

} catch (InputMismatchException e) {
scanner.nextLine();
println("Invalid input. Please enter a valid number");
return getIntegerInput(prompt);
}
}

// Gets a double input from the user
public static Double getDoubleInput(String prompt) {
return null;
// return null;
Scanner scanner = new Scanner(System.in); //this initalize input from the user.
println(prompt); //this prints the prompt to the console

try {
Double userInput = scanner.nextDouble(); //this gets the user's input as a double
scanner.nextLine(); // consume the newline character
return userInput;
} catch (InputMismatchException e) {
scanner.nextLine();
println("Invalid input. Please enter a valid number.");
return getDoubleInput(prompt);
}

}

// //Calculations: Can't use this anymore because it's stateless :( back to the drawing board - Jaiden)
// public static Double getCalc(Double num1, Double num2) {
// double result = 0;
// int operation = 0;

// switch (operation) {
// case 1:
// //addition
// result = num1 + num2;
// break;
// case 2:
// //subtraction
// result = num1 - num2;
// break;
// case 3:
// //multiplication
// result = num1 * num2;
// break;
// case 4:
// //division
// result = num1 / num2;
// break;
// default:
// System.out.println("Invalid operation");
// }

// return result;
// }


}
Loading