123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
-
- import java.util.*;
-
- public class Calculator{
-
- /**
- * Write a description of class Calculator here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
-
- public static double lastAns = 0;
-
- public static void main(String args[]){
- String toDo; //what the user wants to do
- double num1 = 0; //The user's first input
- double num2 = 0; //The user's second input
- double result = 0; //The user's answer
- boolean off = false; //The user typed quit, becomes true
-
- double memory;
-
- boolean error = false;
- MathMethods calc = new MathMethods();
-
- while (off == false){
- Scanner input1 = new Scanner(System.in);
- System.out.println("What do you want to do?");
-
- toDo = input1.nextLine();
-
- if (toDo.equalsIgnoreCase("quit")){
- off = true;
- System.out.println("Goodbye!! \n \n");
-
- } else {
- switch (toDo) {
- //System.out.println("toDO is: " + toDo);
-
- case "add":
- case "subtract":
- case "multiply":
- case "divide":
- case "exponent":
- case "cosine" :
- case "inverse cosine" :
- case "square root" :
- case "invert" :
- case "sine" :
- case "inverse sine" :
- case "degree to radian" :
- case "radian to degree" :
- case "square" :
- case "tangent" :
- case "inverse tangent" :
- case "inverse" :
-
- System.out.println(toDo + " -- Enter first number:");
- num1 = input1.nextDouble();
- //System.out.println("num1 is: " + num1);
-
- switch (toDo){
- case "add":
- case "subtract":
- case "multiply":
- case "divide":
- case "exponent":
- System.out.println(toDo + " -- Enter second number:");
- num2 = input1.nextDouble();
- //System.out.println("num2 is: " + num2);
-
- }
-
- switch (toDo) {
-
- case "add":
- result = calc.add(num1, num2);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "subtract":
- result = calc.subtract(num1, num2);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "multiply":
- result = calc.multiply(num1, num2);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "divide":
- result = calc.divide(num1, num2);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "exponent":
- result = calc.exponent(num1, num2);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "cosine":
- result = calc.cosine(num1);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "inverse cosine":
- result = calc.invCosine(num1);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "square root":
- result = calc.squareRoot(num1);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "invert":
- result = calc.invert(num1);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "sine":
- result = calc.sin(num1);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "inverse sine":
- result = calc.iSin(num1);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "degree to radian":
- result = calc.degToRad(num1);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "radian to degree":
- result = calc.radToDeg(num1);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "fortune cookie":
- calc.printAns(calc.fortuneCookie());
- break;
-
- case "display mode decimal":
- calc.printAns(String.valueOf(lastAns));
- break;
-
- case "display mode hexadecimal":
- calc.printAns(calc.switchDisplayMode("hexadecimal"));
-
- case "display mode octal":
- calc.printAns(calc.switchDisplayMode("octal"));
- break;
-
- case "display mode binary":
- calc.printAns(calc.switchDisplayMode("binary"));
- break;
-
- case "display mode" :
- calc.printAns(calc.switchDisplayMode());
- break;
-
- case "random number":
- result = calc.randomNumber();
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "square":
- result = calc.square(num1);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "inverse":
- result = calc.inverse(num1);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "tangent":
- result = calc.tangent(num1);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "inverse tangent":
- result = calc.iTan(num1);
- lastAns = result;
- calc.printAns(String.valueOf(result));
- break;
-
- case "MR" :
- result = Memory.memRead();
- Memory.printAns(String.valueOf(result));
- break;
-
- case "M+" :
- result = Memory.memUpdate(lastAns);
- Memory.printAns(String.valueOf(result));
- break;
-
- case "MC" :
- result = Memory.memClear();
- Memory.printAns(String.valueOf(result));
- break;
-
- case "roundTo":
- memory = result;
- calc.roundTo(memory, (int)num1);
- break;
-
- }
- }
- }
- }
-
- }
- }
-
-
|