Lab Assignment #8 Activities Solution

1. Answer the questions in the spaces provided or attach a sheet with your answers (be sure to label questions for proper credit) (2 points each, unless noted otherwise)
Your Answer
a. What classes (and their subclasses) are examples of unchecked exceptions?
b. What are the two different ways that a programmer can deal with checked exceptions to avoid compiler errors? (4 points)
Option #1:
Option #2:
CIS 2571: Introduction to Java
Lab Assignment
College of DuPage 2 CREngland
c. Describe the steps that occur when an exception is not caught in the current method.
d. How is a ‘chained exception’ different from an exception that has been rethrown?
2. You will modify a version of the Account class from previous labs to include exception handling and file output. The file will be given to you to modify.
a) Create a custom exception class called InvalidBalanceException that is a subclass of the Exception class. It should be a public class in its own file and should contain a private data member (and public accessor method) to hold the invalid balance. The InvalidBalanceException constructor takes an argument of an invalid account balance and invokes the superclass constructor with a descriptive message that includes the invalid balance amount. (10 points)
b) Modify a copy of the class file (given in class) to throw your newly created exception class when an illegal balance (< 0.0) is attempted to be set for an account. The public class, and file, should be called AccountWithException. (14 points)
c) Create a public TestAccountWithException class, in its own java file, with a main method that accomplishes the following program requirements:
o Prompt the user for an output filename. Create a file for text output (See 14.11.1 Writing Data Using PrintWriter). (10 points)
 If the file exists, prompt the user for a new filename.
 Do not continue until a valid, non-existing, filename has been given and the file opened for output.
o Prompt the user for a first name, last name, and balance (account id and annual interest rate can be constants). Create an AccountWithException object that includes the user specified data. (4 points)
o When valid data exists in the AccountWithException object (i.e. InvalidBalanceException has not been thrown), output the object information to the display as well as the opened output file. Use the overloaded toString() method when outputting the data. (6 points)
CIS 2571: Introduction to Java
Lab Assignment
College of DuPage 3 CREngland
o Use the finally block to ensure the output file has been properly closed. (2 points)
o Include exception handling to catch any thrown InvalidBalanceExceptions or FileNotFound Exceptions and display unique output identifying the specific exception. (4 points)
o Use either the GUI or console for both input and display output of account information. (2 points)
d) Add a block comment at the top of each file to identify your name, file, date, class, assignment, and short description of the included class. Use proper code alignment for full credit. (3 points total, 1 point each)
e) Compile the source code until no errors are found.
• Common Errors:
f) Run the Java bytecode and observe the results.
g) Attach a hardcopy printout of your source code files.
h) Attach a hardcopy printout of your sample output of console/GUI and file for the following test cases (label each appropriately): (3 points)
• valid balance
• invalid balance
• generated output receipt
i) Create a .zip file containing only your .java source code files. Upload a copy of this .zip file to the appropriate assignment in Blackboard (this will be demonstrated during class, if necessary). (2 points)
Powered by