go to previous page   go to home page   hear noise   go to next page


w is 25.0 x is 1.0

Practice Program

Our goal is to write a program that computes the following sum:

sum = 1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6

(This may look like a pointless thing to do, but you will see such sums in calculus, where they are called harmonic series.) Here is a skeleton of the program:

// Class definition for HarmonicSeries
class HarmonicSeries
  double value()
    int term=1, lastTerm = 6;
    double sum = 0.0;
    while ( term <= lastTerm )
       ; // add the next term to sum
       ; // increment term

    return sum;

// Class for testing HarmonicSeries objects
class HarmonicTester
  public static void main ( String[] args )
    HarmonicSeries series = new HarmonicSeries();

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

A few notes on the program:

  1. The class HarmonicSeries describes an object that can compute the sum we want.
  2. The method value() of that object will do the computation.
  3. A HarmonicSeries constructor exists automatically, even though the program doesn't explicitly describe one.
  4. The class HarmonicTester creates a HarmonicSeries object, then uses value() to calculate the sum, then prints it out.


Fill in the two blanks to complete the program. (This situation is very common in programming and it would be very good practice for you to think about this question before going on.)