Question: 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

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!