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.3

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

Exercise 3.4

It would not allow a value of zero.

Exercise 3.5

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.8

The exact deifinition 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.9

2

Exercise 3.10

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

Exercise 3.11

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

Exercise 3.12

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.13

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

Exercise 3.15

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

Exercise 3.16

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

Exercise 3.17

It initializes the time to the values passed to the method.
It uses the method setTime to set the time to the intial value.

Exercise 3.18

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.19, 3.20

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();       
}