Write a program that creates Pet objects from data read from the keyboard. Store these objects into an instance of ArrayList. Then sort the Pet objects into alphabetic order by pet name, and finally display the data in the sorted Pet objects on the screen. The class Pet is given in Chapter 6, Listing 6.1.
This Project has the potential for being more difficult than the Practice Programs since it uses a list of elements other than Strings, which the author states can lead to subtle problems. The PetRecord variable nextPet must be declared inside the while-loop so it is created fresh each iteration. It must be redeclared each time to ensure that PetRecords previously added to the list are not overwritten with data entered on the next iteration. If nextPet is created only once, outside the while-loop (before entering it), the new values entered are written to same address each time and all the PetRecords in the list will end up with identical data, the values entered in the last iteration.
See the code in PetRecordsSortedByName.java. Uses PetRecord.java.
You might also like to view...
A(n) ________ ScreenTip provides more descriptive text than a normal ScreenTip
A) extended B) preview C) enhanced D) compressed
Which statement about symbolic constants and/or macros is true?
a) The effects of symbolic constants and macros, once defined, cannot be modified. b) Preprocessor directive #removedef undefines a symbolic constant or macro. c) The scope of a symbolic constant or macro is normally from its point of definition to the end of the file, but this can be modified with a preprocessor directive. d) A name can always be redefined with #define.