Duo Collab

Main.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import java.util.Scanner;
  2. import java.util.*;
  3. import java.lang.Math;
  4. public class Main{
  5. static final List<String> button1 = Arrays.asList("(-)", "sq", "sqrt", "cos", "sin", "tan", "-cos", "-sin", "-tan");
  6. static final List<String> TRIG = Arrays.asList("cos", "sin", "tan", "-cos", "-sin", "-tan");
  7. public static void main(String[] args){
  8. //Calls class Calculator, Memory, Display
  9. Calculator calc = new Calculator();
  10. Memory memo = new Memory();
  11. Display disp = new Display();
  12. boolean isRadians = true;
  13. //User input
  14. Scanner reader = new Scanner(System.in);
  15. Console.println("Welcome to Calculator!\n");
  16. Console.println("Operations:");
  17. Console.println("|Clear |(-) |OFF |+ |- |* |/ |");
  18. Console.println("|Cos |Sin |Tan |-Cos |-Sin |-Tan |Sq |SqRt |");
  19. Console.println("|! |^ |log |-log |ln |M+ |MC |MRC |");
  20. Console.println("|round |deg |rad |%% | Display Mode |");
  21. double value = 0;
  22. while(true){
  23. // conditional ? ifTrue : ifFalse
  24. String trigMode = isRadians ? "Rad" : "Deg";
  25. String valueStr = disp.getDisplayString(isRadians ? Math.toRadians(value) : value);
  26. Console.println("\nDeg/Rad: [%s]; Mode: [%s] Value: [%s]", trigMode, disp.getDisplayMode(), disp.getDisplayString(value), valueStr);
  27. Console.println("Pick Operation:");
  28. String button = reader.nextLine().toLowerCase();
  29. if(button.equals("display mode")){
  30. Console.println("dec | bin | oct | hex | pct");
  31. String mode = reader.nextLine().toLowerCase();
  32. switch(mode){
  33. case "bin":
  34. disp.setDisplayMode(Display.DisplayMode.BIN);
  35. break;
  36. case "oct":
  37. disp.setDisplayMode(Display.DisplayMode.OCT);
  38. break;
  39. case "hex":
  40. disp.setDisplayMode(Display.DisplayMode.HEX);
  41. break;
  42. case "dec":
  43. disp.setDisplayMode(Display.DisplayMode.DEC);
  44. break;
  45. case "pct":
  46. disp.setDisplayMode(Display.DisplayMode.PCT);
  47. default:
  48. break;
  49. }
  50. continue; //starts while loop over
  51. }
  52. if(TRIG.contains(button)){
  53. // value variable will always be stored in DEGREES... convert.
  54. double radians = Math.toRadians(value);
  55. //result will be in radians
  56. double result = value;
  57. switch(button) {
  58. case "cos":
  59. result = calc.getCosine(value);
  60. break;
  61. case "sin":
  62. result = calc.getSine(radians);
  63. break;
  64. case "tan":
  65. result = calc.getTan(radians);
  66. break;
  67. case "-cos":
  68. result = calc.inverseCos(radians);
  69. break;
  70. case "-sin":
  71. result = calc.inverseSin(radians);
  72. break;
  73. case "-tan":
  74. result = calc.inverseTan(radians);
  75. break;
  76. default:
  77. result = radians;
  78. Console.println("Invalid trig operator");
  79. break;
  80. }
  81. // result is in radians, value is always saved in degrees
  82. value = Math.toDegrees(result);
  83. continue;
  84. }
  85. if(button.equalsIgnoreCase("OFF")){
  86. Console.println("Peace.");
  87. break;
  88. }else if(button.equalsIgnoreCase("Clear")){
  89. value = 0;
  90. continue;
  91. }else if(button.equalsIgnoreCase("M+")){
  92. memo.saveNumber(value);
  93. Console.println("SAVED");
  94. continue;
  95. }else if(button.equalsIgnoreCase("MC")){
  96. memo.clear();
  97. Console.println("MEMORY CLEARED");
  98. continue;
  99. }
  100. if(button1.contains(button)){
  101. switch(button){
  102. case "(-)":
  103. value = calc.invert(value);
  104. break;
  105. case "sq":
  106. value = calc.square(value);
  107. break;
  108. case "sqrt":
  109. value = calc.squareRoot(value);
  110. break;
  111. case "!":
  112. value = calc.factorial(value);
  113. break;
  114. case "log":
  115. value = calc.log(value);
  116. break;
  117. case "-log":
  118. value = calc.inverseLog(value);
  119. break;
  120. case "ln":
  121. value = calc.natLog(value);
  122. break;
  123. case "round":
  124. value = calc.round(value);
  125. break;
  126. }
  127. }else{
  128. double y = Console.getDoubleInput("Input Number:");
  129. switch(button){
  130. case "+":
  131. value = calc.add(value, y);
  132. break;
  133. case "-":
  134. value = calc.subtract(value, y);
  135. break;
  136. case "*":
  137. value = calc.multiply(value, y);
  138. break;
  139. case "/":
  140. value = calc.divide(value, y);
  141. break;
  142. case "^":
  143. value = calc.getPow(value, y);
  144. break;
  145. default:
  146. Console.print("Err");
  147. break;
  148. }
  149. }
  150. if(button.equalsIgnoreCase("MRC")){
  151. value = memo.recall();
  152. if(memo.recall() > 0){
  153. double y = value;
  154. }else{
  155. continue;
  156. }
  157. }
  158. continue;
  159. }
  160. }
  161. }