123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * Created by Allison & Alfredo 5/25/2018
  3. *
  4. * This class takes strings and returns them as numbers
  5. * based on the display mode and trig units mode for
  6. * ints/doubles and angles respectively.
  7. */
  8. public class NumericInputParser
  9. {
  10. enum TrigUnits {rad, deg};
  11. enum Display {dec, bin, hex, oct};
  12. private TrigUnits trigUnitsMode;
  13. private Display displayMode;
  14. //private String trigUnitsMode; // Will be equal to "rad" or "deg"
  15. //private String displayMode; // Will be equal to "dec", "bin", "hex", or "oct"
  16. /**
  17. * Constructor for objects of class NumericInputParser
  18. */
  19. public NumericInputParser()
  20. {
  21. trigUnitsMode = TrigUnits.rad;
  22. displayMode = Display.dec;
  23. }
  24. //Display Mode methods
  25. public void switchDisplayMode() {
  26. String displayString = "";
  27. switch (displayMode){
  28. case dec :
  29. displayMode = Display.bin;
  30. displayString = "bin";
  31. break;
  32. case bin :
  33. displayMode = Display.hex;
  34. displayString = "hex";
  35. break;
  36. case hex :
  37. displayMode = Display.oct;
  38. displayString = "oct";
  39. break;
  40. case oct :
  41. displayMode = Display.dec;
  42. displayString = "dec";
  43. }
  44. //return displayString;
  45. }
  46. public void switchDisplayMode(String mode) {
  47. String displayString = "";
  48. //displayMode = mode;
  49. switch (mode){
  50. case "dec" :
  51. displayMode = Display.dec;
  52. break;
  53. case "bin" :
  54. displayMode = Display.bin;
  55. break;
  56. case "hex" :
  57. displayMode = Display.hex;
  58. break;
  59. case "oct" :
  60. displayMode = Display.oct;
  61. }
  62. //return displayString;
  63. }
  64. public String getDisplayMode() {
  65. String displayString = "";
  66. switch (displayMode){
  67. case dec :
  68. displayString = "dec";
  69. break;
  70. case bin :
  71. displayString = "bin";
  72. break;
  73. case hex :
  74. displayString = "hex";
  75. break;
  76. case oct :
  77. displayString = "oct";
  78. break;
  79. }
  80. return displayString;
  81. }
  82. //Trig mode methods
  83. public void switchUnitsMode(){
  84. trigUnitsMode = (trigUnitsMode.equals(TrigUnits.rad)) ? TrigUnits.deg : TrigUnits.rad;
  85. }
  86. public void switchUnitsMode(String mode){
  87. trigUnitsMode = (mode.equals("deg")) ? TrigUnits.deg : TrigUnits.rad;
  88. //trigUnitsMode = mode;
  89. //return trigUnitsMode;
  90. }
  91. public String getTrigUnitsMode(){
  92. if (trigUnitsMode == TrigUnits.rad){
  93. return "rad";
  94. }
  95. else {
  96. return "deg";
  97. }
  98. }
  99. public double getDouble(String input) {
  100. double val = 0;
  101. if (input.equalsIgnoreCase("pi")) {
  102. val = Math.PI;
  103. }
  104. else if (input.equalsIgnoreCase("e")) {
  105. val = Math.E;
  106. }
  107. else {
  108. switch (displayMode) {
  109. case dec :
  110. val = Double.parseDouble(input);
  111. break;
  112. case bin :
  113. val = Integer.parseInt(input, 2);
  114. break;
  115. case hex :
  116. val = Integer.parseInt(input, 16);
  117. break;
  118. case oct :
  119. val = Integer.parseInt(input, 8);
  120. }
  121. }
  122. return val;
  123. }
  124. //Because of the way this works, all input and results outside of
  125. //base 10 will be truncated for display (the Calculator still holds doubles)
  126. public String getFormattedNumber(double input){
  127. String formatted = "";
  128. switch (displayMode) {
  129. case dec :
  130. formatted = String.format("%.7f",input);
  131. break;
  132. case bin :
  133. formatted = Integer.toBinaryString((int) input);
  134. break;
  135. case hex :
  136. formatted = Integer.toHexString((int) input);
  137. break;
  138. case oct :
  139. formatted = Integer.toOctalString((int) input);
  140. }
  141. return formatted;
  142. }
  143. //Trigonometry methods
  144. //Because java Math does sin, cos, and tan in radians, all
  145. //numbers passed to and from the Calculator will be in radians.
  146. //These methods ensure proper units for the input on trig functions and
  147. //the output of inverse trig functions
  148. public double getAngle(String input) {
  149. double angle = getDouble(input);
  150. angle = trigUnitsMode.equals(TrigUnits.deg) ? angle*(Math.PI/180) : angle;
  151. return angle;
  152. }
  153. public String getFormattedAngle(double angle) {
  154. double radianAngle = trigUnitsMode.equals(TrigUnits.deg) ? angle*(180/Math.PI) : angle;
  155. return getFormattedNumber(radianAngle);
  156. }
  157. }