CS 201 - Computer Science I - Fall 2004
Lab 6 - for loops
Loyola College >
Department of Computer Science >
CS 201 >
Labs >
Lab 6
Due
Electronic Submission: Monday, November 11 at 11:59pm. Labs submitted one day late will be assessed a
20% penalty. Labs will not be accepted more than one day late.
Write-up and print out: Wednesday, November 13 at the beginning of class
Objectives
- to use counting loops to calculate sums
Introduction
Recall the setup of a loop to compute the sum of the first n terms of a series:
double sum = 0.0;
for (int i = 0; i < n; i++)
sum += term(i);
where term(i) is an expression that gives the ith term in the
series. The loop header can be changed if that makes the expression for
the ith term simpler (for example sometimes we use
for (int i = 1; i <= n; i++) to make the loop counter go from
1 to n instead of from 0 to n-1).
This code works by making sure that after the loop has run k times,
the accumulator sum holds the sum of the first k terms in the
series. Before the loop starts, sum should hold the sum of
the first 0 terms of the series, which is zero; hence the initialization
double sum = 0.0;. After the (k-1)st iteration (and at the
beginning of the kth iteration), sum holds the sum of the first
k-1 terms in the series. The statement sum += term(i);
means "add the next term into sum." Since sum already
held the sum of the first k-1 terms, adding in the next term
stores the sum of the first k terms in sum. So after
the loop executes n times, sum will hold the sum of the
first n terms of the series.
Assignment
First create two classes, one class called MyMath<LastName1><LastName2>
and a second class MyMathTest<LastName1><LastName2> with a main method that tests MyMath<LastName1><LastName2>.
In MyMath<LastName1><LastName2> you are going to create four public, static methods. All will be type double.
The four methods are exponential and trig
functions as below (notice the similarity between the formulas --
once you get the first one, the others should
follow).
All four methods will take an integer parameter that indicates
how many terms should be included in the sum; the last three also take
double value that should be used as x in the series.
The four methods are:
- e(n) to compute and return the value
,
- exp(x,n) to compute and return the value
,
- sin(x,n) to compute and return the value
, and
- cos(x,n) to compute and return the value
.
To test each one, write a loop to produce a table like so (results
below are not quite right) with values of n large enough to produce an
approximation to eight digits. Test exp on x = 2.
n Math.cos(Math.PI/4) MyMath.cos(Math.PI/4)
1 0.7071067812 1.0000000000
2 0.7071067812 0.6915748625
3 0.7071067812 0.7074292067
4 0.7071067812 0.7071032149
Individual Write-up
In this write-up, you should begin by drawing a flow-diagram for each of the four methods that you wrote. Begin with an introduction that describes the lab. Then for each series write the first five numbers in the series (the numbers may be in fractional rather than decimal form). Then show that the code you wrote produces those five numbers when n=5 and does not include more or fewer terms. Conclude with any comments or concerns about the lab.
For extra credit, determine the smallest n required to produce the same results as the Math class.
This write-up will be graded for quality of writing and use of technical terms. Sophistication of the write-up will also be accessed.
Grading
- Attendance in lab (5 pts)
- Code compiles and runs (10 pts)
- Code produces the expected output (16 pts)
- Correct method headers (4 pts)
- Completeness of Testing (10 pts)
- Program includes sufficient comments (5 pts)
- Write-up (50 pts)
Submissions
Submit the source code for the MyMath<LastName1><LastName2> class and
your test driver, MyMathTest<LastName1><LastName2> using the class submission page. Turn in a print out of your code and write-ups in class.