JMU JMU - Department of Computer Science
Help Tools
Lab: Experimenting with Collections (Part I)


Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.

Getting Ready: Before going any further, you should:

  1. Depending on your development environment, create either a directory or a project for this lab.
  2. Setup your development environment.
  3. Download the following files:
    to an appropriate directory/folder. (In most browsers/OSs, the easiest way to do this is by right-clicking/control-clicking on each of the links above and then selecting Save as... or Save link as....)

1. A Review: This part of the lab will help you recall/remember some important concepts about arrays of objects.
  1. Open ColorFinder.java.
  2. Are colors and names local variables or attributes of ColorFinder objects?


    Attributes.
    Expand
  3. What "kind of thing" is colors?


    Conceptually, it is an array of Color objects. In fact, it is really an array of references to Color objects.
    Expand
  4. What "kind of thing" is colors[0]?


    Conceptually, it is a Color object. In fact, it is a reference to a Color object.
    Expand
  5. What "kind of thing" is names?


    Conceptually, it is an array of String objects. In fact, it is really an array of references to String objects.
    Expand
  6. What statement allocates memory for colors?


           colors = new Color[147];
        
    Expand
  7. After this statement has been executed, how many actual Color objects have been instantiated?


    None.
    Expand
  8. What statement instantiates the Color objects?


           value = new Color(red, green, blue);
        
    Expand
  9. What statement assigns Color objects to elements of the array colors?


           colors[i] = value;
        
    Expand
  10. How might you combine these two statements into one (and eliminate the need for the variable named value)?


           colors[i] = new Color(red, green, blue);
        
    Expand
2. A Review of Conformal Arrays: This part of the lab will help you remember how conformal arrays (i.e., multiple arrays of the same size) can be used to store and search for objects of various kinds.
  1. Make sure you understand the constructor in the ColorFinder class. In particular, make sure you understand how the colors and names arrays are being used.
  2. Complete the getColor() method in the ColorFinder class. (Hint: Loop through the names array until you find the appropriate index.)
  3. What code did you add?


           for (int i=0; i<names.length && result==null; i++)
           {
              if (description.equals(names[i])) 
              {
                 result = colors[i];
              }
           }
        
    Expand
  4. Open, compile and execute the Driver.
  5. What output is generated when you enter "snow"?


    snow: java.awt.Color[r=255,g=250,b=250]
        
    Expand
  6. What output is generated when you enter "apricotice"? Why?


    No output is generated since there is no such color.
    Expand
3. Using List Objects: This part of the lab will help you become acquainted with the List interface.
  1. Rename your current version of ColorFinder.java to ColorFinder.arrays.
  2. Re-download the original version of ColorFinder.java and open it.
  3. Change the declaration of colors and names so that both are now List objects.
  4. What changes did you make?


        private ArrayList         colors, names;       
        
    Expand
  5. Recall that you must import something in order to use the List interface. What line do you have to add to the top of your class as a result?


        import java.util.List;
        
    Expand
  6. Change the instantiation of the colors and names objects so that they are now ArrayList objects.
  7. What changes did you make?


           colors = new ArrayList();
           names  = new ArrayList();
        
    Expand
  8. What is the major advantage of using List objects rather than arrays?


    They do not have a pre-defined size.
    Expand
  9. Change the statements in the for loop that store the names and colors. (Hint: Look at the documentation for the ArrayList java.util.ArrayList class.)
  10. What changes did you make?


              names.add(key);
              colors.add(value);
        
    Expand
  11. Compile the ColorFinder class.
  12. Even after you fix the minor mistakes you may have made you should get a message something like cannot find symbol ArrayList. Why did you get this message?


    The ArrayList class was not imported.
    Expand
  13. Fix this error.
  14. What changes did you make?


        import java.util.*;
        
    Expand
  15. Add the following code to the getColor() method in the ColorFinder class:
           String          key;
           
           
           
           for (int i=0; i<names.size() && result==null; i++)
           {
              key = names.get(i);          
              if (description.equals(key)) 
              {
                 result = colors.get(i);
              }
           }
        
  16. When will this for loop terminate?


    When there are no more names to check or when the key has been found.
    Expand
  17. Compile the ColorFinder class.
  18. What error messages were generated?


    ColorFinder.java:106: incompatible types
    found   : java.lang.Object
    required: java.lang.String
              key = names.get(i);
                             ^
    ColorFinder.java:109: incompatible types
    found   : java.lang.Object
    required: java.awt.Color
                 result = colors.get(i);
                                    ^
    
    Expand
  19. Fix these errors by typecasting the objects that are returned by the get() method calls.
  20. What changes did you make?


              key = (String)names.get(i);          
        

    and

                 result = (Color)colors.get(i);
        
    Expand
  21. Compile the ColorFinder class.
  22. The compiler still generates a "Note" when you compile the class. What "Note" is generated?


    Note: ColorFinder.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
        
    Expand
  23. Why is this "Note" generated?


    Because we did not ensure type safety.
    Expand
4. Using a Map: This part of the lab will help you understand how to use Map objects and some of the benefits of hash maps.
  1. Copy your current version of ColorFinder.java to ColorFinder.arraylists. (You may need this class in the future. Don't lose it!)
  2. Replace the declaration of the colors and names objects with the following:
        private Map         colors;
        
  3. Delete the statements that instantiate the colors and names objects.
  4. Instantiate the colors object as a HashMap.
  5. What code did you add?


           colors = new HashMap();
        
    Expand
  6. Replace the calls to the add() methods of the names and colors objects in the constructor of the ColorFinder class with the following:
              colors.put(key, value);
        
  7. Complete the getColor() method in the ColorFinder class so that it now uses the HashMap named colors. (Hint: Look at the documentation for the HashMap java.util.HashMap class.)
  8. What code did you add?


        public Color getColor(String description)
        {
           Color           result;
           
           
           result = (Color)colors.get(description);
           return result;       
        }
        
    Expand
  9. Compile the ColorFinder class.
  10. The compiler still generates a "Note" when you compile the class. What "Note" is generated?


    Note: ColorFinder.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
        
    Expand
  11. Why is this "Note" generated?


    Because we still have not ensured type safety.
    Expand
  12. Rename your current version of ColorFinder.java to ColorFinder.hashmap. (You may need this class in the future. Don't lose it!)

Copyright 2021