Calculator.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import java.util.*;
  2. class Calculator{
  3. public static void main(String args[]){
  4. String toDo; //what the user wants to do
  5. double num1 = 0; //The user's first input
  6. double num2 = 0; //The user's second input
  7. double result = 0; //The user's answer
  8. boolean off = false; //The user typed quit, becomes true
  9. double memory;
  10. boolean error = false;
  11. MathMethods calc = new MathMethods();
  12. while (off == false){
  13. Scanner input1 = new Scanner(System.in);
  14. System.out.println("What do you want to do?");
  15. toDo = input1.nextLine();
  16. if (toDo.equals("quit")){
  17. off = true;
  18. System.out.println("Goodbye!!");
  19. } else {
  20. //System.out.println("toDO is: " + toDo);
  21. System.out.println(toDo + " -- Enter first number:");
  22. num1 = input1.nextDouble();
  23. //System.out.println("num1 is: " + num1);
  24. switch (toDo){
  25. case "add":
  26. case "subtract":
  27. case "multiply":
  28. case "divide":
  29. case "exponent":
  30. System.out.println(toDo + " -- Enter second number:");
  31. num2 = input1.nextDouble();
  32. //System.out.println("num2 is: " + num2);
  33. }
  34. switch (toDo) {
  35. case "add":
  36. result = calc.add(num1, num2);
  37. memory = result;
  38. calc.printAns(String.valueOf(result));
  39. break;
  40. case "subtract":
  41. result = calc.subtract(num1, num2);
  42. memory = result;
  43. calc.printAns(String.valueOf(result));
  44. break;
  45. case "multiply":
  46. result = calc.multiply(num1, num2);
  47. memory = result;
  48. calc.printAns(String.valueOf(result));
  49. break;
  50. case "divide":
  51. result = calc.divide(num1, num2);
  52. memory = result;
  53. calc.printAns(String.valueOf(result));
  54. break;
  55. case "exponent":
  56. result = calc.exponent(num1, num2);
  57. memory = result;
  58. calc.printAns(String.valueOf(result));
  59. break;
  60. case "cosine":
  61. result = calc.cosine(num1);
  62. memory = result;
  63. calc.printAns(String.valueOf(result));
  64. break;
  65. case "inverse cosine":
  66. result = calc.invCosine(num1);
  67. memory = result;
  68. calc.printAns(String.valueOf(result));
  69. break;
  70. case "square root":
  71. result = calc.squareRoot(num1);
  72. memory = result;
  73. calc.printAns(String.valueOf(result));
  74. break;
  75. case "invert":
  76. result = calc.invert(num1);
  77. memory = result;
  78. calc.printAns(String.valueOf(result));
  79. break;
  80. case "sine":
  81. result = calc.sin(num1);
  82. memory = result;
  83. calc.printAns(String.valueOf(result));
  84. break;
  85. case "inverse sine":
  86. result = calc.iSin(num1);
  87. memory = result;
  88. calc.printAns(String.valueOf(result));
  89. break;
  90. case "degree to radian":
  91. result = calc.degToRad(num1);
  92. memory = result;
  93. calc.printAns(String.valueOf(result));
  94. break;
  95. case "radian to degree":
  96. result = calc.radToDeg(num1);
  97. memory = result;
  98. calc.printAns(String.valueOf(result));
  99. break;
  100. case "fortune cookie":
  101. }
  102. }
  103. }
  104. }
  105. }