/*
* 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);
import java.util.ArrayList;
import java.util.HashMap;
import java.util.stream.Stream;
public class Fibo {
String name;
int size;
int hashID;
ArrayList<Long> list;
HashMap<Integer, Object> hash;
long executionTime;
public Fibo() {
this(20);
}
public Fibo(int nth) {
this.size = nth;
this.list = new ArrayList<>();
this.hashID = 0;
this.hash = new HashMap<>();
this.init();
}
protected void init() {
final long startTime = System.nanoTime();
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]) );
this.executionTime = System.nanoTime() - startTime;
}
public void setData(long num) {
list.add(num);
hash.put(this.hashID++, list.clone());
}
public long getNth() {
return list.get(this.size - 1);
}
public Object getNthSeq(int i) {
return hash.get(i);
}
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));
}
System.out.println("Init method runtime = " + this.executionTime);
}
static class FiboFor extends Fibo {
public FiboFor() {
super(20);
}
public FiboFor(int n) {
super(n);
}
@Override
protected void init() {
final long startTime = System.nanoTime();
this.name = "For Loop Demonstration";
long[] numbers = {0, 1};
for (int i = 0; i < this.size; i++) {
this.setData(numbers[0]);
numbers = new long[] {numbers[1], numbers[0] + numbers[1]};
}
this.executionTime = System.nanoTime() - startTime;
}
}
static public void main(String[] args) {
FiboFor test1 = new FiboFor();
test1.print();
}
}
Fibo.main(null);
import java.util.ArrayList;
import java.util.HashMap;
import java.util.stream.Stream;
public class Fibo {
String name;
int size;
int hashID;
ArrayList<Long> list;
HashMap<Integer, Object> hash;
long executionTime;
public Fibo() {
this(20);
}
public Fibo(int nth) {
this.size = nth;
this.list = new ArrayList<>();
this.hashID = 0;
this.hash = new HashMap<>();
this.init();
}
protected void init() {
final long startTime = System.nanoTime();
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]) );
this.executionTime = System.nanoTime() - startTime;
}
public void setData(long num) {
list.add(num);
hash.put(this.hashID++, list.clone());
}
public long getNth() {
return list.get(this.size - 1);
}
public Object getNthSeq(int i) {
return hash.get(i);
}
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));
}
System.out.println("Init method runtime = " + this.executionTime);
}
static class FiboWhile extends Fibo {
public FiboWhile() {
super(20);
}
public FiboWhile(int n) {
super(n);
}
@Override
protected void init() {
final long startTime = System.nanoTime();
this.name = "While Loop Demonstration";
long[] numbers = {0, 1};
int i = 0;
while (i < this.size) {
this.setData(numbers[0]);
numbers = new long[] {numbers[1], numbers[0] + numbers[1]};
i++;
}
this.executionTime = System.nanoTime() - startTime;
}
}
static public void main(String[] args) {
FiboWhile test2 = new FiboWhile();
test2.print();
}
}
Fibo.main(null);
import java.util.ArrayList;
import java.util.HashMap;
import java.util.stream.Stream;
public class Fibo {
String name;
int size;
int hashID;
ArrayList<Long> list;
HashMap<Integer, Object> hash;
long executionTime;
public Fibo() {
this(20);
}
public Fibo(int nth) {
this.size = nth;
this.list = new ArrayList<>();
this.hashID = 0;
this.hash = new HashMap<>();
this.init();
}
protected void init() {
final long startTime = System.nanoTime();
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]) );
this.executionTime = System.nanoTime() - startTime;
}
public void setData(long num) {
list.add(num);
hash.put(this.hashID++, list.clone());
}
public long getNth() {
return list.get(this.size - 1);
}
public Object getNthSeq(int i) {
return hash.get(i);
}
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));
}
System.out.println("Init method runtime = " + this.executionTime);
}
static class FiboRecursion extends Fibo {
public FiboRecursion() {
super(20);
}
public FiboRecursion(int n) {
super(n);
}
@Override
protected void init() {
final long startTime = System.nanoTime();
this.name = "Recursion Method Demonstration";
int counter = 0;
long[] numbers = {0, 1};
increment(numbers, counter);
this.executionTime = System.nanoTime() - startTime;
}
public void increment(long[] numbers, int counter) {
this.setData(numbers[0]);
if (counter < this.size - 1) {
numbers = new long[] {numbers[1], numbers[0] + numbers[1]};
increment(numbers, counter + 1);
}
}
}
static public void main(String[] args) {
FiboRecursion test3 = new FiboRecursion();
test3.print();
}
}
Fibo.main(null);
Hacks
Skill 1.B: differnet types of loops and methods of program were used for each situation. A for loop, while loop, and recursion were used to show different methods of achieving the same fibonacci calculations.
Skill 4.C: The for loop, while loop and recursion methods all yield the same results because they are printed in the same format. Different types of loops don't necssarily change the oput if implmented in the same way with the same purpose. This is the case with this method, their ouputs are the same.
Skill 5.A: Recursion runs faster than for and while loops since it is not a loop and doesn't have to repeat code. But all the methods work equally well and the differnece in time for these fibonacci calculations are not noticable.