-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvenFibonacciSum.java
57 lines (52 loc) · 1.66 KB
/
EvenFibonacciSum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.*;
public class EvenFibonacciSum
{
// instance variables - replace the example below with your own
private int x;
/**
* Constructor for objects of class EvenFibonacciSum
*/
public EvenFibonacciSum()
{
clear();
ArrayList fibonacciSequence = fibonacciGenerator();
int currentSum = 0;
for(int i=0; i<fibonacciSequence.size(); i++) {
int currentVal = (Integer) fibonacciSequence.get(i);
if(currentVal % 2 == 0) { //is even
currentSum = currentSum + currentVal;
}
System.out.print(fibonacciSequence.get(i) + ", ");
}
System.out.println("\nThe Sum Of Even Values Is: " + currentSum);
}
public ArrayList fibonacciGenerator() {
ArrayList returnArray = new ArrayList();
returnArray.add(1);
returnArray.add(2);
int currentSum = 0;
int i = 2;
while (currentSum < 4000000) {
int previousTwo = (Integer) (returnArray.get(i-2));
int previousOne = (Integer) (returnArray.get(i-1));
currentSum = previousTwo + previousOne;
returnArray.add(currentSum);
i++;
}
return returnArray;
}
public void printArray(int[] array) {
System.out.println("Here is Your Array!");
for (int i = 0; i<array.length; i++) {
boolean lastTurn = (i==array.length-1);
if (lastTurn) {
System.out.println(array[i]);
} else {
System.out.print(array[i] + ", ");
}
}
}
public void clear() {
System.out.println("\f");
}
}