/*
* 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 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(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) {
FiboWhile.main(null);
}
}
class FiboWhile extends Fibo {
public void FiboWhilePrint(){
int count = 0;
System.out.println();
System.out.println("WHILE LOOP:");
while (count< this.size){
System.out.println("fibonacci Sequence " + (count+1) + " = " + this.getNthSeq(count));
count++;
}
}
public void FiboForPrint(){
System.out.println();
System.out.println("FOR LOOP:");
for (int i = 1; i<=this.size; ++i) {
System.out.println("fibonacci Sequence " + (i+1) + " = " + this.getNthSeq(i));
}
}
public static void main(String args[]){
FiboWhile fib = new FiboWhile();
fib.print();
fib.FiboWhilePrint();
fib.FiboForPrint();
}
}
Fibo.main(null);
/*
* 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);
public class FiboRecursion extends Fibo {
protected void init() {
this.name = "Recursion";
// for loop calculates each fibonacci number from 0 to size and calls setData
for (int i=0; i<=size; i++){
setData(fibRec(i));
}
}
// recursive function to calculate the nth fibonacci number
static public int fibRec(int n)
{
if(n==0 || n==1) { // terminating condition of recursion
return n;
}
return (fibRec(n-1) + fibRec(n-2)); // recursive call (function calls itself)
}
// tester class method
static public void main(String[] args) {
FiboRecursion fib = new FiboRecursion();
fib.print();
}
}
FiboRecursion.main(null);
public class Fibonacci{
static void Fibo(int x){
int num1 = 0, num2 = 1;
int counter = 0;
System.out.println("While Loop: Fibonacci Sequence up to the " + x + " term:");
while (counter < x) {
System.out.print(num1 + " ");
int num3 = num2 + num1;
num1 = num2;
num2 = num3;
counter = counter + 1;
}
}
static void Fibo2(int x){
int num1 = 0, num2 = 1;
System.out.println();
System.out.println("For Loop: Fibonacci Sequence up to the " + x + " term:");
for (int i = 1; i<=x; ++i) {
System.out.print(num1 + " ");
int num3 = num2 + num1;
num1 = num2;
num2 = num3;
}
}
public static void main (String args[]) {
Scanner myObj = new Scanner(System.in);
System.out.println("Input a number:");
int x = myObj.nextInt();
System.out.println("Number imputted: " + x);
Fibo(x);
Fibo2(x);
}
}
Fibonacci.main(null);
public class Fibonacci {
static int Fibo3(int x){
if(x == 0){
return 0;
}
if(x == 1 || x == 2){
return 1;
}
return Fibo3(x-2) + Fibo3(x-1);
}
public static void main(String args[]) {
Scanner myObj = new Scanner(System.in);
System.out.println("Input a number:");
int x = myObj.nextInt();
System.out.println("Number imputted: " + x);
System.out.println("Recursion: Fibonacci Sequence up to the " + x + " term:");
for (int i = 0; i < x; i++){
System.out.print(Fibo3(i) + " ");
}
}
}
Fibonacci.main(null);
Learning Objective Notes
Skill 1.B: Determine code that would be used to complete code segments (ie For, While, Recursion)
- This skill was used in this mini lab to implement the fibonacci algorithm with for loops, while loops, and recursion
- Completed the assignment with different code segments and variations which extended the same method
Skill 4.C: Determine if two or more code segments yield equivalent results (be sure to Discuss how you know results are the same)
- From the outputs of these different implementations of the fibonacci algorithms, all code segments (stream, for loops, while loops, recursion) yield the same results
- For this example of the Fibonacci code, I knew they will yield the same results because the only variations in the code are the one method. All other attributes and methods of the classes are inherited from the Fibo class and unchanged, thus they will yield the same results. I just changed the method to add another iterative loops/recursion.
Skill 5.A: Describe the behavior of a given segment of program code (describe the difference in recursion versus for & while loops, perhaps add timing to determine speed)
- the recursion method should take the longest because it involves running the program an increasing # of times from 1-20 fibCalc() is repeatedly called (ie fibCalc(5) = fibCalc(4) + fibCalc(3) = fibCalc(3) + fibCalc(2) + fibCalc(2) + fibCalc(1)... and so on). In other words, as the number inputted into the fibonacci increases, so will the time it takes to run the code because each one will have to go further down in the sequence to eventually reach the final number.
/*
* 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);
public class FiboRec extends Fibo {
public FiboRec() {
this(20);
this.name = "RECURSIVE LOOP";
}
public FiboRec(int nth) {
super(nth);
}
private long my_fib_rec(long n){
if (n<=1){
return n;
}
return my_fib_rec(n-1) + my_fib_rec(n-2);
}
@Override
protected void init() {
this.name = "RECURSIVE LOOP";
for (int i = 0; i < size; i++) {
this.setData(my_fib_rec(i));
}
}
static public void main(String[] args) {
FiboRec fib = new FiboRec();
fib.print();
}
}
FiboRec.main(null);