Fibonacci Notebook
/*
* 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(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<>();
//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);
While Loop
public class WhileLoop extends Fibo {
public WhileLoop() {
this(8); //iterates 8 times
}
public WhileLoop(int nth) { //makes arrays, establishes nth term
this.size = nth;
this.list = new ArrayList<>();
this.hashID = 0;
this.hash = new HashMap<>();
this.init();
}
protected void init() { //while loop making
this.name = "While";
int num = 0;
long[] w = new long[]{0, 1};
while(this.size >= num) { // while it has iterated less than or equal to 8 times, it iterates again
this.setData(w[0]);
w = new long[]{w[1], w[0] + w[1]}; // add terms to array
num++;
}
}
static public void main(String[] args) { //prints it
WhileLoop whileprint = new WhileLoop();
whileprint.print();
}
}
WhileLoop.main(null)
Recursion
public class Recursion extends Fibo {
public Recursion() { //iterates 8 times
this(8);
}
public Recursion(int nth) { //makes arrays, establishes nth term
this.size = nth;
this.list = new ArrayList<>();
this.hashID = 0;
this.hash = new HashMap<>();
this.init();
}
protected void recur(long[] r, int i) { //starts at first term and adds a new term, keeps doing this over and over again if it iterated less than 8 times
this.setData(r[0]);
r = new long[]{r[1], r[0] + r[1]};
i++;
if (i <= this.size) {
this.recur(r, i);
}
}
protected void init() { // starts at 0, repeats
this.name = "Recursion";
int i = 0;
long[] r = new long[] {0,1};
this.recur(r, i);
}
static public void main(String[] args) { //prints
Recursion recursionprint = new Recursion();
recursionprint.print();
}
}
Recursion.main(null)
For Loop
public class ForLoop extends Fibo{
public ForLoop() { //iterates 8 times
this(8);
}
public ForLoop(int nth) { //makes arrays, establishes nth term
this.size = nth;
this.list = new ArrayList<>();
this.hashID = 0;
this.hash = new HashMap<>();
this.init();
}
protected void init() {
this.name = "For Loop";
long[] f = new long[]{0, 1};
for(int num = 0; num <= this.size; num++) { //uses for loop to iterate up to 8 times, adding a term each time
this.setData(f[0]);
f = new long[]{f[1], f[0] + f[1]};
}
}
static public void main(String[] args) { //prints
ForLoop forprint = new ForLoop();
forprint.print();
}
}
ForLoop.main(null)
1B
- The loops that we learned in these can all be used in our CB frqs. The while loop, for loop, and recursion are all useful 4C
- All the types of iteration all have the same output. They are all doing the same thing just in a different way, which could change how long each one takes, but not the output. 5A
- Recursion uses if-else, while loops use while, and for loops use for.