Part II Exception Handling 3 points Filenames HexBinDecjava
Part II: Exception Handling (3 points)
Filename(s): HexBinDec.java
Public class: HexBinDec
Package-visible class(es): BinaryFormatException, HexFormatException
CSE 114 – Fall 2016 Homework #7 Page 2
Implementapublicstaticmethodint hex2Dec(String hexString),whichconvertsastringrepresent- ing a hexadecimal number into a decimal number. Assume that the converted number can fit within a 32-bit integer. Your method must be able to handle valid uppercase and lowercase letters, as well as digits. Define a custom exception class HexFormatException which your method throws if the parameter hexString is not a validly formatted hexadecimal number.
Implementapublicstaticmethodint bin2Dec(String binString),whichconvertsastringrepresent- ing a binary number into a decimal number. Assume that the converted number can fit within a 32-bit inte- ger. Define a custom exception class BinaryFormatException which your method throws if the parameter binString is not a validly formatted binary number.
Write a main() method consisting of several try-catch statements which demonstrate:
hex2Dec being called with a valid argument. Print the returned integer to the screen.
hex2Dec being called with an invalid argument. Include the same code for printing that you wrote for the previous test.
bin2Dec being called with a valid argument. Print the returned integer to the screen.
bin2Dec being called with an invalid argument. Include the same code for printing that you wrote for the
previous test.
Solution
public class Conversion {
int decimalValueOf(String hexDigits, char hexDigit) {
return hexDigits.indexOf(hexDigit);
}
public int hex2Dec(String hexString) throws HexFormatException {
// Convert the hexString to uppercase
hexString=hexString.toUpperCase();
int len = hexString.length();
String hexNumeral = \"0123456789ABCDEF\";
int decimalValue = 0;
if(!hexString.matches(\"[0-9A-Fa-f]+$\"))
throw new HexFormatException(\"Hex String is not valid. Pass a valid hex string\");
// Starting from the LSB
for(int i=len-1;i>=0;i--) {
decimalValue+=(int) (Math.pow(16, len-i-1)*decimalValueOf(hexNumeral, hexString.charAt(i)));
}
return decimalValue;
}
public int bin2Dec(String binString) throws BinaryFormatException {
String binaryNumeral = \"01\";
int len=binString.length();
int decimalNum = 0;
if(!binString.matches(\"[01]+$\"))
throw new BinaryFormatException(\"Binary String is not valid. Pass a valid binary string\");
for(int i=len-1;i>=0;i--) {
char c=binString.charAt(i);
int digit=binaryNumeral.indexOf(c);
decimalNum+=digit*(int)Math.pow(2, len-i-1);
}
return decimalNum;
}
public static void main(String[] args) {
Conversion converter = new Conversion();
try {
System.out.println(converter.hex2Dec(\"3FAC6E\"));
System.out.println(converter.bin2Dec(\"111001\"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class HexFormatException extends Exception {
public HexFormatException(String msg) {
super(msg);
}
}
public class BinaryFormatException extends Exception {
public BinaryFormatException(String msg) {
super(msg);
}
}


