Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1. Write a Dog class (a dog has a name). Write a toString() method for this class. 2. Create a Collection (use an ArrayList) of
1. Write a Dog class (a dog has a name). Write a toString() method for this class. 2. Create a Collection (use an ArrayList) of dogs and put three dogs in it (Fido, Spot, Lucy). 3. Print the whole Collection without using an explicit iterator, loop, or recursion. 4. Using a for-each construct (also called an enhanced for-loop, but not a C-style standard for loop), traverse the Collection, and only print if the dog's name is Spot. 5. Create an Iterator over the dogs. Write a standard for loop (it will be ugly) over your Iterator. Try each of the following, and comment each one out after you try it. Note the effects/exceptions in your comments. Hint: don't do this in Eclipse, it is easier to see the errors by separately compil- ing/running in the shell. (a) Inside the for loop, if the dog's name is Spot, add a new dog to the Collection inside the loop. (b) Inside the for loop, if the dog's name is Spot, add a new dog to the Iterator inside the loop. (c) Inside the for loop, if the dog's name is Spot, remove Spot from the Collection inside the loop. (d) Inside the for loop, if the dog's name is Spot, remove Spot from the Iterator inside the loop. Think carefully: why do some things not work? Is that how you would implement the language? 6. Now change the type of your Collection to a HashSet. If you have coded with good Collection style, you will not have to make any other changes for your code to work
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started