Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| research:software:reflex:documentation:the_fibonacci_example [2007/07/30 01:55] – created admin | research:software:reflex:documentation:the_fibonacci_example [2007/08/24 20:53] (current) – rtoledo | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== The Fibonacci Example ====== | ||
| + | As a base for the illustrations presented later in this chapter, we will use a simple utility class able to calculate fibonacci numbers. | ||
| + | |||
| + | |||
| + | The [[http:// | ||
| + | |||
| + | <code java> | ||
| + | public class Fibonacci{ | ||
| + | |||
| + | public Fibonacci(){ } | ||
| + | |||
| + | public long get(int n){ | ||
| + | if (n == 0 || n == 1){ | ||
| + | return (long) n; | ||
| + | } | ||
| + | |||
| + | return get(n - 1) + get(n - 2); | ||
| + | } | ||
| + | |||
| + | public static void main(String[] args){ | ||
| + | if (args.length == 0){ | ||
| + | System.out.println(" | ||
| + | should be calculated." | ||
| + | System.exit(0); | ||
| + | } | ||
| + | |||
| + | int theN = Integer.parseInt(args[0]); | ||
| + | |||
| + | long theFibonacciNumber = new Fibonacci().get(theN); | ||
| + | System.out.println(" | ||
| + | |||
| + | System.out.println(); | ||
| + | |||
| + | long theFibonacciNumberPlusOne = new Fibonacci().get(theN + 1); | ||
| + | System.out.println(" | ||
| + | theFibonacciNumberPlusOne); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | This piece of code is pretty straightforward to read: there is one empty constructor to obtain Fibonacci objects references and one unique method to calculate a fibonacci number based on the parameter that represents the one we are interested on. | ||

