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

2017-12-30 Lab!

posted Nov 30, 2017, 9:12 AM by Konstantinovich Samuel   [ updated Nov 30, 2017, 12:42 PM ]

Interface recap:

Writing Interfaces:

Writing interfaces are similar to writing classes. They are their own separate file and instead of writing “public class className”, you write “public interface InterfaceName”. The methods should only call its type, name, and parameters.


public interface InterfaceName{

String method1(String param1);

//this is a method signature, just like abstract method signatures

int method2(double param2);


Interfaces only have constants and method signatures (NOT method bodies)

Technically they can contain only:


method signatures, 

default methods, 

static methods,  

nested types.  (we didn't learn this yet)

When to use Interfaces vs Inheritance?
When we talk about abstract classes we are defining characteristics of an object type, specifying what an object is. You can only extend from one base class so you want to inherit from the class which most accurately defines what you are.

In the case of an interface, we define a capability, we are talking about specifying what the object can do. You can implement as many interfaces as you need. 


Another Interface: The Iterable Interface

  • Iterable means it returns an iterator when it calls the iterator() method.

    • For example, an array is iterable, it can be iterated through.

  • Iterable has one method: Iterator() which returns an iterator for a set of elements of type T

    • This can be used, for example, in an Enhanced For loop:

for(T a : c){ //where c is the iterator, and a is of type T

//do something to a


for(Integer i : listOfStuff){
//where listOfStuff is an ArrayList for example

//do something to i


  • An iterator is basically something that has a “Next” (So while there is another thing, keep going; it allows you to go through every item in something)

  • For example, in iterator can be made for a set of numbers, let’s say between 0 and 500000000:

    • for(int i: new IntegerIteratorThing(500000000)){}

      • where IntegerIteratorThing iterates through some number up to it, or whatever else you’d like

  • It can be made for any collection of things.

  • The enhanced for loops requires a class that can provide an Iterator<T> object. Any class that implements Iterable<T>, can create an iterator<T>. So only Iterable classes can use the enhanced for loop.


public class Driver{
   public static void main(String[]args){
        RangeOfInts a = new RangeOfInts(10,20);
        RangeOfInts b = new RangeOfInts(5,11);
        RangeOfInts c = new RangeOfInts(-1,3);


        for( Integer i : c){

import java.util.*;
public class RangeOfInts implements Iterable<Integer>{
    int min,max;

    public RangeOfInts(){

    public RangeOfInts(int l, int h){
        min = l;
        max = h;

    public int getMin(){ return min;}
    public int getMax(){ return max;}

    public String toString(){
        return "["+min+","+max+"]";

    public Iterator<Integer> iterator(){
return new RangeOfIntsIterator(min,max);


public class RangeOfIntsIterator implements Iterator<Integer> {
        int current,end;

        public RangeOfIntsIterator(int start,int stop){
            current = start;
            end = stop;
        //because marge is java 7
        public void remove(){
            throw new UnsupportedOperationException();
        public Integer next(){

            return current-1;

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



SuperArray, now with more interfaces! (Iterable)

Your goal is to make your SuperArray work with the for-each loop. 
*The for-each loop only works with classes that implement the Iterable<T> interface.*

This means you need to make your SuperArray implement Iterable<T>, since your SuperArray contains String, we use Iterable<String>. Iterable<String> requires that your super array can upon request, manufacture an Iterator<String> using the iterator() method. Before we can do this, we must make an Iterator class!

The iterator object you make will implement the Iterator<T> interface. 

Note that in order for java to know what an iterator is you will need to:
import java.util.Iterator;

0. Make a new directory in your repository:
Copy your superarray into the directory first.

Do all work here, and commit regularly. Do not just update your code when you are done!

1. We will first need to create an iterator class that implements Iterator<String>. We will call this class SuperArrayIterator. 

The superArrayIterator needs to know:
-Which SuperArray is it looking at
-Which element is it up to

It needs to have: 
-A constructor to initialize the state of the iterator. 
-Iterator interface methods ( hasNext, next, and remove. See the Iterator<T> documentation for more information.) 
-The remove should be unsupported. 

To start, write this as a separate class, we can discuss inner classes after you get it to work.

2. Now work on your SuperArray!
-Make your SuperArray implement Iterable<String>
-Add the iterator() method.  //SORRY! fixed the typo

3. That is all! Test with my class here:
public class Driver{
  public static void main(String[] args) {
    SuperArray data = new SuperArray();
    int i = 0;
    while(i < 26){

    System.out.println("Standard loop:");

    for(int n = 0; n < data.size(); n++){
      System.out.print(data.get(n)+" ");
    System.out.println("for-each loop:");
    for(String s : data){
      System.out.print(s+" ");