![]() |
![]() |
|
02115 Java Programming | ||
Autumn 2011 |
if ... else
constructs placed in the
method bodies. But in the branches to be executed when
an error has occured the immediate error handling action
should not be to inform
about the error by applying some
System.out.println
's and then return
from the method.
That way of reporting errors is not well-suited when having
a GUI. It's better to split the error detection and the
error handling and postpone the error handling
to take place at a position close to the user interface.
Then depending on the kind of user interface applied in the
actual run of the program one can just choose to present the
error message by using either a
System.out.println
or a kind of message dialogue.
In order to achieve this generality allowing the program
to use a command line interface or a GUI without needing
different versions of some classes exceptions could be applied.
In the method body where the error is detected the printing of
text should be changed into throwing a checked exception. By adding a
throws
clause to the method declaration the
method may return with an execption object
informing about the error
instead of doing a normal return. It requires the method call
to be placed in the try
branch of a
try ... catch
construct and the error handling
to be done by a catch
.
That first catch
can either act on the exception
or throw it further up to another try ... catch
construct until the error situation is finally handled
on a level close to the user interface
The link on page 375 of BK referring to the Java
Tutorial Creating a GUI with JFC/Swing has been changed into
http://download.oracle.com/javase/tutorial/uiswing
Just click on this new link here:
Creating a GUI With JFC/Swing
Instead experiment with the types of components you have in mind to use in the Mandatory Assignment based primarily on what is presented in the sections 11.8 and 11.9 of our BK textbook.
You can be introduced to and guided through some useful components by doing
Exercises 11.49 - 11.63
For objects which are elements of a collection class you have to
declare a boolean equals(Object o)
method in the corresponding
class. For a HashSet
of elements a hashCode
method should be supplied. And for a HashMap
the
class from which the keys originate should also declare a
hashCode
method. To use TreeSet<T>
and
TreeMap<T, ...>
class T
should declare
a int compareTo(T o)
method. Refer to our two textbooks
PS, primarily section 22.8, and BK section 9.8 (Object
equality: equals
and hashCode
)
Newest edition: 17. October
Previous editions:
- 8. April (just the heading and the footing)