Question: Start with this source code and fix it. ' /** * This is the starting point code for Lab: Reindeer. */ public class Reindeer {
Start with this source code and fix it. ' /** * This is the starting point code for Lab: Reindeer. */ public class Reindeer { // The reindeer private java.util.ArrayList reindeer; /** * Constructor, which you will write */ /** * Add the names of the reindeer to the collection. */ public void addReindeer() { reindeer.add("Dasher"); reindeer.add("Dancer"); reindeer.add("Prancer"); reindeer.add("Vixen"); reindeer.add("Comet"); reindeer.add("Cupid"); reindeer.add("Dunder"); reindeer.add("Blixem"); } /** * A simple method to print out the contents of * the collection. */ public void print() { for(Object o : reindeer) { System.out.print(o + " "); } System.out.println(); } /** * The application method, which you will write * @param args Command-line arguments */ } This code simply declares a private field to store the reindeer names and two methods: one adds eight names1 to the list and the other prints out the contents of the list.
Compile the code as downloaded. You will see that it generates eight warning messages. This is expected. We'll take care of this during the lab.
Complete a Basic Implementation
Update the class with a constructor. In the constructor, instantiate an ArrayList and assign it to the field. Notice the JavaDoc comment at line 9.
Add an application method. In main, instantiate the class and call the add and print methods for the class. Notice that the print method uses the for-each loop, the 'alternate' version of the for loop. There is a JavaDoc comment for the application method, starting on original line 38.
Compile and run the application. It should print out the eight names on a single line. Notice that the warning messages do not preclude running the application.
Handle the Warning Messages
The warning messages are due to using a non-specific collection (generic) type. The class java.util.ArrayList is generic.
public class ArrayList
The warning messages can be handled by specifying the type parameter, E.
Update the declaration and the instantiation of the ArrayList by specifying the type parameter. In this application, we are storing java.lang.String objects in the collection.
Remember that the package java.lang is automatically imported by the Java compiler.
1) Give the code for the field declaration after you specified the type parameter for ArrayList.
2) Give the code for the instantiation of the field from the constructor. This should be the version with no warning messages.
Compile and run the application again. You should see no change in the behavior. (Other than that there are no compiler warning messages.)
Use a Different List Type
Update the application method to replace the ArrayList object with an instance of java.util.LinkedList.
To do this, you will need to change the declaration of the field reindeer to be the interface type, java.util.List. This works because both ArrayList and LinkedList implement List. Don't change the constructor.
In main, add lines to instantiate a LinkedList and bind it to reindeer. Then call the add and print methods. The specific of the instantiation are left as an exercise for the student. There should be no compiler warning messages. Recall the type parameter.
{ Reindeer app = new Reindeer(); System.out.println("Using an ArrayList..."); app.addReindeer(); app.print(); System.out.println("Using a LinkedList..."); app.reindeer = new java.util.Linked app.addReindeer(); app.print(); }
Test the application.
3) Give the source code line from main where you instantiate the LinkedList object.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
