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. System.out.println(toDo + " -- Enter first number:");
  47. num1 = input1.nextDouble();
  48. //System.out.println("num1 is: " + num1);
  49. switch (toDo){
  50. case "add":
  51. case "subtract":
  52. case "multiply":
  53. case "divide":
  54. case "exponent":
  55. System.out.println(toDo + " -- Enter second number:");
  56. num2 = input1.nextDouble();
  57. //System.out.println("num2 is: " + num2);
  58. }
  59. switch (toDo) {
  60. case "add":
  61. result = calc.add(num1, num2);
  62. lastAns = result;
  63. calc.printAns(String.valueOf(result));
  64. break;
  65. case "subtract":
  66. result = calc.subtract(num1, num2);
  67. lastAns = result;
  68. calc.printAns(String.valueOf(result));
  69. break;
  70. case "multiply":
  71. result = calc.multiply(num1, num2);
  72. lastAns = result;
  73. calc.printAns(String.valueOf(result));
  74. break;
  75. case "divide":
  76. result = calc.divide(num1, num2);
  77. lastAns = result;
  78. calc.printAns(String.valueOf(result));
  79. break;
  80. case "exponent":
  81. result = calc.exponent(num1, num2);
  82. lastAns = result;
  83. calc.printAns(String.valueOf(result));
  84. break;
  85. case "cosine":
  86. result = calc.cosine(num1);
  87. lastAns = result;
  88. calc.printAns(String.valueOf(result));
  89. break;
  90. case "inverse cosine":
  91. result = calc.invCosine(num1);
  92. lastAns = result;
  93. calc.printAns(String.valueOf(result));
  94. break;
  95. case "square root":
  96. result = calc.squareRoot(num1);
  97. lastAns = result;
  98. calc.printAns(String.valueOf(result));
  99. break;
  100. case "invert":
  101. result = calc.invert(num1);
  102. lastAns = result;
  103. calc.printAns(String.valueOf(result));
  104. break;
  105. case "sine":
  106. result = calc.sin(num1);
  107. lastAns = result;
  108. calc.printAns(String.valueOf(result));
  109. break;
  110. case "inverse sine":
  111. result = calc.iSin(num1);
  112. lastAns = result;
  113. calc.printAns(String.valueOf(result));
  114. break;
  115. case "degree to radian":
  116. result = calc.degToRad(num1);
  117. lastAns = result;
  118. calc.printAns(String.valueOf(result));
  119. break;
  120. case "radian to degree":
  121. result = calc.radToDeg(num1);
  122. lastAns = result;
  123. calc.printAns(String.valueOf(result));
  124. break;
  125. case "fortune cookie":
  126. calc.printAns(calc.fortuneCookie());
  127. break;
  128. case "display mode decimal":
  129. calc.printAns(String.valueOf(lastAns));
  130. break;
  131. case "display mode hexadecimal":
  132. calc.printAns(calc.switchDisplayMode("hexadecimal"));
  133. case "display mode octal":
  134. calc.printAns(calc.switchDisplayMode("octal"));
  135. break;
  136. case "display mode binary":
  137. calc.printAns(calc.switchDisplayMode("binary"));
  138. break;
  139. case "display mode" :
  140. calc.printAns(calc.switchDisplayMode());
  141. break;
  142. case "random number":
  143. result = calc.randomNumber();
  144. lastAns = result;
  145. calc.printAns(String.valueOf(result));
  146. break;
  147. case "square":
  148. result = calc.square(num1);
  149. lastAns = result;
  150. calc.printAns(String.valueOf(result));
  151. break;
  152. case "inverse":
  153. result = calc.inverse(num1);
  154. lastAns = result;
  155. calc.printAns(String.valueOf(result));
  156. break;
  157. case "tangent":
  158. result = calc.tangent(num1);
  159. lastAns = result;
  160. calc.printAns(String.valueOf(result));
  161. break;
  162. case "inverse tangent":
  163. result = calc.iTan(num1);
  164. lastAns = result;
  165. calc.printAns(String.valueOf(result));
  166. break;
  167. case "MR" :
  168. result = Memory.memRead();
  169. Memory.printAns(String.valueOf(result));
  170. break;
  171. case "M+" :
  172. result = Memory.memUpdate(lastAns);
  173. Memory.printAns(String.valueOf(result));
  174. break;
  175. case "MC" :
  176. result = Memory.memClear();
  177. Memory.printAns(String.valueOf(result));
  178. break;
  179. case "roundTo":
  180. memory = result;
  181. calc.roundTo(memory, (int)num1);
  182. break;
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }