Calculator.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import java.util.*;
  2. /**
  3. * Write a description of class Calculator here.
  4. *
  5. * @author (your name)
  6. * @version (a version number or a date)
  7. */
  8. class Calculator{
  9. public static void main(String args[]){
  10. String toDo; //what the user wants to do
  11. double num1 = 0; //The user's first input
  12. double num2 = 0; //The user's second input
  13. double result = 0; //The user's answer
  14. boolean off = false; //The user typed quit, becomes true
  15. double memory;
  16. boolean error = false;
  17. MathMethods calc = new MathMethods();
  18. while (off == false){
  19. Scanner input1 = new Scanner(System.in);
  20. System.out.println("What do you want to do?");
  21. toDo = input1.next();
  22. if (toDo.equalsIgnoreCase("quit")){
  23. off = true;
  24. System.out.println("Goodbye!!");
  25. } else if(toDo.equalsIgnoreCase("memory")){
  26. System.out.println("Read, Change or Clear?");
  27. String mem = input1.next();
  28. switch(mem) {
  29. case "Read" :
  30. result = Memory.memRead();
  31. Memory.printAns(String.valueOf(result));
  32. break;
  33. case "Change" :
  34. System.out.println("Enter number:");
  35. num1 = input1.nextDouble();
  36. result = Memory.memUpdate(num1);
  37. Memory.printAns(String.valueOf(result));
  38. break;
  39. case "Clear" :
  40. result = Memory.memClear();
  41. Memory.printAns(String.valueOf(result));
  42. break;
  43. }
  44. }
  45. else {
  46. //System.out.println("toDO is: " + toDo);
  47. System.out.println(toDo + " -- Enter first number:");
  48. num1 = input1.nextDouble();
  49. //System.out.println("num1 is: " + num1);
  50. switch (toDo){
  51. case "add":
  52. case "subtract":
  53. case "multiply":
  54. case "divide":
  55. case "exponent":
  56. System.out.println(toDo + " -- Enter second number:");
  57. num2 = input1.nextDouble();
  58. //System.out.println("num2 is: " + num2);
  59. }
  60. switch (toDo) {
  61. case "add":
  62. result = calc.add(num1, num2);
  63. memory = result;
  64. calc.printAns(String.valueOf(result));
  65. break;
  66. case "subtract":
  67. result = calc.subtract(num1, num2);
  68. memory = result;
  69. calc.printAns(String.valueOf(result));
  70. break;
  71. case "multiply":
  72. result = calc.multiply(num1, num2);
  73. memory = result;
  74. calc.printAns(String.valueOf(result));
  75. break;
  76. case "divide":
  77. result = calc.divide(num1, num2);
  78. memory = result;
  79. calc.printAns(String.valueOf(result));
  80. break;
  81. case "exponent":
  82. result = calc.exponent(num1, num2);
  83. memory = result;
  84. calc.printAns(String.valueOf(result));
  85. break;
  86. case "cosine":
  87. result = calc.cosine(num1);
  88. memory = result;
  89. calc.printAns(String.valueOf(result));
  90. break;
  91. case "inverse cosine":
  92. result = calc.invCosine(num1);
  93. memory = result;
  94. calc.printAns(String.valueOf(result));
  95. break;
  96. case "square root":
  97. result = calc.squareRoot(num1);
  98. memory = result;
  99. calc.printAns(String.valueOf(result));
  100. break;
  101. case "invert":
  102. result = calc.invert(num1);
  103. memory = result;
  104. calc.printAns(String.valueOf(result));
  105. break;
  106. case "sine":
  107. result = calc.sin(num1);
  108. memory = result;
  109. calc.printAns(String.valueOf(result));
  110. break;
  111. case "inverse sine":
  112. result = calc.iSin(num1);
  113. memory = result;
  114. calc.printAns(String.valueOf(result));
  115. break;
  116. case "degree to radian":
  117. result = calc.degToRad(num1);
  118. memory = result;
  119. calc.printAns(String.valueOf(result));
  120. break;
  121. case "radian to degree":
  122. result = calc.radToDeg(num1);
  123. memory = result;
  124. calc.printAns(String.valueOf(result));
  125. break;
  126. case "square":
  127. result = calc.square(num1);
  128. memory = result;
  129. calc.printAns(String.valueOf(result));
  130. break;
  131. case "inverse":
  132. result = calc.inverse(num1);
  133. memory = result;
  134. calc.printAns(String.valueOf(result));
  135. break;
  136. case "tangent":
  137. result = calc.tangent(num1);
  138. memory = result;
  139. calc.printAns(String.valueOf(result));
  140. break;
  141. case "inverse tangent":
  142. result = calc.iTan(num1);
  143. memory = result;
  144. calc.printAns(String.valueOf(result));
  145. break;
  146. }
  147. }
  148. }
  149. }
  150. }
  151. // /**
  152. // * Created by leon on 2/9/18.
  153. // */
  154. // public class Console {
  155. // Calculator calc = new Calculator();
  156. // public static void print(String output, Object... args) {
  157. // System.out.printf(output, args);
  158. // }
  159. // public static void println(String output, Object... args) {
  160. // print(output + "\n", args);
  161. // }
  162. // public static String getMethod() {
  163. // Scanner scanner = new Scanner(System.in);
  164. // String userInput = scanner.nextLine();
  165. // return userInput;
  166. // }
  167. // public static String switchStatment() {
  168. // Scanner scanner = new Scanner(System.in);
  169. // String ultimateAnswer = "";
  170. // Boolean turnOff = false;
  171. // switch (getMethod()) {
  172. // case "add" :
  173. // System.out.println("Enter your numbers:");
  174. // double userInput = scanner.nextDouble();
  175. // double nextInput = scanner.nextDouble();
  176. // double answer = Calculator.add(userInput, nextInput);
  177. // ultimateAnswer = Double.toString(answer);
  178. // return Double.toString(answer);
  179. // case "subtract" :
  180. // System.out.println("Enter your numbers:");
  181. // userInput = scanner.nextDouble();
  182. // nextInput = scanner.nextDouble();
  183. // answer = Calculator.subtract(userInput, nextInput);
  184. // ultimateAnswer = Double.toString(answer);
  185. // return ultimateAnswer;
  186. // case "multiply" :
  187. // System.out.println("Enter your numbers:");
  188. // userInput = scanner.nextDouble();
  189. // nextInput = scanner.nextDouble();
  190. // answer = Calculator.multiply(userInput, nextInput);
  191. // ultimateAnswer = Double.toString(answer);
  192. // return ultimateAnswer;
  193. // case "divide" :
  194. // System.out.println("Enter your numbers:");
  195. // userInput = scanner.nextDouble();
  196. // nextInput = scanner.nextDouble();
  197. // answer = Calculator.divide(userInput, nextInput);
  198. // ultimateAnswer = Double.toString(answer);
  199. // return ultimateAnswer;
  200. // case "Square Root" :
  201. // }
  202. // return ultimateAnswer;
  203. // }
  204. // /*public static String getStringInput(String prompt) {
  205. // Scanner scanner = new Scanner(System.in);
  206. // println(prompt);
  207. // String userInput = scanner.nextLine();
  208. // return userInput;
  209. // }
  210. // public static Integer getIntegerInput(String prompt) {
  211. // Scanner scanner = new Scanner(System.in);
  212. // println(prompt);
  213. // int userInput = scanner.nextInt();
  214. // return null;
  215. // }
  216. // public static Double getDoubleInput(String prompt) {
  217. // Scanner scanner = new Scanner(System.in);
  218. // println(prompt);
  219. // double userInput = scanner.nextDouble();
  220. // double nextInput = scanner.nextDouble();
  221. // double answer = Calculator.add(userInput, nextInput);
  222. // return answer;
  223. // }*/
  224. // }