123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
-
- /**
- * Created by Allison & Alfredo 5/25/2018
- *
- * This class takes strings and returns them as numbers
- * based on the display mode and trig units mode for
- * ints/doubles and angles respectively.
- */
- public class NumericInputParser
- {
- enum TrigUnits {rad, deg};
- enum Display {dec, bin, hex, oct};
-
- private TrigUnits trigUnitsMode;
- private Display displayMode;
- //private String trigUnitsMode; // Will be equal to "rad" or "deg"
- //private String displayMode; // Will be equal to "dec", "bin", "hex", or "oct"
-
- /**
- * Constructor for objects of class NumericInputParser
- */
- public NumericInputParser()
- {
- trigUnitsMode = TrigUnits.rad;
- displayMode = Display.dec;
- }
-
- //Display Mode methods
- public void switchDisplayMode() {
- String displayString = "";
- switch (displayMode){
- case dec :
- displayMode = Display.bin;
- displayString = "bin";
- break;
- case bin :
- displayMode = Display.hex;
- displayString = "hex";
- break;
- case hex :
- displayMode = Display.oct;
- displayString = "oct";
- break;
- case oct :
- displayMode = Display.dec;
- displayString = "dec";
- }
- //return displayString;
- }
- public void switchDisplayMode(String mode) {
- String displayString = "";
- //displayMode = mode;
- switch (mode){
- case "dec" :
- displayMode = Display.dec;
- break;
- case "bin" :
- displayMode = Display.bin;
- break;
- case "hex" :
- displayMode = Display.hex;
- break;
- case "oct" :
- displayMode = Display.oct;
- }
- //return displayString;
- }
- public String getDisplayMode() {
- String displayString = "";
- switch (displayMode){
- case dec :
- displayString = "dec";
- break;
- case bin :
- displayString = "bin";
- break;
- case hex :
- displayString = "hex";
- break;
- case oct :
- displayString = "oct";
- break;
- }
- return displayString;
- }
-
-
- //Trig mode methods
- public void switchUnitsMode(){
- trigUnitsMode = (trigUnitsMode.equals(TrigUnits.rad)) ? TrigUnits.deg : TrigUnits.rad;
- }
-
- public void switchUnitsMode(String mode){
- trigUnitsMode = (mode.equals("deg")) ? TrigUnits.deg : TrigUnits.rad;
- //trigUnitsMode = mode;
- //return trigUnitsMode;
- }
- public String getTrigUnitsMode(){
- if (trigUnitsMode == TrigUnits.rad){
- return "rad";
- }
- else {
- return "deg";
- }
- }
-
-
- public double getDouble(String input) {
- double val = 0;
-
- if (input.equalsIgnoreCase("pi")) {
- val = Math.PI;
- }
- else if (input.equalsIgnoreCase("e")) {
- val = Math.E;
- }
- else {
- switch (displayMode) {
- case dec :
- val = Double.parseDouble(input);
- break;
- case bin :
- val = Integer.parseInt(input, 2);
- break;
- case hex :
- val = Integer.parseInt(input, 16);
- break;
- case oct :
- val = Integer.parseInt(input, 8);
- }
- }
- return val;
- }
-
- //Because of the way this works, all input and results outside of
- //base 10 will be truncated for display (the Calculator still holds doubles)
- public String getFormattedNumber(double input){
- String formatted = "";
- switch (displayMode) {
- case dec :
- formatted = String.format("%.7f",input);
- break;
- case bin :
- formatted = Integer.toBinaryString((int) input);
- break;
- case hex :
- formatted = Integer.toHexString((int) input);
- break;
- case oct :
- formatted = Integer.toOctalString((int) input);
- }
- return formatted;
- }
-
- //Trigonometry methods
-
- //Because java Math does sin, cos, and tan in radians, all
- //numbers passed to and from the Calculator will be in radians.
- //These methods ensure proper units for the input on trig functions and
- //the output of inverse trig functions
- public double getAngle(String input) {
- double angle = getDouble(input);
- angle = trigUnitsMode.equals(TrigUnits.deg) ? angle*(Math.PI/180) : angle;
- return angle;
- }
- public String getFormattedAngle(double angle) {
- double radianAngle = trigUnitsMode.equals(TrigUnits.deg) ? angle*(180/Math.PI) : angle;
- return getFormattedNumber(radianAngle);
- }
- }
|