SlideShare ist ein Scribd-Unternehmen logo
1 von 4
Chapter 8 Objects and Classes
1. See the section "Declaring and Creating Objects."
2. Constructors are special kinds of methods that are
called when creating an object using the new operator.
Constructors do not have a return type—not even void.
3. An array is an object. The default value for the
elements of an array is 0 for numeric, false for
boolean, ‘u0000’ for char, null for object element
type.
4. (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
7. Use the Date’s no-arg constructor to create a Date for
the current time. Use the Date’s toString() method to
display a string representation for the Date.
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. You cannot invoke an instance method or reference an
instance variable from a static method. You can invoke a
static method or reference a static variable from an
instance method? c is an instance variable, which cannot
be accessed from the static context in 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).
14.Two benefits: (1) for protecting data and (2) for easy
to maintain the class.
15. Yes. Though radius is private, myCircle.radius is used
inside the Circle class. Thus, it is fine.
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.)

Weitere ähnliche Inhalte

Was ist angesagt?

香港六合彩
香港六合彩香港六合彩
香港六合彩
cctv
 
Quiz2 cs141-1-17
Quiz2 cs141-1-17Quiz2 cs141-1-17
Quiz2 cs141-1-17
Fahadaio
 

Was ist angesagt? (20)

Class notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methodsClass notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methods
 
3 searching algorithms in Java
3 searching algorithms in Java3 searching algorithms in Java
3 searching algorithms in Java
 
L11 array list
L11 array listL11 array list
L11 array list
 
CRMS Calculus 2010 February 8, 2010_B
CRMS Calculus 2010 February 8, 2010_BCRMS Calculus 2010 February 8, 2010_B
CRMS Calculus 2010 February 8, 2010_B
 
Functions
FunctionsFunctions
Functions
 
Kelompok 8 Pbw
Kelompok 8 PbwKelompok 8 Pbw
Kelompok 8 Pbw
 
Kelompok 8 Pbw
Kelompok 8 PbwKelompok 8 Pbw
Kelompok 8 Pbw
 
Visual basic bt0082
Visual basic  bt0082Visual basic  bt0082
Visual basic bt0082
 
Class notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methodsClass notes(week 3) on class objects and methods
Class notes(week 3) on class objects and methods
 
A04
A04A04
A04
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
searching
searchingsearching
searching
 
Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point Selection sort algorithm presentation, selection sort example using power point
Selection sort algorithm presentation, selection sort example using power point
 
香港六合彩
香港六合彩香港六合彩
香港六合彩
 
Java execise
Java execiseJava execise
Java execise
 
Avoid creating unncessary objects
Avoid creating unncessary objectsAvoid creating unncessary objects
Avoid creating unncessary objects
 
(6) collections algorithms
(6) collections algorithms(6) collections algorithms
(6) collections algorithms
 
Effective Java - Chapter 3: Methods Common to All Objects
Effective Java - Chapter 3: Methods Common to All ObjectsEffective Java - Chapter 3: Methods Common to All Objects
Effective Java - Chapter 3: Methods Common to All Objects
 
Chapter 5:Understanding Variable Scope and Class Construction
Chapter 5:Understanding Variable Scope and Class ConstructionChapter 5:Understanding Variable Scope and Class Construction
Chapter 5:Understanding Variable Scope and Class Construction
 
Quiz2 cs141-1-17
Quiz2 cs141-1-17Quiz2 cs141-1-17
Quiz2 cs141-1-17
 

Andere mochten auch

Project final
Project finalProject final
Project final
IIUM
 
Csc1100 elements of programming (revised july 2014) 120lh-2-student
Csc1100 elements of  programming (revised july 2014) 120lh-2-studentCsc1100 elements of  programming (revised july 2014) 120lh-2-student
Csc1100 elements of programming (revised july 2014) 120lh-2-student
IIUM
 
Csc1401 q1 - sect3 - answers scheme
Csc1401   q1 - sect3 - answers schemeCsc1401   q1 - sect3 - answers scheme
Csc1401 q1 - sect3 - answers scheme
IIUM
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
IIUM
 
Csc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declarationCsc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declaration
IIUM
 
Introduction fundamentals sets and sequences
Introduction  fundamentals sets and sequencesIntroduction  fundamentals sets and sequences
Introduction fundamentals sets and sequences
IIUM
 

