Name______________________________

Data Structures and Algorithms I - Lab 4

This lab covers some of the important ideas related to Java exceptions. You should print a copy of this lab so you can record your answers to questions. Turn in the completed questions and any supporting printouts as required in the lab. Here is a link to the API documentation in case you need it.

P1: Throwing Exceptions

The BankAccount class has three methods with preconditions:

1. the constructor (initial balance must not be negative)
2. the withdraw method (withdrawal amount must not be greater than the balance and must not be non-negative)
3. the deposit method (deposit amount must not be negative)

Create a project and folder for this lab, create a new Java file with the provided code, and then modify the code for the BankAccount class so that each of the three methods throws an IllegalArgumentException (supply a descriptive message to the constructor) if the precondition is violated.

Write a driver program (in a separate class file named TestBankAccount.java) that asks the user for:

Have the driver program construct a BankAccount object and make a deposit followed by a withdrawal. Supply test values to simulate each of the following errors and tell happens in each case (tell the error that is reported)?

1. Construct an account with a negative balance

Answer:

 


2. Withdraw more money than the account balance

Answer:

 


3. Enter an illegal input (such as "ten" instead of "10")

Answer:

 

Are the exceptions above classified as checked exceptions or unchecked exceptions? Explain (Hint - what is the parent class?).

Answer:

 

 

P2. Custom Exceptions

It is often a good idea to define your own exception class rather than rely on a predefined class.

Define (as a second class in the BankAccount file) a class named BankAccountException that can be used by the BankAccount class. This exception must allow a custom message to be supplied to the Exception object via the constructor. Your exception should be an unchecked exception. What class should be extended to create an unchecked exception?

Answer: __________________________________________

Now modify the BankAccount class to throw BankAccountException objects instead of IllegalArgumentException objects. Test your program and record the exception message if you attempt to deposit a negative amount.

Answer:

 

 

If the BankAccountException class is supposed to be a checked exception, then what changes would you need to make

1. in the definition of the BankAccountException class?

Answer:


2. in the BankAccount methods?

Answer:

 

Print the two files (be sure your name is found in each file).

P3. Propagating Checked Exceptions

The DateFormat class of the java.text package has a method

public Date parse(String source) throws ParseException; 

that converts a string such as

"2/27/02 8:00 AM" 

into an object of type Date:

DateFormat formatter = DateFormat.getDateTimeInstance( 
   DateFormat.SHORT, DateFormat.SHORT); 
String source = . . .; 
Date d = formatter.parse(source); 

If the source string is not in the correct format, then the parse method throws a ParseException. In this exercise, you will implement a class Appointment that stores the date and description of an appointment. Create a new file and insert the given Appointment code. Supply a constructor with the following heading

public Appointment(String aDate, String aDescription){... 

that constructs an Appointment object from two strings. Notice that the constructor must initialize the two members, date and description. One of these initializations will require the use of the parse method described above. When you compile this class (after adding the constructor), what error do you get?

Answer:

 

If the first string is not in a legal format, the formatter's parse method will throw an exception. Your constructor MUST deal with this. Why? What category of exception (checked or unchecked) does ParseException belong to? What is its parent class?

Answer:

The parent of ParseException is _______________________

This means ParseException is a(n) ______________________ exception.

To handle the exception, your constructor should merely pass this exception out to the next level. What must be added to the constructor heading to accomplish this? Note: class ParseException is found in java.text.

Answer:


Ensure that your class compiles without error before going on to part 4. One more thing about the DateFormat class. It is an abstract class! No object of this data type can be instantiated! What data type is the member formatter? What is the actual type of the object stored in this variable at runtime? Hint: Try displaying the object as a String or use the inherited getClass method and then display this object as a String. The code to do this must appear in your printed program; add a simple main method to test this.

Answer:

Data type of formatter: _______________________

Data type of the object referred to by formatter at runtime: ________________________________

Lookup the DateFormat class in the API (be careful not to look at DateFormatter by mistake). Find the list of all known subclasses. Copy that list here:

Direct known subclasses of DataFormat: ___________________________________

P4. Catching Exceptions

Write a test program that asks the user to enter a series of appointments and stores them in an appointment book Use this class for AppointmentBooks. You would normally do the testing in a main of an entirely separate class, but for the lab, you can simply do it in a main method of AppointmentBook. After you fix any syntax errors unrelated to exceptions, explain why the compiler complains about an uncaught exception.

Answer:

 

If a parse error occurs during the add method, main should have the program instruct the user to reenter the appointment. Add the appropriate try/catch block and test your program. Do not add or remove any throws clauses. Be sure to try adding an illegal appointment (invalid date) when testing the program.

Print the two files created for this part of the lab.

Be sure your files contain your name and the current date. Submit your answers and program solutions in a single stapled package.