Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Gaddis: Starting Out with Java: From Control Structures through Data Structures, 2/e © 2012 Pearson Education Answers to Review Questions Chapter 9 Multiple Choice and True/False 1. c 2. c 3. a 4. c 5. b 6. c 7. d 8. b 9. c 10. a 11. b 12. False 13. False 14. True 15. False Find the Error 1. 2. The static method setValues cannot refer to the non-static fields x and y. You cannot use the fully-qualified names of enum constants in the case expressions. Algorithm Workbench 1. a) public String toString() { String str; str = "Radius: " + radius + " Area: " + getArea(); return str; } b) public boolean equals(Circle c) { boolean status; if (c.getRadius() == radius) status = true; 1 Gaddis: Starting Out with Java: From Control Structures through Data Structures, 2/e © 2012 Pearson Education 2 else status = false; return status; } c) public boolean greaterThan(Circle c) { boolean status; if (c.getArea() > getArea()) status = true; else status = false; return status; } 2. a) b) c) d) e) 3. 3 3 1 0 Thing.putThing(5); enum Pet { DOG, CAT, BIRD, HAMSTER } Short Answer 1. 2. 3. 4. 5. 6. 7. Access a non-static member. They can be called directly from the class, as needed. They can be used to create utility classes that perform operations on data, but have no need to collect and store data. When a variable is passed as an argument, a copy of the variable's contents is passed. The receiving method does not have access to the variable itself. When an object is passed as an argument, a reference to the object (which is the object's address) is passed. This allows the receiving method to have access to the object. The default equals method returns true if the memory addresses of the two objects being compared are the same. It means that an aggregate relationship exists. When an object of class B is a member of class A, it can be said that class A "has a" class B object. The program will crash. It is not advisable because it will allow access to the private fields. The exception to this is when the field is a String object. This is because String objects are immutable, meaning that they cannot be changed. Gaddis: Starting Out with Java: From Control Structures through Data Structures, 2/e © 2012 Pearson Education 10. The key word this is the name of a reference variable that an object can use to refer to itself. It is available to all non-static methods. a) Color b) Color.RED, Color.ORANGE, Color.GREEN, Color.BLUE c) Color myColor = Color.BLUE; a) POODLE BOXER TERRIER b) 0 1 2 c) BOXER is NOT greater than TERRIER 11. When there are no references to it. 8. 9. 3