CSci 2101 Problem set 4.

Due Wednesday June 9 at 11:59pm

You may work individually or in pairs.

24 points

Problem 1 (12 points): Random integers iterator.

Write a class RandomInts that produces n random integers between min (inclusive) and max (exclusive) as an iterator. This means that you can use it like this:

RandomInts myInts = new RandomInts(1, 101, 8);
for (int i: myInts){

The loop should give you 8 pseudo-random numbers uniformly distributed between 1 and 100 (inclusive).

The iterator in the class must implement Iterator<Integer>. The constructor for RandomInts takes 3 integers: the lower bound min, the upper bound max, and the number of random numbers needed n. Then each call to next() in the iterator returns a random integer between min and max (inclusive). hasNext() returns true until n is reached, at which point it returns false. Note that different iterators for the same RandomInts class would generate different random integers. Use only one instance of Random so that the numbers are guaranteed to be uniformly distributed.

Problem 2 (12 points): OurLinkedList equals method

Add an equals method to the OurLinkedList class. According to Java specification, equals method takes any object. It then checks if the parameter object is of the same type as this object (see instanceof check below). If the object passes the instanceof check, it is then typecast to OurLinkedList<E>. See more on instanceof here:

The method is declared and starts as follows:

public boolean equals(Object obj) {
     //  checking that obj is an OurLinkedList
     if (!(obj instanceof OurLinkedList) ) return false;
     // typecasting obj to OurLinkedList
     OurLinkedList<E> thelist = (OurLinkedList<E>) obj;

     // the rest of your code goes here, use thelist variable:


Note that some of the elements in OurLinkedList may be null. If that's the case, the corresponding elements in both lists must be null for the two lists to be equal.

Test your method extensively, include all your test cases.

Extra credit problem (up to 10 points)

Write and test a class IterableSubstrings that implements <Iterable <String> interface. It takes a string in its constructor and generates (in a for-each loop) all its non-empty substrings (in any order). The iterator must generate substrings without duplication.
For instance, if the constructor call is IterableSubstrings("abcdef") then the iterator generates


Make sure to submit all your testing code in addition to the class itself.

Submit the java file(s) with your testing code by e-mail to me. Make sure to CC your group partner (if any).

CSci 2101 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.