/** * Created by Allison 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 { 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 = "rad"; displayMode = "dec"; } //Display Mode methods public String switchDisplayMode() { switch (displayMode){ case "dec" : displayMode = "bin"; break; case "bin" : displayMode = "hex"; break; case "hex" : displayMode = "oct"; break; case "oct" : displayMode = "dec"; } return displayMode; } public String switchDisplayMode(String mode) { displayMode = mode; return displayMode; } public String getDisplayMode() { return displayMode; } //Trig mode methods public String switchUnitsMode(){ trigUnitsMode = (trigUnitsMode == "rad") ? "deg" : "rad"; //println("Trig units changed to %s", trigUnitsMode); return trigUnitsMode; } public String switchUnitsMode(String mode){ trigUnitsMode = mode; //println("Trig units changed to %s", trigUnitsMode); return trigUnitsMode; } public String getTrigUnitsMode(){ return trigUnitsMode; } public double getDouble(String input) { double val = 0; switch (displayMode) { case "dec" : val = Integer.parseInt(input, 10); 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; } public int getInt(String input) { int val = 0; switch (displayMode) { case "dec" : val = Integer.parseInt(input, 10); break; case "bin" : val = Integer.parseInt(input, 2); break; case "hex" : val = Integer.parseInt("#" + input.toUpperCase(), 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 public String getFormattedNumber(double input){ String formatted = ""; switch (displayMode) { case "dec" : formatted = Double.toString(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 //arguments passed to Calculator will be in radians. public double getAngle(String input) { double angle = Double.parseDouble(input); angle = trigUnitsMode == "deg" ? (angle*(Math.PI/180)) : angle; return angle; } public String getFormattedAngle(double angle) { String angleString = trigUnitsMode == "deg" ? Double.toString(angle*(180/Math.PI)) : Double.toString(angle); return angleString; } }