Chapter 8 Objects and Classes
1. See the section "Declaring and Creating Objects."
2. 構造方法是在創建一個對象使用new操作符時調用的。 構造方法沒有返回類型,甚至連void也沒有。
- 數組是一個對象。 The default value for the elements of an array is 0 for numeric, false for boolean, ‘\u0000’ for char, null for object element type.
- (a) There is such constructor ShowErrors(int) in the ShowErrors class.
The ShowErrors class in the book has a default constructor. It is actually same as
public class ShowErrors {
public static void main(String[] args) {
ShowErrors t = new ShowErrors(5);
}
public ShowErrors () {
}
}
On Line 3, new ShowErrors(5) attempts to create an instance using a constructor ShowErrors(int), but the ShowErrors class does not have such a constructor. That is an error.
(b) x() is not a method in the ShowErrors class.
The ShowErrors class in the book has a default constructor. It is actually same as
public class ShowErrors {
public static void main(String[] args) {
ShowErrors t = new ShowErrors();
t.x();
}
public ShowErrors () {
}
}
On Line 4, t.x() is invoked, but the ShowErrors class does not have the method named x(). That is an error.
(c) The program compiles fine, but it has a runtime error because variable c is null when the println statement is executed.
(d) new C(5.0) does not match any constructors in class C. The program has a compilation error because class C does not have a constructor with a double argument.
5. The program does not compile because new A() is used in class Test, but class A does not have a default constructor. See the second NOTE in the Section, “Constructors.”
6. false
java.util.Date data = new java.util.Date();
System.out.println("The elspsed time since Jan 1, 1970 is "
+ data.getTime() + " milliseconds");
- System.out.println(data.toString());
8. Use the JFrame’s no-arg constructor to create JFrame. Use the setTitle(String) method a set a title and use the setVisible(true) method to display the frame.
9. Date is in java.util. JFrame and JOptionPane are in javax.swing. System and Math are in java.lang.
10. System.out.println(f.i);
Answer: Correct
System.out.println(f.s);
Answer: Correct
f.imethod();
Answer: Correct
f.smethod();
Answer: Correct
System.out.println(Foo.i);
Answer: Incorrect
System.out.println(Foo.s);
Answer: Correct
Foo.imethod();
Answer: Incorrect
Foo.smethod();
Answer: Correct
11. Add static in the main method and in the factorial method because these two methods don’t need reference any instance objects or invoke any instance methods in the Test class.
12. 不能從靜態方法中調用實例方法或引用一個實例變量,能從實例方法中調用靜態方法或引用一個靜態變量。 c 是一個實例變量,不能在靜態方法method2中被訪問。
13. Accessor method is for retrieving private data value and mutator method is for changing private data value. The naming convention for accessor method is getDataFieldName() for non-boolean values and isDataFieldName() for boolean values. The naming convention for mutator method is setDataFieldName(value).
- 保護數據域和易於修改class.
15. radius 是 private, myCircle.radius is used inside the Circle class. 因此這個程序是對的。
16. Java uses “pass by value” to pass parameters to a method. When passing a variable of a primitive type to a method, the variable remains unchanged after the method finishes. However, when passing a variable of a reference type to a method, any changes to the object referenced by the variable inside the method are permanent changes to the object referenced by the variable outside of the method. Both the actual parameter and the formal parameter variables reference to the same object.
The output of the program is as follows:
count 101
times 0
17.
Remark: The reference value of circle1 is passed to x and the reference value of circle2 is passed to y. The contents of the objects are not swapped in the swap1 method. circle1 and circle2 are not swapped. To actually swap the contents of these objects, replace the following three lines
Circle temp = x;
x =y;
y =temp;
by
double temp = x.radius;
x.radius = y.radius;
y.radius = temp;
as in swap2.
18.a. a[0] = 1 a[1] = 2
b. a[0] = 2 a[1] = 1
c. e1 = 2 e2 = 1
d. t1’s i = 2 t1’s j = 1
t2’s i = 2 t2’s j = 1
19.
(a) null
(b) 1234567
(c) 7654321
(d) 1234567
20. (Line 4 prints null since dates[0] is null. Line 5 causes a NullPointerException since it invokes toString() method from the null reference.)