Last active
February 25, 2016 00:49
-
-
Save jrdalpra/bff99568c5e6b4300a72 to your computer and use it in GitHub Desktop.
OO Fibonacci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SuppressWarnings("serial") | |
public class Fibonacci extends Number { | |
private final Integer number; | |
public Fibonacci(Integer number) { | |
super(); | |
this.number = number; | |
} | |
@Override | |
public String toString() { | |
return Integer.toString(intValue()); | |
} | |
@Override | |
public double doubleValue() { | |
return intValue(); | |
} | |
@Override | |
public float floatValue() { | |
return intValue(); | |
} | |
@Override | |
public int intValue() { | |
if (number == 1 || number == 2) | |
return 1; | |
return new Fibonacci(number - 1).intValue() + new Fibonacci(number - 2).intValue(); | |
} | |
@Override | |
public long longValue() { | |
return intValue(); | |
} | |
public static void main(String[] args) { | |
for (int j = 1; j <= 12; j++) { | |
System.out.print(new Fibonacci(j) + " "); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment