\documentclass[12pt]{report}

\begin{document}

\title{com1101 Lab 8}
\author{Greg Pettyjohn}
\date{\today}
\maketitle

\section*{Part 1 Iterating With For Loops}
In this lab we will use the IRange interface to iterate over collections.

Open the {\tt IRangeTest.java} file and study the code for the methods
{\tt containsBlueEyedPerson} and {\tt allOverEighteen}.

\begin{enumerate}
 \item {\tt IRangeTest.java} contains several definitions of data structures to help
       speed up testing. Add a few more definitions of your own.
  
 \item Design the method {\tt containsPersonWithEyecolor} which consumes an {\tt IRange}
       collection and a {\tt Color} object and determines if there is a {\tt Person} in
       the collection with that eyecolor.
 \item Design the method {\tt allOlderThan} which consumes an {\tt IRange} collection and
       a number of years and determines whether all the {\tt Person}s in the collection are
       older than the given age.
 \item Rewrite {\tt containsBlueEyedPerson} and {\tt allOverEighteen} to use \linebreak
       {\tt containsPersonWithEyecolor} and {\tt allOlderThan} as helper functions.
       Reuse the tests!
\end{enumerate}

\section*{Part 2 Writing andMap and orMap}
We can write versions of the {\tt andMap} and {\tt orMap} methods that will work with
{\tt IRange} collections.

\begin{enumerate}
 \item Open the file {\tt IPerson2Bool.java} and study the {\tt IPerson2Bool} interface.
       Write a class that implements {\tt IPerson2Bool} and can be used to determine whether a person
       has blue eyes. Also write a class that implements {\tt IPerson2Bool} and can be
       used to determine whether a {\tt Person} is over 18 year old.
 \item The header for {\tt andMap} can be found in {\tt IRangeTests.java}.
       Write {\tt andMap}, which consumes an {\tt IPerson2Bool} object and an {\tt IRange} collection
       and determines if all of the {\tt Person}s in the collection satisfy the
       {\tt IPerson2Bool} object.
 \item The header for {\tt orMap} can be found in {\tt IRangeTests.java}.
       Write {\tt orMap}, which determines whether at least one {\tt Person} in a {\tt IRange} collection
       satisfies an {\tt IPerson2Bool} object.
 \item Rewrite {\tt containsBlueEyedPerson} and {\tt allOverEighteen} to use {\tt orMap} and
       {\tt andMap}. Use the classes from step 1. Reuse tests!
 \item Create examples that use {\tt andMap} and {\tt orMap} with anonymous inner classes.
\end{enumerate}

\section*{Part 3 Using the IRange Interface in a Recursion}
Here is an example of a function, {\tt totalYears} that sums up the ages of all the {\tt Person}s
in a collection. In the function I use an extra pair of {\tt\{}'s to introduce a local variable
{\tt newTotal}. Why must I use the local variable?

\begin{verbatim}
    // totalYears
    // To sum up all the ages of the People in the given IRangeCollection
    int totalYears(IRange i, int total) {
        if (i.hasMore()) {
            {
                int newTotal = total + ((Person)i.current()).age;
                i.next();
                return totalYears(i, newTotal);
            }
        }
        else
            return total;           
    }
\end{verbatim}

\begin{enumerate}
  \item Design {\tt listPersons} which creates a {\tt AListPerson} object using all the {\tt Person}s
        in an {\tt IRange} collection.
  \item How about sort?
  \item Can you rewrite these methods using a {\tt for} loop?  
\end{enumerate}
\end{document}
