Question: Create a package called cePet . Add two classes: Pet and PetApp, which includes the main method. Implement the class Pet, as described below. It
- Create a package called cePet. Add two classes: Pet and PetApp, which includes the main method.
- Implement the class Pet, as described below. It should be immutable and include the following:
- Three fields: name, age, and species
- A parameterized constructor that initializes all three fields
- Three getters (no setters)
- A toString method that returns a string of the following format: {species} {name}({age}) Where {name}, {age}, and {species} are replaced with the actual field values. E.g., dog Buddy(8)
- Implement the class PetApp.
- Create a symbol table of type BinarySearchST and call it st. It uses pet IDs, which are whole numbers, as keys and associates with them Pet object. Each pet has a unique ID. When you are this far, compare.
- Add the first entry to the symbol table. The key should be a random number from the range [10, 30), and the pet is an 8-year-old dog named Buddy.
- Add nine more pets of your choice to the symbol table. All keys should be a random number from the range [10, 30). However, make sure they are unique. Find a method in BinarySearchST (Links to an external site.)that allows you to check whether a key is already in the symbol table. When you found one, compare.
- After adding all the pets to the symbol table, print the ten keys in a single line. Start by looking for a method in BinarySearchST (Links to an external site.)that allows you to access all the keys in the symbol table. When you found one, compare. Then implement the challenge.
- Print all the names of the pets in the symbol table in a single line. When you are done, compare.
- Check whether 10 is a key in the symbol table. If so, print the corresponding pet. If not, print a message that the given key was not found. Repeat for the keys 17 and 23. When you are this far, compare.
- Find the smallest pet ID (no hard-coding) Identify the method that is best suited for this task. When you found it, compare. Then implement the challenge.
- Find the smallest pet ID that is greater than 17 (no hard-coding) Identify the method that is best suited for this task. When you found it, compare. Then implement the challenge.
- Find the number of pets that are in the symbol table. Identify the method that is best suited for this task. When you found it, compare. Then implement the challenge.
- Add a 3-year-old dog named Waldi with pet ID 30 to the symbol table.
-
Find the number of pet IDs that are smaller than 25.
Identify the method that is best suited for this task. When you found it, compare. Then implement the challenge.
- Print the pet with the pet ID 30.
- Find the second smallest pet ID and print the corresponding pet. Identify the method that is best suited for this task. When you found it, compare. Then implement the challenge.
- Waldi got a new owner, who wants to call him Strolch. Change the pet that is associated with the pet ID 30. It should not longer be called Waldi but Strolch instead. Remember, class Pet is immutable and thus can't be changed. Identify the method that is most helpful for this task. When you found it, compare. Then implement the challenge.
- Print all pets from the symbol table in straight columns. The left column should list the pet ID (2 digits) The right column should list the pet (name, age, species). Include a header and proper spacing.
Sample Output
Notice that the pet IDs will differ from your output because they are randomly selected. The pets are likely to differ, too, because most of them were up to your choice. Make sure though that your output includes labels and full sentences, as demonstrated in this sample output. Also, use single empty lines to group related output.
All keys : 10 11 12 13 15 18 20 24 25 27 All pet names: Bobo Buddy Hazel Smokey Bella Leo Garfield Maddox Sylvester Wolf
Pet associated with 10: tortoise Bobo(50) The key 17 was not found in the symbol table. The key 23 was not found in the symbol table.
Smallest pet ID: 10 Smallest pet ID greater or equal to 17: 18 Number of pets: 10 Pet IDs smaller 25: 8 Pet with ID 30: dog Waldi(3) Second smallest pet ID: 11
IDs Pets --- ------------- 10 tortoise Bobo(50) 11 dog Buddy(8) 12 rat Hazel(4) 13 goldfish Smokey(5) 15 cat Bella(5) 18 tortoise Leo(9) 20 cat Garfield(6) 24 dog Maddox(2) 25 cat Sylvester(7) 27 dog Wolf(9) 30 dog Strolch(3)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
