Carl Reynolds and Paul Tymann
"Schaum's Outline of Principles of Computer Science"
The
syntax says that we will only be comparing an Automobile
object with another Automobile. If, by some error of programming, our program tries to compare an
Automobile object with a Thermostat object, the JVM will generate an error.
The new compareTo() method implements the Comparable interface for the class Automobile.
We??™ve taken a shortcut here which takes advantage of the toString() method we already have for
Automobile. The phrase this.toString() will return a String object which represents this instance
of an Automobile. The key word this always references the particular instance itself. The String
returned will be of this form:
2002 VW Passat
The year will be followed by the make and the model of the Automobile. Likewise, the phrase
car.toString() will return a String representing the other Automobile, such as this:
2006 Kia Rio
The String class has a compareTo() method that orders Strings in ???ASCIIbetical??? order, which is
like alphabetical order, except that it follows the ASCII encoding values of characters. In ASCII the letters are
coded in alphabetical order, but all uppercase letters come before any of the lower-case letters, and digits and
symbols are included as well as letters.
Pages:
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230