CSIS 3103 - Lab 2
Sept 22, 2010
You can work in pairs for this lab. Put your names in the @author section of the comment block at the top of the file.
Objectives:
This lab project involves modifying the implementation of the Time class from Lab 1 by adding more features, including exception handling capabilities.
The class invariant
A class invariant is an assertion that must always be true about objects of a class. For the Time class, this will be a statement of what must always be true about the hours, minutes, and seconds. Write a class invariant as part of the comment block at the top of the Time class.
Constructors should guarantee that the class invariant is true for all objects that are created. Add statements to the constructor that will prevent making an invalid Time object from user supplied initial values. The constructor should throw an exception if an invalid object would occur. Each kind of error should throw an exception with a message that describes which field would be invalid - hour, minute or second.
Put the answers to the following questions in the comment block at the top of the Time class.
Q 1: Is it necessary to check that the hour, minute, and second values satisfy the class invariant for the constructor that gets the time from the computer's built in clock? Explain your answer.
Q 2: All other methods that modify instance variables must also guarantee that the class invariant remains true. Which methods does this include?
Test it
Add test methods to the TimeTest class to verify that the modified constructor works correctly. There should be sufficient tests to verify a valid Time object and to determine that all ways of generating an invalid Time object result in an exception being thrown. Use try-catch in these test methods and use the fail method to detect cases where an exception should have been thrown but wasn't.
Implement an interface
Modify the declaration of the Time class to include the following:
public class Time implements Comparable<Time> {
Eclipse will show an error when the implements clause is added. This is because the methods defined in the Comparable interface need to be implemented. Use the "hint" in Eclipse to add the unimplemented methods (there is only one). Check the Javadoc for the compareTo method to see what it needs to return. Complete the implementation of this method.
Testing compareTo
Add sufficient test methods to the TimeTest class to thoroughly test that the compareTo method works properly.
Use it
Write a short class with a main method that prompts and inputs hour, minute, and second values from the user to make a Time object and compare it to the current time. (Input will use a Scanner object or JOptionPane objects for input.) The program should print an appropriate message to indicate which of the times is earlier. It should use exception handling to report any input errors and prompt the user to try entering the data again. The program should continue accepting user input until a non-numeric value is entered. It then should display a "goodbye" message and stop.
Your very own exception class
Create a class called InvalidTimeException that extends RuntimeException. All that needs to be included in this class is two constructors - a default constructor and one that takes a string as a parameter. Replace all occurrences of IllegalArgumentException with InvalidTimeException in the other classes. Run the test driver and the application to make sure they still work.
Hand In:
Use the File -> Export to create a zip file that contains just the source code (.java) files. Submit this using the link proved in the Assignments page.