NumericInputParser.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * Created by Allison 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. private String trigUnitsMode; // Will be equal to "rad" or "deg"
  11. private String displayMode; // Will be equal to "dec", "bin", "hex", or "oct"
  12. /**
  13. * Constructor for objects of class NumericInputParser
  14. */
  15. public NumericInputParser()
  16. {
  17. trigUnitsMode = "rad";
  18. displayMode = "dec";
  19. }
  20. //Display Mode methods
  21. public String switchDisplayMode() {
  22. switch (displayMode){
  23. case "dec" :
  24. displayMode = "bin";
  25. break;
  26. case "bin" :
  27. displayMode = "hex";
  28. break;
  29. case "hex" :
  30. displayMode = "oct";
  31. break;
  32. case "oct" :
  33. displayMode = "dec";
  34. }
  35. return displayMode;
  36. }
  37. public String switchDisplayMode(String mode) {
  38. displayMode = mode;
  39. return displayMode;
  40. }
  41. public String getDisplayMode() {
  42. return displayMode;
  43. }
  44. //Trig mode methods
  45. public String switchUnitsMode(){
  46. trigUnitsMode = (trigUnitsMode == "rad") ? "deg" : "rad";
  47. //println("Trig units changed to %s", trigUnitsMode);
  48. return trigUnitsMode;
  49. }
  50. public String switchUnitsMode(String mode){
  51. trigUnitsMode = mode;
  52. //println("Trig units changed to %s", trigUnitsMode);
  53. return trigUnitsMode;
  54. }
  55. public String getTrigUnitsMode(){
  56. return trigUnitsMode;
  57. }
  58. public double getDouble(String input) {
  59. double val = 0;
  60. switch (displayMode) {
  61. case "dec" :
  62. val = Integer.parseInt(input, 10);
  63. break;
  64. case "bin" :
  65. val = Integer.parseInt(input, 2);
  66. break;
  67. case "hex" :
  68. val = Integer.parseInt(input, 16);
  69. break;
  70. case "oct" :
  71. val = Integer.parseInt(input, 8);
  72. }
  73. return val;
  74. }
  75. public int getInt(String input) {
  76. int val = 0;
  77. switch (displayMode) {
  78. case "dec" :
  79. val = Integer.parseInt(input, 10);
  80. break;
  81. case "bin" :
  82. val = Integer.parseInt(input, 2);
  83. break;
  84. case "hex" :
  85. val = Integer.parseInt("#" + input.toUpperCase(), 16);
  86. break;
  87. case "oct" :
  88. val = Integer.parseInt(input, 8);
  89. }
  90. return val;
  91. }
  92. //Because of the way this works, all input and results outside of
  93. //base 10 will be truncated
  94. public String getFormattedNumber(double input){
  95. String formatted = "";
  96. switch (displayMode) {
  97. case "dec" :
  98. formatted = Double.toString(input);
  99. break;
  100. case "bin" :
  101. formatted = Integer.toBinaryString((int) input);
  102. break;
  103. case "hex" :
  104. formatted = Integer.toHexString((int) input);
  105. break;
  106. case "oct" :
  107. formatted = Integer.toOctalString((int) input);
  108. }
  109. return formatted;
  110. }
  111. //Trigonometry methods
  112. //Because java Math does sin, cos, and tan in radians, all
  113. //arguments passed to Calculator will be in radians.
  114. public double getAngle(String input) {
  115. double angle = Double.parseDouble(input);
  116. angle = trigUnitsMode == "deg" ? (angle*(Math.PI/180)) : angle;
  117. return angle;
  118. }
  119. public String getFormattedAngle(double angle) {
  120. String angleString = trigUnitsMode == "deg" ? Double.toString(angle*(180/Math.PI)) : Double.toString(angle);
  121. return angleString;
  122. }
  123. }