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, http://www.cs.vassar.edu/~cs101/hovemeyer/, go to the "Labs" section at the bottom and download the file Loops.java.  Start DrJava and open Loops.java.

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.

printIntSequence

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.

computeFactorial

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.

estimateE

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)
0.0
> Loops.estimateE(1)
1.0
> Loops.estimateE(2)
2.0
> Loops.estimateE(3)
2.5
> Loops.estimateE(4)
2.6666666666666665
> Loops.estimateE(5)
2.708333333333333
> Loops.estimateE(6)
2.7166666666666663

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.

computeFib

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)
1
> Loops.computeFib(2)
1
> Loops.computeFib(3)
2
> Loops.computeFib(4)
3
> Loops.computeFib(5)
5
> Loops.computeFib(6)
8
> Loops.computeFib(7)
13
> Loops.computeFib(8)
21
> Loops.computeFib(9)
34
> Loops.computeFib(10)
55

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

Submitting

In a web browser, go to the CS 101 Submission Website, https://mr42.cs.vassar.edu/dropbox/cs101/upload.html.  Using your login and password, upload your version of the file Loops.java.