Andere mochten auch (18)

Peraturan am mengisi dokumen perjanjian
Peraturan am mengisi dokumen perjanjianPeraturan am mengisi dokumen perjanjian
Peraturan am mengisi dokumen perjanjian
 
Project part 2 instructions
Project part 2   instructionsProject part 2   instructions
Project part 2 instructions
 
Lect 18
Lect 18Lect 18
Lect 18
 
Ibm piquant summary
Ibm piquant summaryIbm piquant summary
Ibm piquant summary
 
Principle and Practice of Management MGT Ippt chap006
Principle and Practice of Management MGT Ippt chap006Principle and Practice of Management MGT Ippt chap006
Principle and Practice of Management MGT Ippt chap006
 
Tutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoTutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seo
 
Principle and Practice of Management MGT Ippt chap010
Principle and Practice of Management MGT Ippt chap010Principle and Practice of Management MGT Ippt chap010
Principle and Practice of Management MGT Ippt chap010
 
Project final
Project finalProject final
Project final
 
Csc1100 elements of programming (revised july 2014) 120lh-2-student
Csc1100 elements of  programming (revised july 2014) 120lh-2-studentCsc1100 elements of  programming (revised july 2014) 120lh-2-student
Csc1100 elements of programming (revised july 2014) 120lh-2-student
 
Csc1401 q1 - sect3 - answers scheme
Csc1401   q1 - sect3 - answers schemeCsc1401   q1 - sect3 - answers scheme
Csc1401 q1 - sect3 - answers scheme
 
Assignment 01
Assignment 01Assignment 01
Assignment 01
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
 
Csc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declarationCsc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declaration
 
Redo midterm
Redo midtermRedo midterm
Redo midterm
 
Principle and Practice of Management MGT Ippt chap001
Principle and Practice of Management MGT Ippt chap001Principle and Practice of Management MGT Ippt chap001
Principle and Practice of Management MGT Ippt chap001
 
Chapter 6 1
Chapter 6 1Chapter 6 1
Chapter 6 1
 
Heaps
HeapsHeaps
Heaps
 
Introduction fundamentals sets and sequences
Introduction  fundamentals sets and sequencesIntroduction  fundamentals sets and sequences
Introduction fundamentals sets and sequences
 

Ähnlich wie 08review (1)

chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
It Academy
 

Ähnlich wie 08review (1) (20)

Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
14review(abstract classandinterfaces)
14review(abstract classandinterfaces)14review(abstract classandinterfaces)
14review(abstract classandinterfaces)
 
Constructors
ConstructorsConstructors
Constructors
 
JAVA CONCEPTS
JAVA CONCEPTS JAVA CONCEPTS
JAVA CONCEPTS
 
Ecet 370 Education Organization -- snaptutorial.com
Ecet 370   Education Organization -- snaptutorial.comEcet 370   Education Organization -- snaptutorial.com
Ecet 370 Education Organization -- snaptutorial.com
 
ECET 370 Exceptional Education - snaptutorial.com
ECET 370 Exceptional Education - snaptutorial.com ECET 370 Exceptional Education - snaptutorial.com
ECET 370 Exceptional Education - snaptutorial.com
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Class and Object.pptx
Class and Object.pptxClass and Object.pptx
Class and Object.pptx
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Collections
CollectionsCollections
Collections
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptx
 
Java String
Java String Java String
Java String
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
15review(abstract classandinterfaces)
15review(abstract classandinterfaces)15review(abstract classandinterfaces)
15review(abstract classandinterfaces)
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxing
 
basic concepts
basic conceptsbasic concepts
basic concepts
 
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
 
Imp_Points_Scala
Imp_Points_ScalaImp_Points_Scala
Imp_Points_Scala
 

Mehr von IIUM (20)

How to use_000webhost
How to use_000webhostHow to use_000webhost
How to use_000webhost
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Kreydle internship-multimedia
Kreydle internship-multimediaKreydle internship-multimedia
Kreydle internship-multimedia
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
 
Chap2 practice key
Chap2 practice keyChap2 practice key
Chap2 practice key
 
Group p1
Group p1Group p1
Group p1
 
Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lforms
 
