Lab 04


DUE DATE:    This lab must be completed by Sunday, 11:55PM, October 14th. Ask your TA to verify that your lab was submitted correctly, and get his/her help if need be before the deadline. You have two weeks to do this lab to help you have time to study for the midterm and complete the next project. However, there may be questions on the midterm about the lab concepts so you should complete the lab as soon as possible.

OBJECTIVE:

This lab will give you experience using exceptions. You will learn how to handle an Exception when it occurs, how to throw exceptions when needed and how to create your own checked exception classes by inherting from Exception. 

GRADING

Total 10 points

BACKGROUND:

Review information on Exceptions -- if needed read through the Java review (chapter 1 in our book). Additionally you may want to review the Java slides (Ch 01) from the syllabus. 

ASSIGNMENT:

You are writing a standard utility class to get a single character as input from a user. For example, you want to ask the user a simple question and get back a single character answer (a, b, c, d). To do this you must implement the interface given in QuestionInterface.java. This library should be designed for use on multiple lab and project assignments.

Classes you create:

Question.java -- This class implements the question interface. It is very basic and throws the exceptions described below when errors occur. Its constructor doesn't need any parameters. (Originally this page had an error saying the constructor did take parameters.)

For example:

char [] validAnswers = new char[] {'y', 'n'};
Question q = new Question();
char answer = q.askQuestion("Is the Earth round?", validAnswers);

NotACharException.java -- This class is a checked exception that is thrown from askQuestion() when the user gives an answer that is longer than a single character. For example the user enters: "no" instead of "n", the code should throw this exception with the message "Only single characaters are allowed. You entered no". The NotACharException should also contain a method to get the actual (incorrect) answer the user did give. This will help later in decided what to do with the Exception. For example, NotACharException should contain a method: public String getActualAnswer(); which in the example above returns "no".

NotAValidCharException.java -- This class is a checked exception that is thrown from askQuestion when the user does enter a single character, but it is NOT one of the validAnswers passed in.

In addition to these exceptions, the askQuestion method also allows the user to essentially halt the program by throwing a RuntimeException if the user enters "xxx". This exception should not be caught in your program! Do you need to catch or specify this exception? No. Why?

Frequently you may want to ask a question until the user gives you a valid answer. The next part is to write a better implementation that uses the Question class, but handles the Exceptions and recovers from them.

SmartQuestion.java - This class also implements the QuestionInterface however it asks the question until it gets a valid answer, thus it does not throw any exceptions from the askQuestion method. This class must use a Question instance internally, catch the exceptions thrown from the Question class and handle them.

Pseudo-code for SmartQuestion's askQuestion method:
    askQuestion
      validAnswer=false;
      while (!validAnswer) {
          try
              ans = q.askQuestion();
              validAnswer = true;
          catch NotACharException
             Check if the first character of the user's answer was valid, if so, use it as the answer and continue
              For example, if the valid answers were "y" and "n" and the user types "no", then use "n" as the answer.
              If the user types "maybe", tell the user what the problem was
          catch NotAValidCharException
              Tell the user what the problem was
       } // End While statement  
    return ans;
           
Helper classes provided to you (yes, you MUST use them):

MessageWindow - This class will open a window with a message in it, and allow the user to submit an answer. It will not automatically close itself so you must call the close method everytime (if you get an exception or you don't, you must call the close method --- hint: use a finally block).

String str = MessageWindow.getResponse(message); // Show the window with a specific message in it.
MessageWindow.close(); // Close the window

TestMain.java
- this class implements a test for your program by asking a question using both Question and SmartQuestion. Make sure this class can run without modifications.

What to turn in
You should turn in a Jar file including all your source code and compiled Java program. Including compiled and source code for: Question, SmartQuestion, NotACharException, NotAValidCharException.