Abstract Fibonaccii Hack
A Fibonacci algorithm that runs using an abstract parent 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 */
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(8); // 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<>();
//calculate fibonacci and time mvc
this.calc();
}
/*
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 calc()
Inside references within this class would change from this to super
Repeat process using for, while, recursion
*/
protected abstract void calc();
/*
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("Calculation 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));
}
}
}
public class FiboRecur extends Fibo {
public FiboRecur() {
super();
}
public FiboRecur(int nth) {
super(nth);
}
@Override
protected void calc() {
super.name = "FiboRecur extends Fibo";
long limit = this.size;
this.calc(limit,new long[]{0, 1});
}
protected void calc(long n,long[] f){
if(!(n-- > 0)){return;};
f = new long[]{f[1], f[0] + f[1]};
this.setData(f[0]);
calc(n,f);
}
/*
Tester class method.
*/
static public void main(int... numbers) {
for (int nth : numbers) {
Fibo fib = new FiboRecur(nth);
fib.print();
System.out.println();
}
}
}
FiboRecur.main(2, 5, 8);
Calculation method = FiboRecur extends Fibo
fibonacci Number 2 = 1
fibonacci List = [1, 1]
fibonacci Hashmap = {0=[1], 1=[1, 1]}
fibonacci Sequence 1 = [1]
fibonacci Sequence 2 = [1, 1]
Calculation method = FiboRecur extends Fibo
fibonacci Number 5 = 5
fibonacci List = [1, 1, 2, 3, 5]
fibonacci Hashmap = {0=[1], 1=[1, 1], 2=[1, 1, 2], 3=[1, 1, 2, 3], 4=[1, 1, 2, 3, 5]}
fibonacci Sequence 1 = [1]
fibonacci Sequence 2 = [1, 1]
fibonacci Sequence 3 = [1, 1, 2]
fibonacci Sequence 4 = [1, 1, 2, 3]
fibonacci Sequence 5 = [1, 1, 2, 3, 5]
Calculation method = FiboRecur extends Fibo
fibonacci Number 8 = 21
fibonacci List = [1, 1, 2, 3, 5, 8, 13, 21]
fibonacci Hashmap = {0=[1], 1=[1, 1], 2=[1, 1, 2], 3=[1, 1, 2, 3], 4=[1, 1, 2, 3, 5], 5=[1, 1, 2, 3, 5, 8], 6=[1, 1, 2, 3, 5, 8, 13], 7=[1, 1, 2, 3, 5, 8, 13, 21]}
fibonacci Sequence 1 = [1]
fibonacci Sequence 2 = [1, 1]
fibonacci Sequence 3 = [1, 1, 2]
fibonacci Sequence 4 = [1, 1, 2, 3]
fibonacci Sequence 5 = [1, 1, 2, 3, 5]
fibonacci Sequence 6 = [1, 1, 2, 3, 5, 8]
fibonacci Sequence 7 = [1, 1, 2, 3, 5, 8, 13]
fibonacci Sequence 8 = [1, 1, 2, 3, 5, 8, 13, 21]
public class FiboWhile extends Fibo {
public FiboWhile() {
super();
}
public FiboWhile(int nth) {
super(nth);
}
@Override
protected void calc() {
super.name = "FiboWhile extends Fibo";
long limit = this.size;
// for loops are likely the most common iteration structure, all the looping facts are in one line
long[] f = new long[]{0, 1};
while(limit-- > 0){
f = new long[]{f[1], f[0] + f[1]};
this.setData(f[0]);
}
}
/*
Tester class method.
*/
static public void main(int... numbers) {
for (int nth : numbers) {
Fibo fib = new FiboWhile(nth);
fib.print();
System.out.println();
}
}
}
FiboWhile.main(2, 5, 8);
Calculation method = FiboWhile extends Fibo
fibonacci Number 2 = 1
fibonacci List = [1, 1]
fibonacci Hashmap = {0=[1], 1=[1, 1]}
fibonacci Sequence 1 = [1]
fibonacci Sequence 2 = [1, 1]
Calculation method = FiboWhile extends Fibo
fibonacci Number 5 = 5
fibonacci List = [1, 1, 2, 3, 5]
fibonacci Hashmap = {0=[1], 1=[1, 1], 2=[1, 1, 2], 3=[1, 1, 2, 3], 4=[1, 1, 2, 3, 5]}
fibonacci Sequence 1 = [1]
fibonacci Sequence 2 = [1, 1]
fibonacci Sequence 3 = [1, 1, 2]
fibonacci Sequence 4 = [1, 1, 2, 3]
fibonacci Sequence 5 = [1, 1, 2, 3, 5]
Calculation method = FiboWhile extends Fibo
fibonacci Number 8 = 21
fibonacci List = [1, 1, 2, 3, 5, 8, 13, 21]
fibonacci Hashmap = {0=[1], 1=[1, 1], 2=[1, 1, 2], 3=[1, 1, 2, 3], 4=[1, 1, 2, 3, 5], 5=[1, 1, 2, 3, 5, 8], 6=[1, 1, 2, 3, 5, 8, 13], 7=[1, 1, 2, 3, 5, 8, 13, 21]}
fibonacci Sequence 1 = [1]
fibonacci Sequence 2 = [1, 1]
fibonacci Sequence 3 = [1, 1, 2]
fibonacci Sequence 4 = [1, 1, 2, 3]
fibonacci Sequence 5 = [1, 1, 2, 3, 5]
fibonacci Sequence 6 = [1, 1, 2, 3, 5, 8]
fibonacci Sequence 7 = [1, 1, 2, 3, 5, 8, 13]
fibonacci Sequence 8 = [1, 1, 2, 3, 5, 8, 13, 21]
Popcorn Hacks
Objectives of these hacks are …
- Understand how to fullfill abstract class requirements using two additional algoritms.
- Use inheritance style of programming to test speed of each algorithm. To test the speed, a.) be aware that the first run is always the slowest b.) to time something, my recommendation is 12 runs on the timed element, through out highest and lowest time in calculations.
- Be sure to make a tester and reporting methods.
.85 basis for text based comparison inside of Jupyter Notebook lesson
Hacks
Assign in each Team to build a Thymeleaf UI for portfolio_2025 using this example https://thymeleaf.nighthawkcodingsociety.com/mvc/fibonacci as basis. Encorporate into Algorithms menu.
Since there are three teams, one team can do Fibo, others Pali and Factorial. Assign this to people that are struggling for contribution and presentation to checkpoints.
.90 basis for FE presentation in Thymmeleaf to BE call in Spring