1.

P1. Throwing Exceptions

The BankAccount class has three methods with preconditions:

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

Modify the code for the BankAccount class so that each of the three methods throws an IllegalArgumentException if the precondition is violated.

What is the code for your modified BankAccount class?


Answer:


2.

Write a driver program that asks the user for:

   the initial balance
   an amount to deposit
   an amount to withdraw

Have the program construct a BankAccount object and make the deposit and withdrawal.

What is your driver program?


Answer:


3.

What happens when you make the following errors?

   1. Construct an account with a negative balance
   2. Withdraw more money than the account balance
   3. Enter an illegal input (such as "ten" instead of "10")


Answer:


4.

P2. Custom Exceptions

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

Define a class BankAccountException that can be used by the BankAccount class.

Your exception should be an unchecked exception.

What is the code for your exception class?


Answer:


5.

Why does it make sense to define the BankAccountException class as an unchecked exception class?


Answer:


6.

Now modify the BankAccount class to throw BankAccountException objects instead of IllegalArgumentException objects.

What is the code for your modified class?


Answer:


7.

If the BankAccountException class had been a checked exception, then what changes would you need to make

   1. in the definition of the exception class?
   2. in the BankAccount methods?


Answer:


8.

P3. Propagating Checked Exceptions

The DateFormat class of the standard Java library has a method

Date parse(String source)

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, which is a checked exception.

In this exercise, you will implement a class Appointment that stores the date and description of an appointment:

public class Appointment
{
  . . .
  public String toString()
  {
     return "Appointment[date="
       + formatter.format(date)
       + "description=" + description
       + "]"
  }

  private Date date;
  private String description;
  private static DateFormat formatter
     = DateFormat.getDateTimeInstance(
        DateFormat.SHORT, DateFormat.SHORT);
}

Supply a constructor

Appointment(String aDate, String aDescription)

that constructs an Appointment object from two strings. If the first string is not in a legal format, your constructor should throw a

ParseException.


Answer:


9.

P4. Catching Exceptions

Write a program AppointmentBookTest whose main method asks the user to enter a series of appointments. Add the appointments into an appointment book object. If a parse error occurs in the add method, have the program instruct the user to reenter the appointment.

Here is an outline of the main method, which does not yet catch any exceptions.

boolean done = false;
while (!done)
{
  String input1 = JOptionPane.showInputDialog("Next date (or cancel)");
  if (input1 == null) done = true;
  else
  {
     String input2 = JOptionPane.showInputDialog("Description");
     book.add(input1, input2);
  }
}
System.out.println(book);

Add the appropriate try/catch block and test your program.

What is the complete code for your AppointmentBookTest class?


Answer:


10.

The preceding program is tedious to use because the user must type all appointment data into a series of dialog boxes. Improve the method so that the data can be stored in a file.

The simplest way to do this is to use input redirection. Put the data in a text file, say, appts.txt, and then run the program from a command shell such as

java AppointmentBookTest < appts.txt

The < shell operator links the standard input stream System.in to a file.

To read the input one line at a time, you need to turn System.in into a BufferedReader. Use the following class:

public class AppointmentBookReader
{
  public AppointmentBookReader(InputReader reader)
  {
     in = new BufferedReader(reader);
     book = new AppointmentBook();
  }

  public AppointmentBook read()
     throws IOException, ParseException
  {
     boolean done = false;
     while (!done)
     {
        String input1 = in.readLine();
        if (input1 == null) done = true;
        else
        {
           String input2 = in.readLine();
           book.add(input1, input2);
        }
     }
     return book;
  }
  private BufferedReader in;
  private AppointmentBook book;
}

To read from an appointment book, use the following code:

   AppointmentBookReader bookReader
      = new AppointmentBookReader(new InputStreamReader(
         System.in));
   AppointmentBook book = bookReader.read();
   System.out.println(book);

Now change the AppointmentBookTest program so that it uses an AppointmentBookReader.

If there is any exception, describe the nature of the exception in English. Print the contents of the appointment book at the end of your program, whether or not there is any exception.

What is the complete code for your AppointmentBookTest class?


Answer:


11.

Give an example of an input file that causes the AppointmentBookReader to throw a ParseException.


Answer:


12.

Give an example of an input file that causes the AppointmentBookReader to throw an IOException.


Answer:


13.

P5. The finally Clause

The preceding example uses input redirection, which requires the program user to start the program from a command line. It would be preferable to be able to prompt the user for the name of a file, then open the file and read the input.

An outline for this code is the following:

String input = JOptionPane.showInputDialog(
  "Filename:");
  AppointmentBookReader bookReader
     = new AppointmentBookReader(new FileReader(input));
  AppointmentBook book = bookReader.read();
  System.out.println(book);

Modify the AppointmentBookTest program so that it prompts the user for a file name and reads the appointments from the given file. If there is an error in the file, prompt the user for another file name. If the user cancels the input dialog, quit the program.

What is the complete code for your AppointmentBookTest class?


Answer:


14.

When there is an input error and the AppointmentBookReader class exits with an exception, it should close its BufferedReader object by calling in.close()

Modify the read method of the AppointmentBookReader class to close in in a finally clause.

What is the code for your modified read method?


Answer: