123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- import java.util.Scanner;
-
- /**
- * Class used perform calculator operations using Calculator methods and Console print methods
- *
- * @author (Demetrius Murray, Nick Satinover, Zavon Malone)
- * @version (10/22/18 v1.4 3:07PM)
- */
- public class MainApplication {
-
- public static void main(String[] args) {
- System.out.println("It's time for the calculator.");
- Console.printLegend();
- Calculator calc = new Calculator();
-
- TrigFunctions trig = new TrigFunctions();
- Bonus bonus = new Bonus();
-
- double currentNum = 0;
- double tempNum;
- String tempString;
- String firstValue = "";
-
- try{
- firstValue = Console.inputString();
- if(firstValue.matches("[0-9]+")){
- currentNum = Double.valueOf(firstValue);
- }
- else if(firstValue.matches("q"))
- {
- Console.quit();
- }
- else if(firstValue.matches("c"))
- {
- currentNum = Console.displayClear();
- currentNum = Double.valueOf(Console.inputString());
- }
- else{
- Console.error();
- currentNum = Double.valueOf(Console.inputString());
- }
- }
- catch(Exception a)
- {
- System.out.println("An error occured with the last request");
- System.exit(0);
- }
-
- while(String.valueOf(firstValue) != "q") {
- try{
- switch (Console.getOperator()) {
- case '+': //takes string input. if statement places memory value into function, else converts to double
- tempString = Console.inputString();
- if(tempString.equals("R")){
- tempNum = calc.getMemory();
- } else {
- tempNum = Double.valueOf(tempString);
- }
- currentNum = calc.add(currentNum, tempNum);
- break;
- case '-': //takes string input. if statement places memory value into function, else converts to double
- tempString = Console.inputString();
- if(tempString.equals("R")){
- tempNum = calc.getMemory();
- } else {
- tempNum = Double.valueOf(tempString);
- }
- currentNum = calc.subtract(currentNum, tempNum);
- break;
- case '*': //takes string input. if statement places memory value into function, else converts to double
- tempString = Console.inputString();
- if(tempString.equals("R")){
- tempNum = calc.getMemory();
- } else {
- tempNum = Double.valueOf(tempString);
- }
- currentNum = calc.multiply(currentNum, tempNum);
- break;
- case '/': //takes string input. if statement places memory value into function, else converts to double
- tempString = Console.inputString();
- if(tempString.equals("R")){
- tempNum = calc.getMemory();
- } else {
- tempNum = Double.valueOf(tempString);
- }
- while(tempNum == 0 ){
- System.out.print("Error, cannot divide by zero. Please ");
- tempNum = Console.inputDouble();
- }
- currentNum = calc.divide(currentNum, tempNum);
- break;
- case '@':
- currentNum = calc.square(currentNum);
- break;
- case '&':
- currentNum = calc.squareRoot(currentNum);
- break;
- case '^': //takes string input. if statement places memory value into function, else converts to double
- tempString = Console.inputString();
- if(tempString.equals("R")){
- tempNum = calc.getMemory();
- } else {
- tempNum = Double.valueOf(tempString);
- }
- currentNum = calc.exponent(currentNum, tempNum);
- break;
- case '!':
- if (currentNum == 0){
- System.out.println("Error, cannot divide by zero. Press 'c' to clear.");
- continue;
- }
- currentNum = calc.invert(currentNum);
- break;
- case '#':
- currentNum = calc.changeSign(currentNum);
- break;
- case 'M':
- calc.storeMemory(currentNum);
- Console.printMemoryValue(calc.getMemory());
- continue;
- case 'R':
- Console.printMemoryValue(calc.getMemory());
- break;
- case 'C':
- calc.clearMemory();
- Console.printMemoryValue(calc.getMemory());
- break;
- case '~':
- currentNum = Console.displayChange();
- break;
- case 'c':
- currentNum = Console.displayClear();
- currentNum = Double.valueOf(Console.inputString()); //asks for new value
- continue; //back to top of loop
- case 'l':
- Console.printLegend();
- break;
- case 'q':
- Console.quit();
- break;
- /**
- * Below are method calls to the TrigFunction class
- */
- case 's':
- currentNum = trig.sin(currentNum);
- break;
- case 'x':
- currentNum = trig.cosine(currentNum);
- break;
- case 't':
- currentNum = trig.tangent(currentNum);
- break;
- /*
- case 'w':
- currentNum = trig.inverseSign(currentNum);
- break;
- case 'e':
- currentNum = trig.inverseCosign(currentNum);
- break;
- case 'r':
- currentNum = trig.inverseTangent(currentNum);
- break;
- */
- /**
- * Below are method calls to the Bonus class
- */
- case 'f':
- currentNum = bonus.factorial((int) currentNum);
- break;
- default:
- Console.errorOperator();
- }
- }
- /**
- * This block will only execute if any Arithmetic exception occurs in try block
- */
- catch(ArithmeticException e)
- {
- System.out.println("You should not divide a number by zero");
- }
- /**
- * This block will only execute if any other exception occurs in try block
- */
- catch(Exception e)
- {
- System.out.println("An error occured with the last request");
- }
- Console.displayValue(currentNum);
- }
- /*
- if (Console.inputString() == "c"){
- Console.displayClear();
- }
- */
- }
- }
|