/*
* 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(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();
}
/*
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);
public class FiboFor extends Fibo {
public FiboFor() {
super(10); // This parameter that is passed in the super that is used for the amount of repetitions
}
public FiboFor(int nth) {
super(nth); // take the value from the super and uses it throughout the loop
}
@Override
protected void init() {
super.name = "For Extends"; // name at the beginning
long limit = this.size; // creates a limit and setting it to ten
// 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]);
}
/*
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) {
FiboFor fib = new FiboFor();
fib.print();
}
}
FiboFor.main(null);
public class FiboFor extends Fibo {
public FiboFor() {
super(10);
}
public FiboFor(int nth) {
super(nth);
}
/*
Abstract Polymorphic "init()" method using While
*/
@Override
protected void init() {
super.name = "While";
// longer and more error prone "while" syntax is often best performer in this small scale test
long limit = super.size;
long[] f = new long[]{0, 1};
while (limit-- > 0) {
super.setData(f[0]);
f = new long[]{f[1], f[0] + f[1]};
}
}
/*
Class method "main" with purpose of testing FibWhile
*/
static public void main(String[] args) {
FiboFor fib = new FiboFor();
fib.print();
}
}
FiboFor.main(null);
public class FiboFor extends Fibo {
public FiboFor() {
super(10);
}
public FiboFor(int nth) {
super(nth);
}
/*
Abstract Polymorphic "init()" method using While
*/
@Override
protected void init() {
//setup for recursion
super.name = "Recursion";
long limit = super.size;
long[] f = new long[]{0, 1};
initRecurse(limit,f); // recursion is redirected
}
/*
Recursive methods contains an escape, in this "base condition" with a limit
VERY IMPORTANT... recursion requires pre-decrement, post-decrement will not occur until unstacking
*/
private void initRecurse(long limit, long[] f) {
if (limit == 0) return; //exit condition
super.setData(f[0]);
// observe this syntax and function, many think of recursion as another way to do iteration
initRecurse(--limit, new long[]{f[1], f[0] + f[1]});
}
/*
Class method "main" with purpose of testing FibWhile
*/
static public void main(String[] args) {
FiboFor fib = new FiboFor();
fib.print();
}
}
FiboFor.main(null);