Solutions to other chapters:
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26  
Java Methods Home Page Skylight Publishing



Java Methods A & AB:
Object-Oriented Programming and Data Structures

Answers and Solutions to Exercises

Chapter 9

1.   public String replace(String str, char ch)
2. (a) F -- a no-args constructor is not specified.
(b) T -- the int parameter is promoted to double.
4.   Yes for String: its documentation describes the following constructor:
"String(String value) -- Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string."

No for Color: its documentation describes seven constructors, but none of them is a copy constructor.

9. (b) public Disk(Disk d) { center = new Point(d.center); radius = d.radius; }
12.   Objects of subclasses of Integer or String would not be necessarily immutable; if such objects were passed to library methods that rely on immutability, these methods may stop working properly.
15.   This class will not compile because the two swap methods differ only in their return types.  A way to fix it is to rename one of the methods, for example makeSwappedPair for the second method.
18.   A static method (main) calls a non-static method (hello).  hello should be declared static.

Copyright © 2006 by Skylight Publishing