Operations.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import java.util.Scanner;
  2. import java.lang.*;
  3. public class Operations{
  4. Scanner stdin = new Scanner(System.in);
  5. public double chooseOp(double num){
  6. boolean isValid = false;
  7. int choice;
  8. double result = 0;
  9. while(!isValid){
  10. System.out.println("\n-------------------------------------------------");
  11. System.out.println(num);
  12. System.out.println("-------------------------------------------------");
  13. System.out.println("Select type of operation to perform (enter # 1-3)");
  14. System.out.println("(1) Core Functions");
  15. System.out.println("(2) Trigonometry");
  16. System.out.println("(3) Advanced Functions");
  17. System.out.println("-------------------------------------------------");
  18. System.out.print("> ");
  19. choice = stdin.nextInt();
  20. switch(choice){
  21. case 1: result = coreFunc(num);
  22. isValid = true;
  23. break;
  24. case 2: result = trigFunc(num);
  25. isValid = true;
  26. break;
  27. case 3: result = advFunc(num);
  28. isValid = true;
  29. break;
  30. default:
  31. break;
  32. }
  33. }
  34. return result;
  35. }
  36. public double coreFunc(double num){ //take displayNum as input parameter
  37. boolean isValid = false;
  38. int choice;
  39. double entNum, result = 0;
  40. while(!isValid){
  41. System.out.println("\n-------------------------------------------------");
  42. System.out.println(num);
  43. System.out.println("-------------------------------------------------");
  44. System.out.println("Select core function to perform (enter # 1-9)");
  45. System.out.println("(1) Add");
  46. System.out.println("(2) Subtract");
  47. System.out.println("(3) Multiply");
  48. System.out.println("(4) Divide");
  49. System.out.println("(5) Square");
  50. System.out.println("(6) Square Root");
  51. System.out.println("(7) Exponentiation");
  52. System.out.println("(8) Inverse (1/x)");
  53. System.out.println("(9) Change sign (+/-)");
  54. System.out.println("---------------------------------------------");
  55. System.out.print("> ");
  56. choice = stdin.nextInt();
  57. switch(choice){
  58. case 1: System.out.println("Enter number to add to " + num + ": ");
  59. System.out.print("> ");
  60. entNum = stdin.nextDouble();
  61. result = num + entNum;
  62. isValid = true;
  63. break;
  64. case 2: System.out.println("Enter number to subtract from " + num + ": ");
  65. System.out.print("> ");
  66. entNum = stdin.nextDouble();
  67. result = num - entNum;
  68. isValid = true;
  69. break;
  70. case 3: System.out.println("Enter number to multiply to " + num + ": ");
  71. System.out.print("> ");
  72. entNum = stdin.nextDouble();
  73. result = num * entNum;
  74. isValid = true;
  75. break;
  76. case 4: System.out.println("Enter number to divide from " + num + ": ");
  77. System.out.print("> ");
  78. entNum = stdin.nextDouble();
  79. result = num / entNum; //use Double.isNaN(x)
  80. isValid = true;
  81. break;
  82. case 5: System.out.println("Squaring number on display (" + num + "^2) ");
  83. System.out.print("> ");
  84. result = num * num;
  85. isValid = true;
  86. break;
  87. case 6: System.out.println("Taking square root of " + num);//cant take negative #
  88. System.out.print("> ");
  89. result = Math.sqrt(num);
  90. isValid = true;
  91. break;
  92. case 7: System.out.println("Enter exponent to raise " + num + " to (" + num + "^n)");
  93. System.out.print("> ");
  94. entNum = stdin.nextDouble();
  95. result = num * entNum;
  96. isValid = true;
  97. break;
  98. case 8: System.out.println("Calculating inverse of " + num + " (1/" + num + ")");
  99. System.out.print("> ");
  100. result = 1/num;
  101. isValid = true;
  102. break;
  103. case 9: System.out.println("Calculating negation of " + num + " (n -> -n , -n -> n)");
  104. System.out.print("> ");
  105. result = num * -1;
  106. isValid = true;
  107. break;
  108. default:
  109. break;
  110. }
  111. }
  112. return result;
  113. }
  114. public double trigFunc(double num){ //take displayNum as input parameter
  115. boolean isValid = false;
  116. int choice;
  117. double result = -0;
  118. while(!isValid){
  119. System.out.println("\n-------------------------------------------------");
  120. System.out.println(num);
  121. System.out.println("-------------------------------------------------");
  122. System.out.println("Select trigonemtric function (enter # 1-6)");
  123. System.out.println("(1) Sin");
  124. System.out.println("(2) Cos");
  125. System.out.println("(3) Tangent");
  126. System.out.println("(4) ArcSin");
  127. System.out.println("(5) ArcCos");
  128. System.out.println("(6) ArcTan");
  129. System.out.println("--------------------------------------------");
  130. System.out.print("> ");
  131. choice = stdin.nextInt();
  132. switch(choice){
  133. case 1: System.out.println("Calculating sine of " + num);
  134. result = Math.sin(num);
  135. isValid = true;
  136. break;
  137. case 2: System.out.println("Calculating cosine of " + num);
  138. result = Math.cos(num);
  139. isValid = true;
  140. break;
  141. case 3: System.out.println("Calculating tangent of " + num);
  142. result = Math.tan(num);
  143. isValid = true;
  144. break;
  145. case 4: System.out.println("Calculating inverse sine (arcSin) of " + num);
  146. result = Math.asin(num);
  147. isValid = true;
  148. break;
  149. case 5: System.out.println("Calculating inverse cosine (arcCos) of " + num);
  150. result = Math.acos(num);
  151. isValid = true;
  152. break;
  153. case 6: System.out.println("Calculating inverse tangent (arcTan) of " + num);
  154. result = Math.atan(num);
  155. isValid = true;
  156. break;
  157. }
  158. }
  159. return result;
  160. }
  161. public double advFunc(double num){ //take displayNum as input parameter
  162. boolean isValid = false;
  163. int choice;
  164. double result = 0;
  165. while(!isValid){
  166. System.out.println("\n-------------------------------------------------");
  167. System.out.println(num);
  168. System.out.println("-------------------------------------------------");
  169. System.out.println("Select advanced function to perform (enter # 1-7)");
  170. System.out.println("(1) Factorial");
  171. System.out.println("(2) Log");
  172. System.out.println("(3) Inverse log (10^x)");
  173. System.out.println("(4) Natural log (Ln x)");
  174. System.out.println("(5) Inverse Natural log (e^x)");
  175. System.out.println("(6) Cubed root"); //add custom 1
  176. System.out.println("(7) Absolute value");//add custom 2
  177. System.out.println("-------------------------------------------------");
  178. System.out.print("> ");
  179. choice = stdin.nextInt();
  180. switch(choice){
  181. case 1: System.out.println("Calculating factorial of " + num);
  182. int i;
  183. double fct = 1;
  184. for(i=2; i<num; i++){
  185. fct *= i;
  186. }
  187. result = fct;
  188. isValid = true;
  189. break;
  190. case 2: System.out.println("Calculating log of " + num);
  191. result = Math.log10(num);
  192. isValid = true;
  193. break;
  194. case 3: System.out.println("Calculating inverse log (10^x) of " + num);
  195. result = Math.pow(10 , Math.log10(num));
  196. isValid = true;
  197. break;
  198. case 4: System.out.println("Calculating natural log (Ln) of " + num);
  199. result = Math.log(num);
  200. isValid = true;
  201. break;
  202. case 5: System.out.println("Calculating inverese natural log (e^x) of " + num);
  203. result = Math.exp(num);
  204. isValid = true;
  205. break;
  206. case 6: System.out.println("Calculating cubed root of " + num);
  207. result = Math.cbrt(num);
  208. isValid = true;
  209. break;
  210. case 7: System.out.println("Calculating absolute value of " + num);
  211. result = Math.abs(num);
  212. isValid = true;
  213. break;
  214. default:
  215. break;
  216. }
  217. }
  218. return result;
  219. }
  220. }