JMU JMU - Department of Computer Science
Help Tools
Lab: Gaining Experience with the Iterator Pattern


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....

1. Getting Acquainted with the Existing Code: This part of the lab will help you get acquainted with the existing CourseTime and CourseSchedule classes.
  1. Read and understand the documentation for the CourseTime and CourseSchedule classes.
  2. Add code to the following application so that it constructs a CourseTime at (or after) 9:05 on MWF and prints its String Representation.
    import java.util.Iterator;
    
    
    public class Driver
    {
        public static void main(String[] args)
        {
            CourseSchedule mwf = new CourseSchedule(8, 0, 50, 15, 10);
            CourseSchedule th  = new CourseSchedule(8, 0, 75, 15,  7);
    
    
        }
    }
    

    What code did you add?


            CourseTime time = new CourseTime(9, 5, mwf);
            System.out.println(time);
     
    Expand
  3. Execute your application. What was output?


    9:05AM- 9:55AM
    Expand
  4. Modify your application so that it constructs and prints the first CourseTime at or after 9:06 on MWF. What code did you change/add?


            CourseTime time = new CourseTime(9, 6, mwf);
    
    Expand
  5. Execute your application. What was output and why?


    10:10AM-11:00AM was output because that is the first "slot" at or after 9:06AM (since 9:05AM- 9:55AM is before 9:06AM).
    Expand
  6. Modify your application so that it now prints the first valid CourseTime on MWF. What code did you change/add.


            CourseTime time = new CourseTime(mwf);        
    
    Expand
  7. Modify your application so that it now prints the first three valid CourseTimes on MWF. Specifically, construct a CourseTime that corresponds to the first possible CourseTime and then use information from the CourseSchedule to construct the next two CourseTime objects. What code did you change/add?


            CourseTime first  = new CourseTime(mwf);
            System.out.println(first);
            int        offset = mwf.getLength() + mwf.getGap();        
            for (int i=0; i<2; i++)
            {
                CourseTime time = CourseTime.createLaterCourseTime(first, i*offset);
                System.out.println(time);
            }
    
    Expand
  8. Modify your application so that it achieves the same thing without having to make use of the CourseSchedule. Instead, make use of the fact that, as you saw earlier, CourseTime objects are "rounded up". What code did you add?


            CourseTime time = new CourseTime(mwf);        
            System.out.println(time);
            for (int i=0; i<2; i++)
            {
                time = CourseTime.createLaterCourseTime(time, 1);            
                System.out.println(time);
            }
    
    Expand
2. Motivation: This part of the lab will provide motivation for the remainder of the lab.
  1. Modify your application so that it constructs and prints all of the CourseTimes on MWF. What code did you change/add?


            CourseTime time = new CourseTime(mwf);        
            System.out.println(time);
            while (!time.isLast())
            {
                time = CourseTime.createLaterCourseTime(time, 1);            
                System.out.println(time);
            }
    
    Expand
  2. What is awkward about this code?


    The first time and the subsequent times are treated differently.
    Expand
  3. What "expertise" is required by this code?


    You have to know that CourseTime objects are "rounded up" (or you have to work with information from the CourseSchedule) and you have to understand the isLast() method.
    Expand
  4. What design pattern addresses these kinds of situations and how is it used in Java?


    The Iterator Pattern addresses these kinds of situations. In Java, there are Iterables and Iterators. Also, you can use a for-each loop with Iterables.
    Expand
3. Implementing the Iterator Pattern: This part of the lab will give you experience implementing an Iterator. Specifically, it will give you experience implementing the following design.
iterator.png
  1. Implement the CourseTimeIterator illustrated in the UML diagram above. What code did you write?


    public class CourseTimeIterator implements Iterator<CourseTime>
    {
        private boolean          hasNext;    
        private CourseTime       next;
        
    
        public CourseTimeIterator(CourseTime first)
        {
            next    = first;
            hasNext = true;        
        }
        
        public boolean hasNext()
        {
            return hasNext;
        }
        
        public CourseTime next() throws NoSuchElementException
        {
            if (!hasNext()) throw new NoSuchElementException();
    
            CourseTime result;
            result = next;
            next   = CourseTime.createLaterCourseTime(next, 1);
            if (result.isLast()) hasNext = false;
    
            return result;
        }
    }
    
    Expand
  2. Modify the CourseTime class so that it is consistent with the UML diagram above. What code did you change/add?


    I modified the declaration of the class as follows:
    public class CourseTime implements Comparable<CourseTime>, Iterable<CourseTime>
    

    and added the following method:

        public Iterator<CourseTime> iterator()
        {
            return new CourseTimeIterator(this);        
        }
    
    Expand
4. Using the Iterator Pattern: This part of the lab will give you experience using the Iterator Pattern.
  1. Modify your application so that it explicitly uses an Iterator to print all of the MWF CourseTimes. What code did you change/add?


            CourseTime first = new CourseTime(8, 0, mwf);
            Iterator<CourseTime> times = first.iterator();
            while (times.hasNext()) System.out.println(times.next());
    
    Expand
  2. Modify your application so that it, instead, uses a for-each loop. What code did you change/add?


            CourseTime first = new CourseTime(8, 0, mwf);
            for (CourseTime t: first) System.out.println(t);
    
    Expand
5. Alternative Solutions: This part of the lab will help you understand that good problem solvers/designers generate, evaluate, and compare alternative solutions to the same problem.
  1. Create an alternative implementation of the CourseTimeIterator class. For example, the implementation above uses the isLast() method in the CourseTime class. Create an implementation that does not. For example, create one that uses the compareTo() method. What code did you add?


    public class CourseTimeIterator implements Iterator<CourseTime>
    {
        private CourseTime       current, next;
        
    
        public CourseTimeIterator(CourseTime first)
        {
            current = new CourseTime(first);
            next    = CourseTime.createLaterCourseTime(current, 1);
        }
        
        public boolean hasNext()
        {
            return (next.compareTo(current) > 0);
        }
        
        public CourseTime next() throws NoSuchElementException
        {
            if (!hasNext()) throw new NoSuchElementException();
            CourseTime    result;
         
            result  = current;
            current = next;
            next    = CourseTime.createLaterCourseTime(current, 1);
    
            return result;
        }
    }
    
    Expand
  2. Compare the different implementations using the dimensions of quality discussed in lecture and the readings.

Copyright 2025