123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. * Write a description of class Calculator here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7. import java.util.Scanner;
  8. public class Calculator extends BasicCalculations
  9. {
  10. // instance variables - replace the example below with your own
  11. /**
  12. * Constructor for objects of class Calculator
  13. */
  14. public Calculator() {
  15. currentNumber = 0;
  16. isError = false;
  17. }
  18. /**
  19. * An example of a method - replace this comment with your own
  20. *
  21. * @param y a sample parameter for a method
  22. * @return the sum of x and y
  23. */
  24. public void runCommandLoop()
  25. {
  26. Scanner input = new Scanner(System.in);
  27. Calculator calc = new Calculator();
  28. while (true) {
  29. System.out.print("Enter a command: ");
  30. String command = input.next();
  31. if (!isError) {
  32. if (command.equalsIgnoreCase("exit")) {
  33. System.out.print("Thanks for calculating with us!");
  34. break;
  35. }
  36. if (command.equalsIgnoreCase("clear")) {
  37. calc.clear();
  38. }
  39. if (command.equalsIgnoreCase("invert")) {
  40. calc.invertSign();
  41. }
  42. if (command.equalsIgnoreCase("inverse")) {
  43. calc.inverse(currentNumber);
  44. }
  45. if (command.equalsIgnoreCase("save")) {
  46. calc.remember();
  47. }
  48. if (command.equalsIgnoreCase("recall")) {
  49. calc.recall();
  50. }
  51. if (command.equalsIgnoreCase("save2")) {
  52. calc.addToNumInMemory();
  53. }
  54. if (command.equalsIgnoreCase("mode")) {
  55. calc.switchDisplayMode();
  56. }
  57. if (command.equalsIgnoreCase("units")) {
  58. calc.switchUnitsMode();
  59. }
  60. if (command.equalsIgnoreCase("add")) {
  61. if (currentNumber == 0) {
  62. firstNumber = input.nextDouble();
  63. // read second number
  64. secondNumber = input.nextDouble();
  65. // call the add method on calc and display
  66. calc.add(firstNumber,secondNumber);
  67. } else {
  68. firstNumber = currentNumber;
  69. secondNumber = input.nextDouble();
  70. calc.add(firstNumber,secondNumber);
  71. }
  72. }
  73. if (command.equalsIgnoreCase("subtract")) {
  74. if (currentNumber == 0) {
  75. firstNumber = input.nextDouble();
  76. // read second number
  77. secondNumber = input.nextDouble();
  78. // call the add method on calc and display
  79. calc.subtract(firstNumber,secondNumber);
  80. } else {
  81. firstNumber = currentNumber;
  82. secondNumber = input.nextDouble();
  83. calc.subtract(firstNumber,secondNumber);
  84. }
  85. }
  86. if (command.equalsIgnoreCase("multiply")) {
  87. if (currentNumber == 0) {
  88. firstNumber = input.nextDouble();
  89. // read second number
  90. secondNumber = input.nextDouble();
  91. // call the add method on calc and display
  92. calc.multiply(firstNumber,secondNumber);
  93. } else {
  94. firstNumber = currentNumber;
  95. secondNumber = input.nextDouble();
  96. calc.multiply(firstNumber,secondNumber);
  97. }
  98. }
  99. if (command.equalsIgnoreCase("divide")) {
  100. if (currentNumber == 0) {
  101. firstNumber = input.nextDouble();
  102. // read second number
  103. secondNumber = input.nextDouble();
  104. // call the add method on calc and display
  105. calc.divide(firstNumber,secondNumber);
  106. } else {
  107. firstNumber = currentNumber;
  108. secondNumber = input.nextDouble();
  109. calc.divide(firstNumber,secondNumber);
  110. }
  111. }
  112. if (command.equalsIgnoreCase("square")) {
  113. if (currentNumber == 0) {
  114. firstNumber = input.nextDouble();
  115. // call the add method on calc and display
  116. calc.square(firstNumber);
  117. } else {
  118. firstNumber = currentNumber;
  119. calc.square(firstNumber);
  120. }
  121. }
  122. if (command.equalsIgnoreCase("round")) {
  123. if (currentNumber == 0) {
  124. firstNumber = input.nextDouble();
  125. // call the add method on calc and display
  126. calc.round(firstNumber);
  127. } else {
  128. firstNumber = currentNumber;
  129. calc.round(firstNumber);
  130. }
  131. }
  132. if (command.equalsIgnoreCase("up")) {
  133. if (currentNumber == 0) {
  134. firstNumber = input.nextDouble();
  135. // call the add method on calc and display
  136. calc.roundUp(firstNumber);
  137. } else {
  138. firstNumber = currentNumber;
  139. calc.roundUp(firstNumber);
  140. }
  141. }
  142. if (command.equalsIgnoreCase("down")) {
  143. if (currentNumber == 0) {
  144. firstNumber = input.nextDouble();
  145. // call the add method on calc and display
  146. calc.roundDown(firstNumber);
  147. } else {
  148. firstNumber = currentNumber;
  149. calc.roundDown(firstNumber);
  150. }
  151. }
  152. if (command.equalsIgnoreCase("log")) {
  153. if (currentNumber == 0) {
  154. firstNumber = input.nextDouble();
  155. // call the add method on calc and display
  156. calc.log(firstNumber);
  157. } else {
  158. firstNumber = currentNumber;
  159. calc.log(firstNumber);
  160. }
  161. }
  162. if (command.equalsIgnoreCase("power")) {
  163. if (currentNumber == 0) {
  164. firstNumber = input.nextDouble();
  165. // call the add method on calc and display
  166. secondNumber = input.nextDouble();
  167. calc.power(firstNumber, secondNumber);
  168. } else {
  169. firstNumber = currentNumber;
  170. secondNumber = input.nextDouble();
  171. calc.power(firstNumber, secondNumber);
  172. }
  173. }
  174. if (command.equalsIgnoreCase("sin")) {
  175. if (currentNumber == 0) {
  176. firstNumber = input.nextDouble();
  177. // call the add method on calc and display
  178. calc.sin(firstNumber);
  179. } else {
  180. firstNumber = currentNumber;
  181. calc.sin(firstNumber);
  182. }
  183. }
  184. if (command.equalsIgnoreCase("invsin")) {
  185. if (currentNumber == 0) {
  186. firstNumber = input.nextDouble();
  187. // call the add method on calc and display
  188. calc.asin(firstNumber);
  189. } else {
  190. firstNumber = currentNumber;
  191. calc.asin(firstNumber);
  192. }
  193. }
  194. if (command.equalsIgnoreCase("cos")) {
  195. if (currentNumber == 0) {
  196. firstNumber = input.nextDouble();
  197. // call the add method on calc and display
  198. calc.cos(firstNumber);
  199. } else {
  200. firstNumber = currentNumber;
  201. calc.cos(firstNumber);
  202. }
  203. }
  204. if (command.equalsIgnoreCase("invcos")) {
  205. if (currentNumber == 0) {
  206. firstNumber = input.nextDouble();
  207. // call the add method on calc and display
  208. calc.acos(firstNumber);
  209. } else {
  210. firstNumber = currentNumber;
  211. calc.acos(firstNumber);
  212. }
  213. }
  214. if (command.equalsIgnoreCase("tan")) {
  215. if (currentNumber == 0) {
  216. firstNumber = input.nextDouble();
  217. // call the add method on calc and display
  218. calc.tan(firstNumber);
  219. } else {
  220. firstNumber = currentNumber;
  221. calc.tan(firstNumber);
  222. }
  223. // and on and on
  224. }
  225. if (command.equalsIgnoreCase("invtan")) {
  226. if (currentNumber == 0) {
  227. firstNumber = input.nextDouble();
  228. // call the add method on calc and display
  229. calc.atan(firstNumber);
  230. } else {
  231. firstNumber = currentNumber;
  232. calc.atan(firstNumber);
  233. }
  234. }
  235. } else {
  236. System.out.println("Error! Clear Error!");
  237. if (command.equalsIgnoreCase("clear")) {
  238. calc.clear();
  239. }
  240. }
  241. }
  242. }
  243. }