Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎

2018-11-26

posted Nov 26, 2018, 6:19 AM by Konstantinovich Samuel   [ updated Nov 27, 2018, 5:30 AM ]
Goal: Go over something similar to charsequence to get you thinking


Integer Sequence:

public interface IntegerSequence{
  boolean hasNext();//does the sequence have more elements?
  int next();       //return the next value in the sequence, call this multiple times
  int length();     //total length, not current
  void reset();     //start over from the original begining of the sequence
}

A simple sequence is a range from a to b:

import java.util.NoSuchElementException;
public class Range implements IntegerSequence{

  int start,end,current;

  public Range(int _start,  int _end){
    start = _start;
    end = _end;
    current = start;
  }

  public void reset(){
    current = start;
  }

  public int length(){
    return end - start;
  }

  public boolean hasNext(){
    return current<end;
  }

  public int next(){
    if(hasNext()){
      current++;
      return current-1;
    }else{
      throw new NoSuchElementException("s="+start+",e="+end+",c="+current);
    }
  }
}

How to use this?
    IntegerSequence r = new Range(10,20);
    System.out.println("Range: "+r.hasNext());
    while(r.hasNext()){
      System.out.print(r.next()+", ");
    }
    System.out.println();

Range does not allow for non-consecutive values, however non-consecutive values are still sequences, so we need a new version of this:

import java.util.NoSuchElementException;
public class ArraySequence implements IntegerSequence{
  int current;
  int []data;

  public ArraySequence(int [] newData){
    data = new int[newData.length];
    for(int i = 0; i < data.length; i++){
      data[i]=newData[i];
    }
    current = 0;
  }

  public void reset(){
    current = 0;
  }

  public boolean hasNext(){
    return current < data.length;
  }
  public int next(){
    //return data[current++];
    if(hasNext()){
      current++;
      return data[current-1];
    }else{
      throw new NoSuchElementException();
    }
  }

  public int length(){
    return data.length;
  }
}

Usage:

    int[]nums = {1,3,5,0,-1,3,9};
    IntegerSequence as = new ArraySequence(nums);

    System.out.println("ArraySequence(array):");
    while(as1.hasNext()){
      System.out.print(as1.next()+", ");
    }



We can add a second constructor to allow initializing this with any existing IntegerSequence()
    //Postcondition: The seq will not retain its original position
  //It will be reset.
  public ArraySequence(IntegerSequence seq){
    data = new int[seq.length()];
    seq.reset();    int i = 0;
    while(seq.hasNext()){
      data[i] = seq.next();
      i++;
    }    seq.reset();
    current = 0;
  }

This would allow:

    IntegerSequence r = new Range(10,20);
    IntegerSequence as2 = new ArraySequence(r);
   
    System.out.println("ArraySequence(seq):");
    while(as2.hasNext()){
      System.out.print(as2.next()+", ");
    }
    System.out.println();

Note:
These sequences must be reset after use.



Comments