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:

Sure. The Java code describes the computation you want done, just like the math-like definition of Triangle.

Complete Program

Here is a complete program for testing this method. The value for N is hard-coded. You might wish to improve the program so that N is entered by the user.

class TriangleCalc
{
  int Triangle( int N )
  {
    if ( N == 1 )
      return 1;
    else
      return N + Triangle( N-1 );
  }
}

class TriangleTester
{
  public static void main ( String[] args)
  {
    TriangleCalc tri = new TriangleCalc();
    int result = tri.Triangle( 4 );
    System.out.println("Triangle(4) is " + result );
  }
}

Here is the output of the program:

C:/>java TriangleTester
Triangle(4) is 10

C:/>

Of course, it would be worth while at this point to copy this program to a file and run it.

QUESTION 9:

What is Triangle(500)?