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 ?
There are plenty wrong with the design of this assignment.
Use
enumThe test code contains this snippets:
In one place the type is
MySimpleIt, in another it’sMySimpleIterator. Either way, the name suggests using an interface to define a bunch of constants. THIS IS NOT A PROPER USE OF aninterface!!!It would be a much better design to use an
enuminstead:See: Effective Java 2nd Edition Item 30: Use enums instead of
intconstants.Consider multiple implementations of the
interfaceinstead of a monolith withsetTypeIt 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
AbstractIntegerSequencerthat defines the basic functionality (resetting bounds, answeringhasNext(),iterator(), etc), that delegates to anabstract 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 SequenceTypefor astaticfactory 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 itimplements Iterator<Integer>.From JLS 4.8 Raw Types (emphasis theirs):
See also Effective Java 2nd Edition Item 32: Don’t use raw type in new code.
Don’t confuse
Iterator<T>withIterable<T>.Let’s say you have something like this:
If you make
seq implements Iterable<Integer>, Iterator<Integer>and@Override Iterator<Integer> iterator()toreturn this;, then the second loop wouldn’t print anything, sinceseqis its owniterator(), and at that point there is no morehasNext()forseq.A proper implementation of
Iterable<Integer>should be able to generate as many independentIterator<Integer>as necessary for the user, and such implementation would again print prime numbers between0and10in the above code.Further readings on stackoverflow
PrimeGenerator implements Iterator<Integer>(among other things)