MathMethods.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. public static String switchDisplayMode(String mode){
  64. Calculator display = new Calculator();
  65. String answer = "";
  66. switch (mode) {
  67. case "binary":
  68. answer = "0b " + Integer.toBinaryString((int)display.lastAns);
  69. break;
  70. case "octal":
  71. answer = "0 " + Integer.toOctalString((int)display.lastAns);
  72. break;
  73. case "decimal":
  74. answer = String.valueOf(display.lastAns);
  75. break;
  76. case "hexadecimal":
  77. answer = "0x " + Integer.toHexString((int)display.lastAns);
  78. break;
  79. }
  80. return answer;
  81. }
  82. public String switchDisplayMode(){
  83. String answer = "";
  84. if (displayModeCounter%4 == 0){
  85. answer = switchDisplayMode("binary");
  86. displayModeCounter++;
  87. }
  88. else if (displayModeCounter%4 == 1){
  89. answer = switchDisplayMode("octal");
  90. displayModeCounter++;
  91. }
  92. else if (displayModeCounter%4 == 2) {
  93. answer = switchDisplayMode("hexadecimal");
  94. displayModeCounter++;
  95. } else {
  96. answer = switchDisplayMode("decimal");
  97. displayModeCounter++;
  98. }
  99. return answer;
  100. }
  101. public static double randomNumber() {
  102. Random rand = new Random();
  103. return rand.nextDouble()*101;
  104. }
  105. public static void printAns(String printAnswer){
  106. System.out.println( "ANSWER: " + printAnswer + " \n \n");
  107. }
  108. public static double inverse(double n1) {
  109. return 1 / n1;
  110. }
  111. public static double tangent(double n1) {
  112. return Math.tan(n1);
  113. }
  114. public static double iTan(double n1) {
  115. return Math.atan(n1);
  116. }
  117. public static double state() {
  118. Calculator display = new Calculator();
  119. return (display.lastAns);
  120. }
  121. public static void roundTo(Double memory, int numPlaces){
  122. System.out.printf("ANSWER: %." + (numPlaces)+ "f\n", memory);
  123. }
  124. }