Calculator.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import java.util.*;
  2. public class Calculator{
  3. /**
  4. * Write a description of class Calculator here.
  5. *
  6. * @author (your name)
  7. * @version (a version number or a date)
  8. */
  9. public static double lastAns = 0;
  10. public static void main(String args[]){
  11. String toDo; //what the user wants to do
  12. double num1 = 0; //The user's first input
  13. double num2 = 0; //The user's second input
  14. double result = 0; //The user's answer
  15. boolean off = false; //The user typed quit, becomes true
  16. double memory;
  17. boolean error = false;
  18. MathMethods calc = new MathMethods();
  19. while (off == false){
  20. Scanner input1 = new Scanner(System.in);
  21. System.out.println("What do you want to do?");
  22. toDo = input1.nextLine();
  23. if (toDo.equalsIgnoreCase("quit")){
  24. off = true;
  25. System.out.println("Goodbye!! \n \n");
  26. } else {
  27. switch (toDo) {
  28. //System.out.println("toDO is: " + toDo);
  29. case "add":
  30. case "subtract":
  31. case "multiply":
  32. case "divide":
  33. case "exponent":
  34. case "cosine" :
  35. case "inverse cosine" :
  36. case "square root" :
  37. case "invert" :
  38. case "sine" :
  39. case "inverse sine" :
  40. case "degree to radian" :
  41. case "radian to degree" :
  42. case "square" :
  43. case "tangent" :
  44. case "inverse tangent" :
  45. case "inverse" :
  46. case "roundTo" :
  47. System.out.println(toDo + " -- Enter first number:");
  48. num1 = input1.nextDouble();
  49. //System.out.println("num1 is: " + num1);
  50. }
  51. switch (toDo){
  52. case "add":
  53. case "subtract":
  54. case "multiply":
  55. case "divide":
  56. case "exponent":
  57. System.out.println(toDo + " -- Enter second number:");
  58. num2 = input1.nextDouble();
  59. //System.out.println("num2 is: " + num2);
  60. }
  61. switch (toDo) {
  62. case "add":
  63. result = calc.add(num1, num2);
  64. lastAns = result;
  65. calc.printAns(String.valueOf(result));
  66. break;
  67. case "subtract":
  68. result = calc.subtract(num1, num2);
  69. lastAns = result;
  70. calc.printAns(String.valueOf(result));
  71. break;
  72. case "multiply":
  73. result = calc.multiply(num1, num2);
  74. lastAns = result;
  75. calc.printAns(String.valueOf(result));
  76. break;
  77. case "divide":
  78. result = calc.divide(num1, num2);
  79. lastAns = result;
  80. calc.printAns(String.valueOf(result));
  81. break;
  82. case "exponent":
  83. result = calc.exponent(num1, num2);
  84. lastAns = result;
  85. calc.printAns(String.valueOf(result));
  86. break;
  87. case "cosine":
  88. result = calc.cosine(num1);
  89. lastAns = result;
  90. calc.printAns(String.valueOf(result));
  91. break;
  92. case "inverse cosine":
  93. result = calc.invCosine(num1);
  94. lastAns = result;
  95. calc.printAns(String.valueOf(result));
  96. break;
  97. case "square root":
  98. result = calc.squareRoot(num1);
  99. lastAns = result;
  100. calc.printAns(String.valueOf(result));
  101. break;
  102. case "invert":
  103. result = calc.invert(num1);
  104. lastAns = result;
  105. calc.printAns(String.valueOf(result));
  106. break;
  107. case "sine":
  108. result = calc.sin(num1);
  109. lastAns = result;
  110. calc.printAns(String.valueOf(result));
  111. break;
  112. case "inverse sine":
  113. result = calc.iSin(num1);
  114. lastAns = result;
  115. calc.printAns(String.valueOf(result));
  116. break;
  117. case "degree to radian":
  118. result = calc.degToRad(num1);
  119. lastAns = result;
  120. calc.printAns(String.valueOf(result));
  121. break;
  122. case "radian to degree":
  123. result = calc.radToDeg(num1);
  124. lastAns = result;
  125. calc.printAns(String.valueOf(result));
  126. break;
  127. case "fortune cookie":
  128. calc.printAns(calc.fortuneCookie());
  129. break;
  130. case "display mode decimal":
  131. calc.printAns(String.valueOf(lastAns));
  132. break;
  133. case "display mode hexadecimal":
  134. calc.printAns(calc.switchDisplayMode("hexadecimal"));
  135. case "display mode octal":
  136. calc.printAns(calc.switchDisplayMode("octal"));
  137. break;
  138. case "display mode binary":
  139. calc.printAns(calc.switchDisplayMode("binary"));
  140. break;
  141. case "display mode" :
  142. calc.printAns(calc.switchDisplayMode());
  143. break;
  144. case "random number":
  145. result = calc.randomNumber();
  146. lastAns = result;
  147. calc.printAns(String.valueOf(result));
  148. break;
  149. case "square":
  150. result = calc.square(num1);
  151. lastAns = result;
  152. calc.printAns(String.valueOf(result));
  153. break;
  154. case "inverse":
  155. result = calc.inverse(num1);
  156. lastAns = result;
  157. calc.printAns(String.valueOf(result));
  158. break;
  159. case "tangent":
  160. result = calc.tangent(num1);
  161. lastAns = result;
  162. calc.printAns(String.valueOf(result));
  163. break;
  164. case "inverse tangent":
  165. result = calc.iTan(num1);
  166. lastAns = result;
  167. calc.printAns(String.valueOf(result));
  168. break;
  169. case "MR" :
  170. result = Memory.memRead();
  171. Memory.printAns(String.valueOf(result));
  172. break;
  173. case "M+" :
  174. result = Memory.memUpdate(lastAns);
  175. Memory.printAns(String.valueOf(result));
  176. break;
  177. case "MC" :
  178. result = Memory.memClear();
  179. Memory.printAns(String.valueOf(result));
  180. break;
  181. case "roundTo":
  182. memory = result;
  183. calc.roundTo(memory, (int)num1);
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. }