... timeData = new TimeData();
59
60         System.out.printf("  Calculating fibonacci(%d)%n",  n);
61         timeData.start = Instant.now();
62         long fibonacciValue = fibonacci(n);
63         timeData.end = Instant.now();
64         displayResult(n, fibonacciValue, timeData);
65         return timeData;
66      }
67
68      // recursive method fibonacci; calculates nth Fibonacci number
69      private static long fibonacci(long n) {
70         if (n == 0 || n == 1) {
71            return n;
72         }
73         else {
74            return fibonacci(n - 1) + fibonacci(n - 2);
75         }
76      }
77
78      // display fibonacci calculation result and total calculation time
79      private static void displayResult(
80         int n, long value, TimeData timeData) {
81
82         System.out.printf("  fibonacci(%d) = %d%n",  n, value);
83         System.out.printf(
84            " Calculation time for fibonacci(%d) ...

Get Java How To Program, Late Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.