Exercise on algo analysis answer
Exercise on algo analysis   answerExercise on algo analysis   answer
Exercise on algo analysis answer
 
Report format
Report formatReport format
Report format
 
Edpuzzle guidelines
Edpuzzle guidelinesEdpuzzle guidelines
Edpuzzle guidelines
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Group assignment 1 s21516
Group assignment 1 s21516Group assignment 1 s21516
Group assignment 1 s21516
 
Avl tree-rotations
Avl tree-rotationsAvl tree-rotations
Avl tree-rotations
 
Week12 graph
Week12   graph Week12   graph
Week12 graph
 
Vpn
VpnVpn
Vpn
 
Principle and Practice of Management MGT Ippt chap002
Principle and Practice of Management MGT Ippt chap002Principle and Practice of Management MGT Ippt chap002
Principle and Practice of Management MGT Ippt chap002
 
Principle and Practice of Management MGT Ippt chap003
Principle and Practice of Management MGT Ippt chap003Principle and Practice of Management MGT Ippt chap003
Principle and Practice of Management MGT Ippt chap003
 

Kürzlich hochgeladen

VIP Call Girls Bhavnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Bhavnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Bhavnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Bhavnagar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Russian🍌Dazzling Hottie Get☎️ 9053900678 ☎️call girl In Chandigarh By Chandig...
Russian🍌Dazzling Hottie Get☎️ 9053900678 ☎️call girl In Chandigarh By Chandig...Russian🍌Dazzling Hottie Get☎️ 9053900678 ☎️call girl In Chandigarh By Chandig...
Russian🍌Dazzling Hottie Get☎️ 9053900678 ☎️call girl In Chandigarh By Chandig...
Chandigarh Call girls 9053900678 Call girls in Chandigarh
 
VIP Call Girls Agra 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Agra 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Agra 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Agra 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Kürzlich hochgeladen (20)

WORLD DEVELOPMENT REPORT 2024 - Economic Growth in Middle-Income Countries.
WORLD DEVELOPMENT REPORT 2024 - Economic Growth in Middle-Income Countries.WORLD DEVELOPMENT REPORT 2024 - Economic Growth in Middle-Income Countries.
WORLD DEVELOPMENT REPORT 2024 - Economic Growth in Middle-Income Countries.
 
The NAP process & South-South peer learning
The NAP process & South-South peer learningThe NAP process & South-South peer learning
The NAP process & South-South peer learning
 
Pimpri Chinchwad ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi R...
Pimpri Chinchwad ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi R...Pimpri Chinchwad ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi R...
Pimpri Chinchwad ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi R...
 
TEST BANK For Essentials of Negotiation, 7th Edition by Roy Lewicki, Bruce Ba...
TEST BANK For Essentials of Negotiation, 7th Edition by Roy Lewicki, Bruce Ba...TEST BANK For Essentials of Negotiation, 7th Edition by Roy Lewicki, Bruce Ba...
TEST BANK For Essentials of Negotiation, 7th Edition by Roy Lewicki, Bruce Ba...
 
celebrity 💋 Agra Escorts Just Dail 8250092165 service available anytime 24 hour
celebrity 💋 Agra Escorts Just Dail 8250092165 service available anytime 24 hourcelebrity 💋 Agra Escorts Just Dail 8250092165 service available anytime 24 hour
celebrity 💋 Agra Escorts Just Dail 8250092165 service available anytime 24 hour
 
VIP Call Girls Bhavnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Bhavnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Bhavnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Bhavnagar 7001035870 Whatsapp Number, 24/07 Booking
 
Akurdi ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
Akurdi ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...Akurdi ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...
Akurdi ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
 
Nanded City ? Russian Call Girls Pune - 450+ Call Girl Cash Payment 800573673...
Nanded City ? Russian Call Girls Pune - 450+ Call Girl Cash Payment 800573673...Nanded City ? Russian Call Girls Pune - 450+ Call Girl Cash Payment 800573673...
Nanded City ? Russian Call Girls Pune - 450+ Call Girl Cash Payment 800573673...
 
Election 2024 Presiding Duty Keypoints_01.pdf
Election 2024 Presiding Duty Keypoints_01.pdfElection 2024 Presiding Duty Keypoints_01.pdf
Election 2024 Presiding Duty Keypoints_01.pdf
 
