DEFINICION DEL PROBLEMA
Modificar Secuencia.java añadiendo un método obtenerRSelector() que
produce una implementación diferente de la interfaz Selector que se mueve
hacia atrás de la secuencia desde el final al principio.
CODIGO JAVA
public class Sequence {
private Object[] o;
private int next = 0;
public Sequence(int size) {
o = new Object[size];
}
public void add(Object x) {
if(next < o.length) {
o[next] = x;
next++;
}
}
private class SSelector implements Selector {
int i = 0;
public boolean end() {
return i == o.length;
}
public Object current() {
return o[i];
}
public void next() {
if(i < o.length) i++;
}
} // fin de SSelector
private class RSelector implements Selector{
int i=o.length-1;
public boolean end() {
return i==-1;
}
public Object current() {
return o[i];
}
public void next() {
if (i>=0)i--;
}
}
public Selector getSelector() {
return new SSelector();
}
public RSelector getRSelector() {
return new RSelector();
}
public static void main(String args[]) {
Sequence s = new Sequence(10);
for(int i = 0; i < 10; i++)
s.add(Integer.toString(i));
Selector sl = s.getSelector();
Sequence r = new Sequence(10);
for (int i=0; i < 10; i++)
r.add(Integer.toString(i));
Selector rs = r.getRSelector();
System.out.println("Método SSlector");
while(!sl.end()) {
System.out.println((String)sl.current());
sl.next();
}//Fin de While
System.out.println("Método RSelector");
while (!rs.end()) {
System.out.println((String)rs.current());
rs.next();
}//Fin de while
}
} // fin de main
Selector.java
public interface Selector {
boolean end();
Object current();
void next();
}
No hay comentarios.:
Publicar un comentario