Objects First With Java Chapter 3
Objects First With Java Chapter 3
The class diagram contains only 2 elements: LabClass and Student. The LabClass
class is linked to the Student class.
The object diagram contains 4 elements. 1 LabClass object and 3 Student objects. The
LabClass object contains links to the three 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
Exercise 3.4
Exercise 3.6
Nothing happens.
No.
It should print out an error message.
Exercise 3.7
Exercise 3.8
Exercise 3.9
false
true
false
false
true
Exercise 3.10
The long version of this would be:
But since both must have identical values, the simplest form is:
a==b
Exercise 3.11
Or:
(a || b) && (a != b)
Or even:
a != b
Exercise 3.12
!(!a || !b)
Exercise 3.13
No.
The method assumes that the value will only contain two digits.
Exercise 3.14
No.
Exercise 3.15
The exact definition can be found in The Java Language Specification Third Edition
in section 15.17.3
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.17.3
The correct answer may not be intuitive when negative values are involved.
The key is that the modulus operator works so that
(a / b) * b + (a % b) == a
rearranging we get:
a % b == a – (a / b) * b
Exercise 3.16
Exercise 3.18
Exercise 3.19
Exercise 3.20
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.21
Many students get this one wrong on the boundaries. A common wrong solution,
which results in a value equal to the limit is:
A correct, but difficult to visually verify version, which is often the result of fixing the
above is:
Both ways of incrementing are equally good. Once the modulus operator is
understood, its use is obviously more succinct.
Exercise 3.23
Exercise 3.24
It needs 60 clicks
Using the method setTime() on the object.
Exercise 3.26
Exercise 3.27
Exercise 3.28
Exercise 3.29
Exercise 3.30
p1.print("file1.txt", true);
p1.print("file2.txt", false);
int status;
status = p1.getStatus(12);
status = p1.getStatus(34);
/**
* 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();
}
Exercise 3.38
This time the item is null. The if-statement will then execute the line:
System.out.println("No new mail.");
Exercise 3.40
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.
Exercise 3.45
A subject field must be added to the MailItem class and set via the constructor. An
accessor is also added and the print() method should include it.
See 03-45-mail-system.
Exercise 3.46