Completed 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.
go to previous page   go to home page   hear noise   go to next page

Answer:

The completed program is given below

Completed Program

Here is the completed program:

class HarmonicSeries
{
  double value()
  {
    int term=1, lastTerm = 6;
    double sum = 0.0;
    
    while ( term <= lastTerm )
    {
      sum += 1.0/term;           // add the next term to sum
      term++ ;                   // increment term
    }

    return sum;
  } 
}

class HarmonicTester
{
  public static void main ( String[] args )
  {
    HarmonicSeries series = new HarmonicSeries();

    System.out.println("Sum of 6 terms:" + series.value() );
  }
}

You might wish to copy this program to your editor and run it. If you do, you will see something like:

C:/users>java HarmonicTester

Sum of 6 terms:2.4499999999999997

QUESTION 11:

How might this program be improved? Click here for a