Chapter 10 Object-Oriented Thinking
Chapter 10 Object-Oriented Thinking
1. No. The Loan class has the getLoanDate() method that returns loanDate. loanDate is an object of the
Date class. Since Date is mutable, the contents of loanDate can be changed. So, the Loan class is not
immutable.
2. Yes.
3. The common relationships among classes are association, aggregation, composition, and inheritance.
4. Association is a general binary relationship that describes an activity between two classes.
Aggregation is a special form of association that represents an ownership relationship between two
objects. Aggregation models has-a relationships. An object can be owned by several other aggregating
objects. If an object is exclusively owned by an aggregating object, the relationship between the object
and its aggregating object is referred to as a composition.
5. Aggregation: empty diamond on the aggregating class. Composition: Solid diamond on the
aggregating class.
6. Since aggregation and composition relationships are represented using classes in the same way, we
will not differentiate them and call both compositions for simplicity.
7. Omitted
8.
Answer: Correct
Answer: Correct
Integer i = Integer.valueOf("23");
Answer: Correct
Integer i = Integer.parseInt("23",8);
Answer: 19
Double d = new Double();
Double d = Double.valueOf("23.45");
Answer: Correct
int i = (Integer.valueOf("23")).intValue();
Answer: Correct
double d = (Double.valueOf("23.4")).doubleValue();
Answer: Correct
int i = (Double.valueOf("23.4")).intValue();
Answer: Correct
String s = (Double.valueOf("23.4")).toString();
Answer: Correct
9. You can simply use number + "" to convert an integer to a string. Alternatively use new
Integer(int).toString() to convert an integer to a string. To convert a numeric string into an integer, use
Integer.parseInt(s). Use new Double(double).toString() to convert a double to a string. To convert a
numeric string into a double, use Double.parseDouble(s).
10.
-1
11.
10
10
16
11
11
17
12.
Integer x = 3; // Correct
13.
-1
x is 3
y is 7
z is 10
15.
s1 == s2 => true
s1 == s3 => false
s1 == s4 => true
16.
String s = "Welcome to Java";
is better, because this type of string is stored as an interned string. The interned strings of the
same value share the same object.
17 The output is
Welcome to Java
Hint: No method in the String class can change the content of the string. String is an
immutable class.
18.
Replace all occurrence of character e with E in s1 and assign the new string to s2.
Split "Welcome to Java and HTML" into an array tokens using delimited by a space.
19. No.
20. 0.
21. Use the overloaded static valueOf method in the String class.
22. The text is declared in Line 2 as a data field, but redeclared in Line 5 as a local variable. The local
variable is assigned with the string passed to the constructor, but the data field is still null. In Line
10, test.text is null, which causes NullPointerException when invoking the toLowerCase() method.
24.
false
true
A,B;C
A#B#C
ABC
25. 3
26. The StringBuilder class, introduced in JDK 1.5, is similar to StringBuffer except that the update
methods in StringBuffer are synchronized.
27. Use the StringBuilder’s constructor to create a string buffer for a string, and use the toString method
in StringBuilder class to return a string from a StringBuilder.
sb.reverse();
s = sb.toString();
sb.delete(4, 10);
s = sb.toString();
30. Both string and string buffer use arrays to hold characters. The array in a string is fixed once a string
is created. The array in a string buffer may change if the buffer capacity is changed. To accommodate
the change, a new array is created.
31.
(2) JavaHTML
(4) JHTMLava
(5) v
(6) 4
(7) Jav
(8) Ja
(9) avaJ
(10) JComputera
(11) av
(12) va
32.
The output is
Java
NOTE:
Inside the method, the statement s = s + " and HTML" creates a new String object s,
which is different from the original String object passed to the change(s, buffer) method.
The original String object has not been changed. Therefore, the printout from the original
string is Java.
Inside the method, the content of the StringBuilder object is changed to Java and HTML.
Therefore, the printout from buffer is Java and HTML.