Created
May 16, 2017 20:00
-
-
Save mkmozgawa/daa49c4546a637be757198f884d41692 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package consoleConverter; | |
class BaseOutOfBoundException extends Exception { | |
public String getMessage() { | |
return ("Base out of bound"); | |
} | |
} | |
class NumberUninterpretableException extends Exception { | |
public String getMessage() { | |
return ("Number uninterpretable"); | |
} | |
} | |
public class Converter { | |
private String number; | |
private int baseFrom; | |
private int baseTo; | |
public Converter(String number, int baseFrom, int baseTo) { | |
this.number = number; | |
this.baseFrom = baseFrom; | |
this.baseTo = baseTo; | |
} | |
public String convertBase() throws BaseOutOfBoundException, NumberUninterpretableException | |
{ | |
if (2 > this.baseFrom || this.baseFrom > 16 || 2 > this.baseTo || this.baseTo > 16) { | |
throw new BaseOutOfBoundException(); | |
} | |
try { | |
return Integer.toString(Integer.parseInt(this.number, this.baseFrom),this.baseTo); | |
} catch (NumberFormatException e) { | |
throw new NumberUninterpretableException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment