Week of 9/20
Fibo, Java, and Project Design
- Initial Code with Fibo Class
- Fibo in a For Loop Format
- Fibonacci in a While Loop Format
- Fibonacci Series using Recursion
- Project Idea
- Meet our team!
/*
* 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);
//Using For Loop
public class FibonacciFor extends Fibo {
public static void main(String[] args)
{
// Set it to the number of elements you want in the Fibonacci Series
int tries = 10;
int num1 = 0;
int num2 = 1;
System.out.print("Fibonacci Series: ");
for (int i = 1; i <= tries; i++) //looping through the numbers until i <= to 10
{
System.out.print(num1+" ");
/* On each iteration, we are assigning second number
* to the first number and assigning the sum of last two
* numbers to the second number
*/
int total = num1 + num2; //Actual Fibo program, adding last and first number together
num1 = num2;
num2 = total;
}
Fibo fib = new FibonacciFor(); //Printing code from initial fibo
fib.print();
}
}
FibonacciFor.main(null);
//Using While Loop
public class FibonacciWhile extends Fibo {
public static void main(String[] args)
{
int tries = 10;
int num1 = 0;
int num2 = 1;
System.out.print("Fibonacci Series: "); //Initial line, basically saying number of numbers
int i=1;
while(i <= tries)
{
System.out.print(num1+" ");
int total = num1 + num2;
num1 = num2;
num2 = total;
i++;
}
Fibo fib = new FibonacciWhile();
fib.print();
}
}
FibonacciWhile.main(null);
//Using Recursion
public class FibonacciRecursion extends Fibo {
public static int fibonacciRecursion(int a){
if(a == 0){
return 0;
}
if(a == 1 ){
return 1;
}
if( a == 2){
return 1;
}
return fibonacciRecursion(a-2) + fibonacciRecursion(a-1);
}
public static void main(String args[]) {
int tries = 10;
System.out.print("Fibonacci Series of "+tries+" numbers: ");
for(int i = 0; i < tries; i++){
System.out.print(fibonacciRecursion(i) +" ");
}
Fibo fib = new FibonacciRecursion();
fib.print();
}
}
FibonacciRecursion.main(null);
// Java program to print greeting
class greet {
public static void main(String[] args)
{
System.out.println("Welcome to Health.ly!"); // line that we are printing
}
}
greet.main(null); // calling the class to use
// Array with list of members and positions
String[][] positions = {{"Saumya", "Scrum Leader"},{"Serafina", "Deployment"}, {"Armaan", "Frontend"}, {"Prisha", "Backend"}};
// Printing out the array values side by side
System.out.println(Arrays.deepToString(positions));