Base Code

  • modified so that init is abstract, no code/runtime from init method
  • ABSTRACT, most basic form of the class, overwritten when extended
/*
 * Creator: Nighthawk Coding Society
 * Mini Lab Name: Fibonacci sequence, featuring a Stream Algorithm
 * 
*/

import java.util.ArrayList;  
import java.util.HashMap;
import java.util.stream.Stream;

/* Objective will require changing to abstract class with one or more abstract methods below */
abstract class Fibo {
    String name;  // name or title of method
    int size;  // nth sequence
    int hashID;  // counter for hashIDs in hash map
    ArrayList<Long> list;   // captures current Fibonacci sequence
    HashMap<Integer, Object> hash;  // captures each sequence leading to final result

    /*
     Zero parameter constructor uses Telescoping technique to allow setting of the required value nth
     @param: none
     */
    public Fibo() {
        this(10); // telescope to avoid code duplication, using default as 20
    }

    /*
     Construct the nth fibonacci number
     @param: nth number, the value is constrained to 92 because of overflow in a long
     */
    public Fibo(int nth) {
        this.size = nth;
        this.list = new ArrayList<>();
        this.hashID = 0;
        this.hash = new HashMap<>();
        //initialize fibonacci and time mvc
        this.init();
    }

    protected abstract void init();

    /*
     Number is added to fibonacci sequence, current state of "list" is added to hash for hashID "num"
     */
    public void setData(long num) {
        list.add(num);
        hash.put(this.hashID++, list.clone());
    }

    /*
     Custom Getter to return last element in fibonacci sequence
     */
    public long getNth() {
        return list.get(this.size - 1);
    }

    /*
     Custom Getter to return last fibonacci sequence in HashMap
     */
    public Object getNthSeq(int i) {
        return hash.get(i);
    }

    /*
     Console/Terminal supported print method
     */
    public void print() {
        System.out.println("Init method = " + this.name);
        System.out.println("fibonacci Number " + this.size + " = " + this.getNth());
        // System.out.println("fibonacci List = " + this.list);
        // System.out.println("fibonacci Hashmap = " + this.hash);
        for (int i=0 ; i<this.size; i++ ) {
            System.out.println("fibonacci Sequence " + (i+1) + " = " + this.getNthSeq(i));
        }
    }
}

Extending: For Loop

public class FiboFor extends Fibo {
    
    public FiboFor() {
        super();
    }

    public FiboFor(int nth) {
        super(nth);
    }

    protected void init() {
        super.name = "For";
        long limit = this.size;
        // for loops are likely the most common iteration structure, all the looping facts are in one line
        for (long[] f = new long[]{0, 1}; limit-- > 0; f = new long[]{f[1], f[0] + f[1]})
            this.setData(f[0]);
    }
    
    static public void main(String[] args) {
        FiboFor fib = new FiboFor();
        fib.print();
    }

}

FiboFor.main(null);
Init method = For
fibonacci Number 10 = 34
fibonacci Sequence 1 = [0]
fibonacci Sequence 2 = [0, 1]
fibonacci Sequence 3 = [0, 1, 1]
fibonacci Sequence 4 = [0, 1, 1, 2]
fibonacci Sequence 5 = [0, 1, 1, 2, 3]
fibonacci Sequence 6 = [0, 1, 1, 2, 3, 5]
fibonacci Sequence 7 = [0, 1, 1, 2, 3, 5, 8]
fibonacci Sequence 8 = [0, 1, 1, 2, 3, 5, 8, 13]
fibonacci Sequence 9 = [0, 1, 1, 2, 3, 5, 8, 13, 21]
fibonacci Sequence 10 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Extending: Recursive Loop

public class FiboRecursive extends Fibo{

    public FiboRecursive() {
        super(5);
    }

    public FiboRecursive(int nth) {
        super(nth);
    }

    public void recursion(long limit, long[] f) {
        if (limit == 0) return;
        this.setData(f[0]);
        recursion(--limit, new long[]{f[1], f[0] + f[1]});
    }

    protected void init() {
        super.name = "Recursion";
        long limit = this.size;
        long[] f = new long[]{0, 1};

        recursion(limit,f);
    }

    static public void main(String[] args) {
        FiboRecursive fib = new FiboRecursive();
        fib.print();
    }

}

FiboRecursive.main(null);
Init method = Recursion
fibonacci Number 5 = 3
fibonacci Sequence 1 = [0]
fibonacci Sequence 2 = [0, 1]
fibonacci Sequence 3 = [0, 1, 1]
fibonacci Sequence 4 = [0, 1, 1, 2]
fibonacci Sequence 5 = [0, 1, 1, 2, 3]

Extending: While Loop

public class FiboWhile extends Fibo {
    
    public FiboWhile() {
        super(20);
    }

    public FiboWhile(int nth) {
        super(nth);
    }

    protected void init() {
        super.name = "While";
        long limit = this.size;
        long[] f = new long[]{0, 1};
        while (limit-- > 0) {
            this.setData(f[0]);
            f = new long[]{f[1], f[0] + f[1]};
        }
    }
    
    static public void main(String[] args) {
        FiboWhile fib = new FiboWhile();
        fib.print();
    }

}

FiboWhile.main(null);
Init method = While
fibonacci Number 20 = 4181
fibonacci Sequence 1 = [0]
fibonacci Sequence 2 = [0, 1]
fibonacci Sequence 3 = [0, 1, 1]
fibonacci Sequence 4 = [0, 1, 1, 2]
fibonacci Sequence 5 = [0, 1, 1, 2, 3]
fibonacci Sequence 6 = [0, 1, 1, 2, 3, 5]
fibonacci Sequence 7 = [0, 1, 1, 2, 3, 5, 8]
fibonacci Sequence 8 = [0, 1, 1, 2, 3, 5, 8, 13]
fibonacci Sequence 9 = [0, 1, 1, 2, 3, 5, 8, 13, 21]
fibonacci Sequence 10 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
fibonacci Sequence 11 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
fibonacci Sequence 12 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
fibonacci Sequence 13 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
fibonacci Sequence 14 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]
fibonacci Sequence 15 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
fibonacci Sequence 16 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
fibonacci Sequence 17 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
fibonacci Sequence 18 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597]
fibonacci Sequence 19 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584]
fibonacci Sequence 20 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]

College Board Skills

  • Skill 1.B:Determine code that would be used to complete code segments (ie For, While, Recursion):
    • See table of contents for each code segment with different types of loops
  • Skill 4.C: Determine if two or more code segments yield equivalent results (be sure to Discuss how you know results are the same)
    • From the outputs, we can see that each code segment yields the same result. We can also make sure that the correct type of loop is running because of the print statements that show which function is being run, since each has a unique name associ
  • Skill 5.A: Describe the behavior of a given segment of program code (describe the difference in recursion versus for & while loops, perhaps add timing to determine speed)

    • The recursion code segment iterates by running the function called "recursion", which called the setdata method, and then re-runs itself by calling itself, and reducing limit by 1 each time, until limit = 0.
    • The while loop does the same as the recursive loop, but instead of calling itself over and over again, the loop is built into the "while" statement, which loops while (limit-- > 0), which reduces limit each time by 1.
  • Noted: Did not need to use @override since the original init method from superclass did not have any code inside. Extending and modifying init worked without using @override