CPE 102

Winter 2008

Lab 8

 

Objectives

 

 

Resources

 

 

Ground Rules

 

 

Orientation 

 

You will be developing two Iterator classes, one called PowerSequence and one called RealIterator. These classes will also implement the Iterable interface so that they can be used with the foreach construct.  Because these classes make use of the double primitive type and Double wrapper class you will also likely deepen your understanding of this type and it associated wrapper class.

 

Specification

 

  1. Implement a class called PowerSequence as follows:

 

    1. The class should have the minimally necessary instance variable(s) to support the specified functionality.

 

    1. You may assume all parameters passed to the constructors will be zero or positive.

 

    1. The class must implement a single constructor that accepts a double parameter representing the base-value to use when generating the sequence.  The sequence is described below.

 

    1. The class must implement the Iterator interface.  The required remove method should be implemented, per the documentation, to be unsupported.  Calling the next method repeatedly must return the base raised to the zero-through-n power where n is the power that causes the value to be so large it is not supported by the double data type.  You can read about the limits of double in the Java documentation for the Double wrapper class.  The hasNext method should return true if there is another valid value, i.e., one that does not exceed the capacity of a double, in the sequence, otherwise false.  For example, passing in a base of 2 to the constructor would result in the following sequence:

 

1, 2, 4, 8, 16, 32, …, n (where n is the last value before exceeding the capabilities of the double data type)

 

    1. The class must implement the Iterable interface.  By doing so you can use the class with the foreach construct.  An example follows:

 

for (Double d : SomeIterableReferenceThatReturnsDoubleValuesHere)

{

   System.out.println(d);

}

 

This loop will print the each and every double value generated by the provided Iterable object (in this example, SomeIterableReferenceThatReturnsDoubleValuesHere).  Remember the Java keyword, this, when implementing the single method the Iterable interface requires!

 

    1. When developing your JUnit tests, use the foreach construct to test your implementation, and be sure to test it well. Does your implementation return the correct values in the specified sequence? Does it return false when there are no more valid values (but not sooner)? Testing requires some thought since the double data type can support such large values – be sure to solve this little conundrum. Discuss it with your peers and be sure to ask (after some effort on your own) if you can’t figure it out.

 

  1. Implement a class called RealIterator as follows:

 

    1. The class should have the minimally necessary instance variable(s) to support the specified functionality.

 

    1. You may assume all parameters passed to the constructors will be zero or positive.

 

    1. The class must implement a constructor that accepts a double parameter representing the amount to increment by.  Iterators created with this constructor start at zero and end when the next value exceeds the capabilities of the double data type.

 

    1. The class must implement a constructor that accepts two double parameters: the first representing the amount to increment by, and the second representing the value to begin incrementing from.  Iterators created with this constructor end when the next value exceeds the capabilities of the double data type.

 

    1. The class must implement a constructor that accepts three double parameters: the first representing the amount to increment by, the second representing the value to begin incrementing from, and the third representing the value to stop at, exclusive.

 

    1. The class must implement the Iterator interface.  The required remove method should be implemented, per the documentation, to be unsupported.  Calling the next method repeatedly must return values between the starting value and the ending value, increasing by the specified increment amount each time.  The hasNext method should return true if there is another valid value in the sequence, otherwise false.  For example, passing in an increment amount of 0.1 to the constructor would result in the following sequence:

 

0, 0.1, 0.2, 0.3, 0.4, …, Double.MAX_VALUE

 

Note that, due to the finite nature of the double data type, you may see small variations from the expected value.  Also, depending on the sequence you may not see the exact value for Double.MAX_VALUE.

 

    1. The class must implement the Iterable interface.  By doing so you can use the class with the foreach construct.  An example follows:

 

for (Double d : SomeIterableReferenceThatReturnsDoubleValuesHere)

{

   System.out.println(d);

}

 

This loop will print the each and every double value generated by the provided Iterable object (in this example, SomeIterableReferenceThatReturnsDoubleValuesHere).  Remember the Java keyword, this, when implementing the single method the Iterable interface requires!

 

    1. When developing your JUnit tests, use the foreach construct to test your implementation, and be sure to test it well.  Does your implementation return the correct values in the specified sequence?  Does it return false when there are no more valid values (but not sooner)?  Testing requires some thought since the double data type can support such large values – be sure to solve this little conundrum.  Discuss it with your peers and be sure to ask (after some effort on your own) if you can’t figure it out.