Postal Ballots-For home voting step by step process 2024.pptx
Postal Ballots-For home voting step by step process 2024.pptxPostal Ballots-For home voting step by step process 2024.pptx
Postal Ballots-For home voting step by step process 2024.pptx
 
2024: The FAR, Federal Acquisition Regulations, Part 31
2024: The FAR, Federal Acquisition Regulations, Part 312024: The FAR, Federal Acquisition Regulations, Part 31
2024: The FAR, Federal Acquisition Regulations, Part 31
 
Chakan ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
Chakan ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...Chakan ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...
Chakan ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
 
World Press Freedom Day 2024; May 3rd - Poster
World Press Freedom Day 2024; May 3rd - PosterWorld Press Freedom Day 2024; May 3rd - Poster
World Press Freedom Day 2024; May 3rd - Poster
 
Russian🍌Dazzling Hottie Get☎️ 9053900678 ☎️call girl In Chandigarh By Chandig...
Russian🍌Dazzling Hottie Get☎️ 9053900678 ☎️call girl In Chandigarh By Chandig...Russian🍌Dazzling Hottie Get☎️ 9053900678 ☎️call girl In Chandigarh By Chandig...
Russian🍌Dazzling Hottie Get☎️ 9053900678 ☎️call girl In Chandigarh By Chandig...
 
VIP Call Girls Agra 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Agra 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Agra 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Agra 7001035870 Whatsapp Number, 24/07 Booking
 
Top Rated Pune Call Girls Hadapsar ⟟ 6297143586 ⟟ Call Me For Genuine Sex Se...
Top Rated  Pune Call Girls Hadapsar ⟟ 6297143586 ⟟ Call Me For Genuine Sex Se...Top Rated  Pune Call Girls Hadapsar ⟟ 6297143586 ⟟ Call Me For Genuine Sex Se...
Top Rated Pune Call Girls Hadapsar ⟟ 6297143586 ⟟ Call Me For Genuine Sex Se...
 
A Press for the Planet: Journalism in the face of the Environmental Crisis
A Press for the Planet: Journalism in the face of the Environmental CrisisA Press for the Planet: Journalism in the face of the Environmental Crisis
A Press for the Planet: Journalism in the face of the Environmental Crisis
 
Junnar ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
Junnar ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...Junnar ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For S...
Junnar ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For S...
 
Night 7k to 12k Call Girls Service In Navi Mumbai 👉 BOOK NOW 9833363713 👈 ♀️...
Night 7k to 12k  Call Girls Service In Navi Mumbai 👉 BOOK NOW 9833363713 👈 ♀️...Night 7k to 12k  Call Girls Service In Navi Mumbai 👉 BOOK NOW 9833363713 👈 ♀️...
Night 7k to 12k Call Girls Service In Navi Mumbai 👉 BOOK NOW 9833363713 👈 ♀️...
 
PPT BIJNOR COUNTING Counting of Votes on ETPBs (FOR SERVICE ELECTORS
PPT BIJNOR COUNTING Counting of Votes on ETPBs (FOR SERVICE ELECTORSPPT BIJNOR COUNTING Counting of Votes on ETPBs (FOR SERVICE ELECTORS
PPT BIJNOR COUNTING Counting of Votes on ETPBs (FOR SERVICE ELECTORS
 

08review (1)

  • 1. Chapter 8 Objects and Classes 1. See the section "Declaring and Creating Objects." 2. Constructors are special kinds of methods that are called when creating an object using the new operator. Constructors do not have a return type—not even void. 3. An array is an object. The default value for the elements of an array is 0 for numeric, false for boolean, ‘u0000’ for char, null for object element type. 4. (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 () {
  • 2. } } 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 7. Use the Date’s no-arg constructor to create a Date for the current time. Use the Date’s toString() method to display a string representation for the Date. 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
  • 3. 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. You cannot invoke an instance method or reference an instance variable from a static method. You can invoke a static method or reference a static variable from an instance method? c is an instance variable, which cannot be accessed from the static context in 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). 14.Two benefits: (1) for protecting data and (2) for easy to maintain the class. 15. Yes. Though radius is private, myCircle.radius is used inside the Circle class. Thus, it is fine. 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
  • 4. 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.)