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


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 (e.g., the course downloads 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....
  4. robotics.jar contains a Direction.class and Robot.class, both of which are in the robotics package.

1. Making a List Type Safe: This part of the lab will help you understand how to use type-safe collections.
  1. Copy the file ColorFinder.arraylists from your solution to the lab on "Experimenting with Collections I" to your directory for this lab.
  2. Rename ColorFinder.arraylists to ColorFinder.java.
  3. Change the declaration of colors so that it is a List of Color objects.
  4. What change did you make?
        private List<Color>         colors;       
        
    Expand
  5. Change the declaration of names so that it is a List of String objects.
  6. What change did you make?
        private List<String>        names;       
        
    Expand
  7. Change the instantiation of the colors and names objects.
  8. What changes did you make?
           colors = new ArrayList<Color>();
           names  = new ArrayList<String>();
        
    Expand
  9. "Fix" the getColor() method so that it no longer casts the objects returned by the calls to get().
  10. What changes did you make?
              key = names.get(i);          
        

    and

                 result = colors.get(i);
        
    Expand
    Expand
  11. Was it necessary to remove the type cast?
    No. But, there is no reason to cast a String as a String or a Color as a Color.
    Expand
  12. Rename ColorFinder.java to ColorFinder.arraylists.
2. Making a Map Type Safe: This part of the lab will help you understand how to use type-safe Map objects.
  1. Copy the file ColorFinder.hashmap from your solution to the lab on "Experimenting with Collections I" to your directory for this lab.
  2. Rename ColorFinder.hashmap to ColorFinder.java.
  3. Change the declaration of colors so that it is a Map that uses String objects for keys and Color objects for values.
  4. What change did you make?
        private Map<String,Color>         colors;
        
    Expand
  5. Instantiate the colors object as (an appropriately typed) HashMap
  6. What change did you make?
           colors = new HashMap<String,Color>();
        
    Expand
  7. Rename ColorFinder.java to ColorFinder.hashmap.
3. Passing a Simple Type-Safe Collection: This part of the lab will help you understand how to pass type safe collections as parameters.
  1. Open the Bender and Driver1 classes.
  2. The Robot class includes the following methods:
            protected boolean canMove(Direction d)
    
            protected void move(Direction d)
    
            protected boolean haveBeen(Direction d)
        

    Complete the execute() method in the Bender class using these methods based on the comments describing this method. (Note: You need not use the haveBeen() method.) Do not use an Iterator.

  3. What code did you add?
        public void execute(List<Direction> method)
        {
           Direction        d;
           
    
           for (int i=0; i<method.size(); i++)
           {
              d = method.get(i);
    	  move(d);          
           }
        }
        
    Expand
  4. Compile and execute Driver1.
  5. Modify the execute() method so that it uses an Iterator.
  6. What is your execute() method now?
        public void execute(List<Direction> method)
        {
           Direction              d;
           Iterator<Direction>    i;
    
           i = method.iterator();
           while (i.hasNext())
           {
               d = i.next();
               move(d);
           }
        }
        
    Expand
4. Creating a Simple Type-Safe Collection: This part of the lab will help you understand how to read a text file and use it to create a simple type-safe collection.
  1. Open the Driver2 class.
  2. Add the following method to the Bender class and complete it. (For this question you should assume that there are four directions: Direction.FORWARD, Direction.RIGHT, Direction.BACKWARD, and Direction.LEFT.)
         * Read instructions from a file
         *
         * @param name   The name of the file
         * @return       An ArrayList of Direction objects
         */
        public List<Direction> read(String name)
        {
    
    
        }
        
  3. What code did you add?
        public List<Direction> read(String name)
        {
           File                          file;       
           Scanner                       in;       
           String                        line;       
           List<Direction>    method;
           
    
           try
           {
              file    = new File(name);
              in      = new Scanner(file);
              method  = new ArrayList<Direction>();
              
    
              while (in.hasNext())
              {
    	     Direction      d;
    
    	     line = in.nextLine();
    	     d = Direction.valueOf(line);
    	     if (d != null) method.add(d);
    
                 // This could also be accomplished as follows:
                 //line = in.nextLine();
                 //if      (line.equals("FORWARD"))  method.add(Direction.FORWARD);
                 //else if (line.equals("RIGHT"))    method.add(Direction.RIGHT);
                 //else if (line.equals("BACKWARD")) method.add(Direction.BACKWARD);
                 //else if (line.equals("LEFT"))     method.add(Direction.LEFT);
              }
           }
           catch (IOException ioe)
           {
              method = null;
           }
           
           return method;       
        }
        
    Expand
  4. Compile and execute Driver2.
  5. This version of the Direction class includes Direction.CONTINUE. Add a line to the read() method that accounts for this Direction.
  6. What code did you add?
                 else if (line.equals("CONTINUE")) method.add(Direction.CONTINUE);
        
    Expand
  7. Modify the execute() method so that it handles Direction.CONTINUE. Specifically, when a Direction.CONTINUE is encountered the execute() method must get the next element in the method collection and then repeatedly move the Robot in that direction until it can no longer do so.
  8. What code did you add?
        // Using a loop
        public void execute(List<Direction> method)
        {
           Direction        d;
           
    
           for (int i=0; i<method.size(); i++)
           {
              d = method.get(i);
    
              if (d.equals(Direction.CONTINUE))
              {
                 i++;
                 d = method.get(i);
                 while (canMove(d)) move(d);             
              }
              else
              {
                 move(d);          
              }
           }
        }
    
    
        // Using an Iterator
        public void execute(List<Direction> method)
        {
           Direction        d;
           Iterator<Direction>    i;
    
           i = method.iterator();
           while (i.hasNext())
           {
              d = i.next();
    
              if (d.equals(Direction.CONTINUE))
              {
                 d = i.next();
                 while (canMove(d)) move(d);             
              }
              else
              {
                 move(d);          
              }
           }
        }
        
    Expand
5. Using Complicated Type-Safe Collections: This part of the lab will help you understand more complicated type-safe collections.
  1. Read and understand Driver3.
  2. Add the following attribute to the Bender class:
        private Map<String, List<Direction>>    library;
        
  3. Make sure you understand this declaration.
  4. Instantiate the library attribute in the constructor of the Bender class.
  5. What code did you add?
           library = new Map<String, List<Direction>>();
        
    Expand
  6. Add the following method to the Bender class and complete it:
        /**
         * Add a method to the existing library
         *
         * @param method   The method to add
         */
        public void add(String name, List<Direction> method)
        {
    
        }
        
  7. What code did you add?
    
        /**
         * Add a method to the existing library
         *
         * @param method   The method to add
         */
        public void add(String name, List<Direction> method)
        {
           library.put(name, method);       
        }
        
    Expand
  8. Add the following method to the Bender class and complete it:
        /**
         * Execute a named set of instructions from the library.
         * Specifically, retrieve a named set of instructions from
         * the library and then call the other version of
         * execute().
         *
         * @param name   The name of the method
         */
        public void execute(String name)
        {
        }
        
  9. What code did you add?
        /**
         * Execute a named set of instructions from the library.
         * Specifically, retrieve a named set of instructions from
         * the library and then call the other version of
         * execute().
         *
         * @param name   The name of the method
         */
        public void execute(String name)
        {
           List<Direction>        method;
           
           method = library.get(name);
           if (method != null) execute(method);
        }
        
    Expand
  10. Compile and execute Driver3.

Copyright 2024