MathMethods.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import java.util.*;
  2. import java.lang.*;
  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 class MathMethods
  10. {
  11. private static int displayModeCounter=4;
  12. public static Double add(double n1, double n2){
  13. return (n1 + n2);
  14. }
  15. public static Double subtract(double n1, double n2){
  16. return (n1 - n2);
  17. }
  18. public static Double multiply(double n1, double n2){
  19. return (n1 * n2);
  20. }
  21. public static Double divide(double n1, double n2){
  22. return (n1 / n2);
  23. }
  24. public static Double exponent(double n1, double n2){
  25. return (Math.pow(n1, n2));
  26. }
  27. public static Double cosine(double n1){
  28. return (Math.cos(n1));
  29. }
  30. public static Double invCosine(double n1){
  31. return (Math.acos(n1));
  32. }
  33. public static double squareRoot(double n1) {
  34. return (Math.sqrt(n1));
  35. }
  36. public static double invert(double n1) {
  37. return (n1*-1);
  38. }
  39. public static double sin(double n1){
  40. return (Math.sin(n1));
  41. }
  42. public static double iSin(double n1){
  43. return (Math.asin(n1));
  44. }
  45. public static double degToRad(double n1) {
  46. return (Math.toRadians(n1));
  47. }
  48. public static double radToDeg(double n1) {
  49. return (Math.toDegrees(n1));
  50. }
  51. public static String fortuneCookie() {
  52. Random rand = new Random();
  53. String[] wiseWords = new String[] {"You're a hard worker and you will do great on all the labs!",
  54. "Look how much java you learned this week! That's awesome!",
  55. "Keep up the good work!" ,
  56. "You're cushing it!", "You're a coding rockstar!",
  57. "Keep coding!"};
  58. int arrIndex = rand.nextInt(6);
  59. return wiseWords[arrIndex];
  60. }
  61. public static double square(double n1) {
  62. return Math.pow(n1, 2);
  63. }
  64. public static String switchDisplayMode(String mode){
  65. Calculator display = new Calculator();
  66. String answer = "";
  67. switch (mode) {
  68. case "binary":
  69. answer = "0b " + Integer.toBinaryString((int)display.lastAns);
  70. break;
  71. case "octal":
  72. answer = "0 " + Integer.toOctalString((int)display.lastAns);
  73. break;
  74. case "decimal":
  75. answer = String.valueOf(display.lastAns);
  76. break;
  77. case "hexadecimal":
  78. answer = "0x " + Integer.toHexString((int)display.lastAns);
  79. break;
  80. }
  81. return answer;
  82. }
  83. public String switchDisplayMode(){
  84. String answer = "";
  85. if (displayModeCounter%4 == 0){
  86. answer = switchDisplayMode("binary");
  87. displayModeCounter++;
  88. }
  89. else if (displayModeCounter%4 == 1){
  90. answer = switchDisplayMode("octal");
  91. displayModeCounter++;
  92. }
  93. else if (displayModeCounter%4 == 2) {
  94. answer = switchDisplayMode("hexadecimal");
  95. displayModeCounter++;
  96. } else {
  97. answer = switchDisplayMode("decimal");
  98. displayModeCounter++;
  99. }
  100. return answer;
  101. }
  102. public static double randomNumber() {
  103. Random rand = new Random();
  104. return rand.nextDouble()*101;
  105. }
  106. public static void printAns(String printAnswer){
  107. System.out.println("ANSWER: " + printAnswer + "\n \n");
  108. }
  109. public static double inverse(double n1) {
  110. return 1 / n1;
  111. }
  112. public static double tangent(double n1) {
  113. return Math.tan(n1);
  114. }
  115. public static double iTan(double n1) {
  116. return Math.atan(n1);
  117. }
  118. public static double state() {
  119. Calculator display = new Calculator();
  120. return (display.lastAns);
  121. }
  122. }