Complete Fib

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

The complete method is seen below.

Complete Fib

public int fib( int n )
{
  if ( n == 1 ) 
    return 1;
    
  else if  ( n == 2 ) 
    return 1;
    
  else
    return fib( n-1 ) + fib( n-2 );
}

It may worry you that the code for fib(int n) uses fib(n-1) and fib(n-2). This is fine. Look at the math-like definition. Play with it until you understand it. The program says the same thing but uses a different language (Java) to say it.

It is the job of the Java system to make sure that you get the computation that you ask for.

QUESTION 18:

If you are uneasy about that explanation, what can you do?