Chapter 3

Exercise 3.1

The class diagram contains only 2 elements: LabClass and Student
The object diagram contains 4 elements. 1 LabClass object and 3 Student objects

Exercise 3.2

A class diagram changes when you modify the source code. That can be by changing the relations between classes or creating/deleting classes.

Exercise 3.3

An object diagram changes when the program is running. It can be changed by creating new objects, or calling methods.

Exercise 3.4

private Instructor tutor;

Exercise 3.6

Nothing happens.
No.
It should print out an error message.

Exercise 3.7

It would not allow a value of zero.

Exercise 3.8

The test will be true if one of the conditions is true
It will always be true if the limit is larger than or equal to 0.

Exercise 3.9

false
true
false
false
true

Exercise 3.10

a==b

Exercise 3.11

(a || b) && (a != b)

Exercise 3.12

!(!a || !b)

Exercise 3.13

No.

The method assumes that the value will only contain to digits.

Exercise 3.14

No.

Exercise 3.15

The exact definition can be found in The Java Language Specification Second Edition in section 15.17.3
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#239829

Exercise 3.16

2

Exercise 3.17

-4, -3, -2, -1, 0, 1, 2, 3, 4

Exercise 3.18

]-m,m[ (the range from -(m-1) to (m-1) )

Exercise 3.19

As long as value is smaller than limit, it gets incremented by 1 (the modulo can be ignored)
When value reaches the limit, the modulo operation will result in value being set to 0.
So, the increment method increments value by one, until the limit is reached, at which point it will start over at 0.

Exercise 3.20

public void increment()
{
    value = value + 1;
    if(value >= limit) 
        value = 0;        
}

Both ways of incrementing are equally good.

Exercise 3.22

The time is initialised to 00.00.
The constructor creates two new NumberDisplay which are vitalized to 0 (in the constructor for NumberDisplay)

Exercise 3.23

It needs 60 clicks
Using the method setTime() on the object.

Exercise 3.24

public Editor(String fileName, int number)

Exercise 3.25

Rectangle window = new Rectangle(3,6);

Exercise 3.26

It initialises the time to the values passed to the method.
It uses the method setTime to set the time to the initial value.

Exercise 3.27

Both constructors creates two new NumberDisplays.
The first constructor calls updateDisplay and the second calls setTime(hour, minute).
In the second constructor there is no call to updateDisplay because this will be done in the method setTime.

Exercise 3.28

p1.print("file1.txt", true);

p1.print("file2.txt", false);

int status1 = p1.getStatus(12);

int status2 = p1.getStatus(34);

Exercise 3.29, 3.30

1) change the updateDisplay in ClockDisplay as this:
/**
 * Update the internal string that represents the display.
 */
private void updateDisplay()
{
	int hour = hours.getValue(); 
	String suffix = "am";       
		  
	if(hour >= 12) {
		hour = hour - 12;
		suffix = "pm";
	}
 if(hour == 0) {
		hour = 12;
	} 
	displayString = hour + "." + minutes.getDisplayValue() + suffix;
}
2)
public ClockDisplay()
{
	hours = new NumberDisplay(12); //changed
	minutes = new NumberDisplay(60);
	updateDisplay();
}

public ClockDisplay(int hour, int minute)
{
	hours = new NumberDisplay(12); //changed
	minutes = new NumberDisplay(60);
	setTime(hour, minute);
}


private void updateDisplay()
{
	int hour = hours.getValue();
	if(hour == 0)
		hour = 12;
		
	displayString = hour + "." + minutes.getDisplayValue();       
}

Exercise 3.36

Since the debugger shows that:
Mailitem item = <object reference>
This indicates that item is not null, and the next line that will be marked is item.print()

Exercise 3.37

This time the item is null. The if-statement will then execute the line: System.out.println("No new mail.");

Exercise 3.38

After pressing Step Into and then pressing Step it prints:
From: Sophie
After the next Step it prints:
To: Juan
And after another Step:
Message: Hi Juan!

Step Into goes into the method print(). From there on, each line of print is executed by a pressing Step.
This results in the lines being printed one at a time instead of just printing all 3 lines as before.