CMPU 101 - Lab 4

$Revision: 1.4 $

In this lab you will use loops to solve several computational problems.

Getting Started

Open a web browser.  From the class web page,, go to the "Labs" section at the bottom and download the file  Start DrJava and open

Within the Loops class, you will implement several static methods according to the instructions below.

In the initial version of the Loops class, the methods that return a value have a "dummy" return statement.  As you implement each method, remove the dummy return statements.  (They are only present so that the Loops class will compile.)

Methods to implement

You will implement the following methods.


The printIntSequence method takes two int parameters, min and max.  It should print each integer between min and max, inclusive. Each consecutive pair of integers should be separated by a comma and a space.  For example, the call Loops.printIntSequence(5, 10) should print

5, 6, 7, 8, 9, 10

The entire sequence should be printed on a single line.  Therefore, you should use System.out.print rather than System.out.println for printing the int values and the ", " separators.  (System.out.println causes a new line to be started after printing the value.)

After printing the entire sequence, call System.out.println() to end the line.


The computeFactorial method takes a single int parameter n.  It should return the int value that results from computing the factorial of n.  The factorial of a number n is written n!, and is defined as:

1 * 2 * ... * n-1 * n

For example, the call Loops.computeFactorial(6) should return the value 720.

Note that the 0! (the factorial of the number 0) is defined as 1.  So, Loops.computeFactorial(0) should evaluate as 1.


The mathematical constant e is the sum of the infinite series:

e = 1/0! + 1/1! + 1/2! + 1/3! + ...

The estimateE method takes an int parameter numTerms and computes the sum of the first numTerms terms of this series as a double value.  For example, here are some examples of calling this method from the DrJava interactions window:

Welcome to DrJava.
> Loops.estimateE(0)
> Loops.estimateE(1)
> Loops.estimateE(2)
> Loops.estimateE(3)
> Loops.estimateE(4)
> Loops.estimateE(5)
> Loops.estimateE(6)

As more terms are added to the sum, the result approaches the value of e, which is approximately 2.71828.

In your implementation of estimateE, call the computeFactorial method in order to compute the divisor of each term.  Don't forget that when performing the division, at least one of the operands needs to be a double in order for the result to be a double.


Note: this method is challenging.  Don't worry if you don't have time to finish this method during lab.  (Please do try to finish it on your own time.)

The Fibonacci sequence is

1, 1, 2, 3, 5, 8, 13, 21, ...

The first two elements of the sequence have the value 1.  Each subsequent element of the sequence is computed by adding the previous two elements of the sequence.

The computeFib method takes an int parameter int and computes the n'th element of the Fibonacci sequence.  For example, here are some results of calling this method in the DrJava interactions window:

Welcome to DrJava.
> Loops.computeFib(1)
> Loops.computeFib(2)
> Loops.computeFib(3)
> Loops.computeFib(4)
> Loops.computeFib(5)
> Loops.computeFib(6)
> Loops.computeFib(7)
> Loops.computeFib(8)
> Loops.computeFib(9)
> Loops.computeFib(10)

Hint: you will probably want to treat the values 1 and 2 as special cases, rather than using a loop to compute them.


In a web browser, go to the CS 101 Submission Website,  Using your login and password, upload your version of the file