... 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) = %.3f seconds%n",
85             n, timeData.timeInSeconds());
86       }
87
88 ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.