views:

145

answers:

1

To be honest I'm not quite sure if I understand the task myself :) I was told to create class MySimpleIt, that implements Iterator and Iterable and will allow to run the provided test code. Arguments and variables of objects cannot be either Collections or arrays.
The code :

 MySimpleIt msi=new MySimple(10,100,
                           MySimpleIt.PRIME_NUMBERS);

 for(int el: msi)
   System.out.print(el+" ");    
 System.out.println();

 msi.setType(MySimpleIterator.ODD_NUMBERS);
 msi.setLimits(15,30);
 for(int el: msi)
   System.out.print(el+" ");    
 System.out.println();

 msi.setType(MySimpleIterator.EVEN_NUMBERS);
 for(int el: msi)
   System.out.print(el+" ");    
 System.out.println();

The result I should obtain :

11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 


15 17 19 21 23 25 27 29 


16 18 20 22 24 26 28 30

And here's my code :

import java.util.Iterator; 
interface MySimpleIterator{
    static int ODD_NUMBERS=0;
    static int EVEN_NUMBERS = 1;
    static int PRIME_NUMBERS = 2;

    int setType(int i);
}

public class MySimpleIt implements Iterable, Iterator, MySimpleIterator {    
    public MySimple my;

    public MySimpleIt(MySimple m){ 
        my = m;      
    }

    public int setType(int i){
        my.numbers = i;
        return my.numbers;
    }

    public void setLimits(int d, int u){
        my.down = d;
        my.up = u;
    }

    public Iterator iterator(){
        Iterator it = this.iterator();
        return it;
    }

    public void remove(){  
    }

    public Object next(){
        Object o = new Object();
        return o;
    }

    public boolean hasNext(){
        return true;
    }

}

class MySimple {
    public int down;
    public int up;
    public int numbers;

    public MySimple(int d, int u, int n){
        down = d;
        up = u;
        numbers = n;
    }
}

In the test code I have error in line when creating MySimpleIt msi object, as it finds MySimple instead of MySimpleIt. Also I have errors in for-each loops, because compiler wants 'ints' there instead of Object. Anyone has any idea on how to solve it ?

+5  A: 

There are plenty wrong with the design of this assignment.

Use enum

The test code contains this snippets:

MySimpleIt(erator?).PRIME_NUMBERS
MySimpleIt(erator?).ODD_NUMBERS
MySimpleIt(erator?).EVEN_NUMBERS

In one place the type is MySimpleIt, in another it's MySimpleIterator. Either way, the name suggests using an interface to define a bunch of constants. THIS IS NOT A PROPER USE OF an interface!!!

It would be a much better design to use an enum instead:

enum SequenceType {
  PRIME_NUMBERS, ODD_NUMBERS, EVEN_NUMBERS;
}

See: Effective Java 2nd Edition Item 30: Use enums instead of int constants.


Consider multiple implementations of the interface instead of a monolith with setType

It looks like your sequencer should be able to switch sequence type on a whim. This will result in that class being a huge blob that must know how to generate every type of sequences. It may work okay just for the 3 types given here, but it's definitely a poor design if you later want to add more types of sequences.

Consider just having different implementations of the same interface for the different types of sequences. You may want to define an AbstractIntegerSequencer that defines the basic functionality (resetting bounds, answering hasNext(), iterator(), etc), that delegates to an abstract protected int generateNext() for subclasses to @Override. This way, the specifics of the type of sequence to generate is nicely encapsulated to each subclass.

You can still keep enum SequenceType for a static factory method that instantiates these different subclasses, one for each sequence type, but those sequences themselves probably shouldn't be able to switch type on a whim.


Use generics

Instead of making your type implements Iterator, you should make it implements Iterator<Integer>.

From JLS 4.8 Raw Types (emphasis theirs):

The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.

See also Effective Java 2nd Edition Item 32: Don't use raw type in new code.


Don't confuse Iterator<T> with Iterable<T>.

Let's say you have something like this:

IntegerSequencer seq = new PrimeSequencer(0, 10);

for (int i : seq) {
  System.out.println(i);
} // prints "2", "3", "5", 7"

for (int i : seq) {
  System.out.println(i);
} // what should it print???

If you make seq implements Iterable<Integer>, Iterator<Integer> and @Override Iterator<Integer> iterator() to return this;, then the second loop wouldn't print anything, since seq is its own iterator(), and at that point there is no more hasNext() for seq.

A proper implementation of Iterable<Integer> should be able to generate as many independent Iterator<Integer> as necessary for the user, and such implementation would again print prime numbers between 0 and 10 in the above code.


Further readings on stackoverflow

polygenelubricants