Lab08 Custom Exception handout

advertisement
Lab8 CS262 Learning Outcomes: Defining Custom Exception
Define a custom exception called BinaryFormatException.
Write a parseBinary method that parses a binary number as a string into a decimal integer
and throws a binaryFormatException if the string is not a binary string. The method
header is as follows:
public static int parseBinary(String binaryString) throws BinaryFormatException
For example, binaryString "1000001" is 65 (1 x 26 + 0 x 25 + 0 x 24 + 0 x 23 + 0 x 22 + 0 x
21 + 1 x 20= 65). The binaryString "11011B111" will raise a BinaryFormatException.
The BinaryFormatException class has two constructors shown below.
Your test program may look like the following:
/**
* This class contains a static parseBinary() method with the following signature:<br>
* public static int parseBinary(String binaryString) throws BinaryFormatException <br>
* and serves as a test program of the custom-defined BinaryFormatException class.
*
* @author (Wing Huen)
* @version (Lab8 CS262 F09)
*/
public class Lab08_Huen {
/**
* Test program for method parseBinary.
*/
public static void main(String[] args) throws BinaryFormatException {
System.out.println(parseBinary("1000001"));
System.out.println(parseBinary("11011B111"));
}
/**
* This method parseBinary() parses a binary number
* as a string into a decimal integer.
*/
public static int parseBinary(String binaryString) throws BinaryFormatException {
int value = 0;
// implement this
return value;
}
}
Sample Test result:
What to submit
To get full credit, you must correctly solve the problem and complete all the following
tasks. You will get bonus points for finishing the lab during the lab period. The due time
is indicated below. Late labs will not be accepted or graded.
 Demo your program to the instructor or the lab assistant during this lab or
BEFORE the next lab.
 Document your classes and methods with the javadoc convention.
 Copy your Lab08_Last_First folder to Q:\Shared\Huen\262\Submit\Lab08
 Finally, remember to keep a copy of your lab in your U drive.
Good luck and have fun!
If you finish early, here is a challenge:
1. Extend your program to test for octal strings. An octal number has the base 8 and
hence is one that can only have digits 0, 1, 2, 3, 4, 5, 6, or 7.
2. Enter the test string in the command line.
Download