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

Answer:

The completed program is given below.

Complete Program

Here is the complete program:

import java.util.Scanner ;
class HarmonicSeries
{
  double value( int limit )
  {
    int term=1 ;
    double sum = 0.0;
    
    while ( term <= limit )
    {
      sum += 1.0/term;           // add the next term to sum
      term++ ;                   // increment term
    }

    return sum;
  } 
}

class HarmonicTester
{
  public static void main ( String[] args ) 
  {
    Scanner scan = new Scanner(System.in);
    System.out.print("How many Terms? ");
    int limit = scan.nextInt();
    HarmonicSeries series = new HarmonicSeries();

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

  }
}

QUESTION 13:

With my 750 MHz AMD Athlon computer it takes 22 seconds to run the program with the limit set at 1 000 000 000. Is your computer slower or faster than mine?