Printing out the values of Fibonacci Sequence using different methods!
Learning how to iterate code in different ways, example of a Fibonacci Sequence used
/*
* 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(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
*/
abstract protected void init();
/*
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);
abstract class fibonacci { //defining the class
public void printValues (int count){ //function which prints the values
int[] numbers = getfib(count); //getting values and storing into Array
for(int i =0; i<numbers.length; i++){ //for loop to reiterate code
System.out.println(numbers[i]); //prints out values in each index of array
}
}
abstract int[] getfib(int count);
}
public int fibo (int count) {
if(count == 0)
return 0;
else if(count == 1)
return 1;
else
return fibo(count - 1) + fibo(count - 2);
}
class fibonacciFor extends fibonacci{ //making a class extending the main abstract class to do the for loop
int[] getfib(int count) { //array to store values
int[] numbers = new int[count+1];
int fib1 = 0, fib2 = 1; //defining the variables which will be assigned fibo values
for (int i = 1; i <= count; ++i) { //for loop which iterates
// compute the next term
int fibNext = fib1 + fib2;
fib1 = fib2;
fib2 = fibNext;
numbers[i] = fibNext; //added value added into array
}
return numbers; //array returned
}
public static void main(){
fibonacciFor myFibonacciFor = new fibonacciFor(); //function printed in main
myFibonacciFor.printValues(10);
}
}
fibonacciFor.main(); //the method is ran
class fibonacciWhile extends fibonacci { //defining a class
int index = 0; //defining an index to iterate code
int[] getfib(int count) { //passing count to array get fib
int[] numbers = new int[count]; //defining a new array numbers with input count
int fib1 = 0, fib2 = 1;
while(index < count) { //makes the index run for what count equals
// compute the next term
int fibNext = fib1 + fib2; //passing values
fib1 = fib2;
fib2 = fibNext;
numbers[index] = fibNext;
index++; //iterating loop
}
return numbers; //returning array
}
public static void main(){
fibonacciWhile myfibonacciWhile = new fibonacciWhile(); //printing out values in main
myfibonacciWhile.printValues(10);
}
}
fibonacciWhile.main(); //running code
Skill sets matched
-Skill 1B was matched, as different types of iterations such as recursions, while loops and for loops were used -Skill 4B was matched, as the outputs for the different iterations was the same and everything matches -Skill 5C was matchesd as comments in code help reader understand what each segment is supposed to do