Errata - Introductory Java
These are the errors I am aware of in Introductory Java. If you
have come across any others, please let me know
Page 40 - Chapter 4, 'Lift' example
Is this a 'Lift' or a 'LiftMain'?
The class in this example is called 'LiftMain':
public class LiftMain
{ ...
etc.
However, the 'main' method of this class creates an instance of 'Lift':
{
Lift a_lift = new Lift();
etc.
The confusion here stems from the fact that there are two versions of the 'Lift' class, one with a 'main' method and one without. Since they could not both be called 'Lift', I had to rename the first one 'LiftMain', but failed to change the 'main' method to refer to the correct class. Therefore the line above should read:
{
LiftMain a_lift = new LiftMain();
etc.
Page 140 - Chapter 7, exercise 6
Adding a 'removeAppointment' method to the Diary class
(with thanks to
Charles Hetherington)
A solution like this might be expected to work:
public void removeAppointment(int day,
int month, int year)
{
Calendar the_key =
Calendar.getInstance();
month--;
the_key.set(year, month, day);
if(slots.containsKey(the_key))
etc.
In this section of code the last line probably evaluates false even if you
put in the correct dates (calling the showAppointments method would show that
the dates are there in the hashtable), so what's going on? The problem is that
Calendars and Dates store time information down to the millisecond. This means
that test data can sometimes return true, but only if the machine is running
fast enough to create both the key and the removal data almost simultaneously.
The answer is to apply the 'clear' method to the Calendar object before
populating it with the selected date information. 'clear' resets all the fields
to zero (or other suitable default values). In your code you might do something
like...
{
Calendar the_key =
Calendar.getInstance();
// clear the time fields
the_key.clear();
month--;
the_key.set(year,
month, day);
if(slots.containsKey(the_key))
etc.
Of course you must also be sure to clear the key objects before you set their
dates and put them in the table.