Fibonacci Class

/*
 * 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 */
public 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(20); // 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();
    }

    /*
     This Method should be "abstract"
     Leave method as protected, as it is only authorized to extender of the class
     Make new class that extends and defines init()
     Inside references within this class would change from this to super
     Repeat process using for, while, recursion
     */
    protected void init() {
        this.name = "Stream";
        Stream.iterate(new long[]{0, 1}, f -> new long[]{f[1], f[0] + f[1]})
            .limit(this.size)
            .forEach(f -> this.setData(f[0]) );
    }

    /*
     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));
        }
    }

    /*
    Tester class method.  If this becomes abstract you will not be able to test it directly ...
    Change this method to  call "main" class of each of the extended classes
     */
    static public void main(String[] args) {
        Fibo fib = new Fibo();
        fib.print();
    }
}
Fibo.main(null);

Fibonacci using a For Loop

import javax.swing.JOptionPane;

public class FiboFor {
    public static void main(String[] args) {
      String inpStr = JOptionPane.showInputDialog("Give the amount of Fibo numbers that you would like to print");
      int inpNum = Integer.parseInt(inpStr);
      int first = 0;
      int second = 1;
  
      System.out.println("Here is the Fibonacci Sequence for " + inpNum + " terms:");

      for (int i = 1; i <= inpNum; i++) {
        System.out.print(first + " ");
        int nextNum = first + second;
        first = second;
        second = nextNum;
      }
    }
}

FiboFor.main(null);
Here is the Fibonacci Sequence for 10 terms:
0 1 1 2 3 5 8 13 21 34 

Fibonacci using While Loop

import javax.swing.JOptionPane;

public class FiboWhile {
    public static void main(String[] args) {
      int start = 0;  
      String inpStr = JOptionPane.showInputDialog("Give the amount of Fibo numbers that you would like to print");
      int inpNum = Integer.parseInt(inpStr);
      int first = 0;
      int second = 1;

      System.out.println("Here is the Fibonacci Sequence for " + inpNum + " terms:");
  
      while (start < inpNum) {
        System.out.print(first + " ");
        int nextNum = first + second;
        first = second;
        second = nextNum;
        start++;
      }
    }
}

FiboWhile.main(null);
Here is the Fibonacci Sequence for 10 terms:
0 1 1 2 3 5 8 13 21 34 

Fibonacci Recursion Loop

import javax.swing.JOptionPane;

public class FiboRec{
    public static int Recursion(int n){
        if(n == 0){
            return 0;
        }
        if(n == 1 || n == 2){
            return 1; 
        }
        return Recursion(n-2) + Recursion(n-1);
    }

    public static void main(String args[]) {
        String num = JOptionPane.showInputDialog("Give the amount of Fibo numbers that you would like to print");
        int end = Integer.parseInt(num);
        System.out.println("Here is the Fibonacci Sequence for " + num + " terms:");
        for(int i = 0; i < end; i++){
            System.out.print(Recursion(i) +" ");
        }
    }
}

FiboRec.main(null);
Here is the Fibonacci Sequence for 10 terms:
0 1 1 2 3 5 8 13 21 34 

Fibonacci Abstraction

abstract class Fibonacci {
    abstract void run(); 

    private int first;
    private int second;

    public void setFirst(int num) {
        first = num;
    }

    public int getFirst() {
        return first;
    }

    public void setSecond(int num) {
        second = num;
    }

    public int getSecond() {
        return second;
    }

}

class Fibo extends Fibonacci {

    void run() {
        String inpStr = JOptionPane.showInputDialog("Give the amount of Fibo numbers that you would like to print");
        int inpNum = Integer.parseInt(inpStr);
        setFirst(0);
        setSecond(1);
    
        System.out.println("Here is the Fibonacci Sequence for " + inpNum + " terms:");

        for (int i = 1; i <= inpNum; i++) {
            System.out.print(getFirst() + " ");
            int nextNum = getFirst() + getSecond();
            setFirst(getSecond());
            setSecond(nextNum);
        }
    }
    public static void main(String args[]) {
        Fibonacci obj = new Fibo();
        obj.run();
    }
}

Fibo.main(null);
Here is the Fibonacci Sequence for 10 terms:
0 1 1 2 3 5 8 13 21 34 
The Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details.

Hacks Questions

Skill 1.B: Used different types of loops or methods of program for each situation

Skill 4.C: Code segments produce same results because the output is printed in the same format, the only thing changing is the type of loop. Key Finding: Different Loops can produce same output as long as the loops have the same purpose- in this case with for and while loops

Skill 5.A: Recursion runs the fastest since it's not a loop, but all methods work equally well. It's important to know how to recreate a program in different ways.