Complete Program

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:

Not a good idea. This would prevent the system from crashing, but would hide some other serious problem with the input data.

Complete Program

Here is a complete program that uses the factorial() method:

//FactorialTester.java
//
class FactorialCalc
{
  int factorial( int N )
  {
    if ( N == 0 )
      return 1;
    else
      return N * factorial( N-1 ) ;
  }
}

class FactorialTester
{
  public static void main ( String[] args)
  {
    int argument = 10;
    FactorialCalc f = new FactorialCalc();
    int result = f.factorial( argument );
    System.out.println("Factorial(" + argument + ") is " + result );
  }
}

Here is a run of the program:

C:/JavaNotes/Recursion03>javac FactorialTester.java

C:/JavaNotes/Recursion03>java FactorialTester
Factorial(10) is 3628800

QUESTION 6:

Here is another run of the program with a new value for the argument:

C:/JavaNotes/Recursion03>java FactorialTester
Factorial(17) is -288522240

That answer does not look correct. What went wrong?