1. Evaluating mathematical series
2. Testing code in the DrJava Console
Evaluating mathematical series
We looked at several examples of computing the value of a mathematical series.
The goal of these examples was to learn how to identify the patterns expressed
in mathematical notation and how to describe the computation in a programming
language.
In each of the examples below we need to decide:
* what "information" to keep track of, i.e. what variables/PostIts we need
* initial values for these variables/PostIts
* how many times to go through the loop/computation
* how to update the variables/PostIts on each cycle of the loop
Example 1
Goal: Write a procedure addIntegers(a, b) which computes the sum of the integers
in the range [a..b]:
a + ... + b
As we carry out the computation we need to:
* keep track of:
- result/total so far (initially empty, i.e. 0)
- current number to add to result (initially a)
* update the variables:
- total <-- total + number (add current number to the total)
- number <-- number + 1 (move on to the next number to add)
Here is the outline of the computation:
1. keep track of total so far (initially 0)
2. keep track of current number (initially a)
3. as long as current number is in the range [a..b]:
4. update total (add to it the number)
5. update current number (add to it 1 to get to its neighbor)
6. give back the total
The Java code is:
// computes the value of a+...+b
int addIntegers(int a, int b)
{
int result = 0; // 1.
int curNum = a; // 2.
while (curNum <= b) // 3.
{
result = result + curNum; // 4.
curNum = curNum + 1; // 5.
}
return result; // 6.
}
Example 2
Goal: Write a procedure double addPowers(double x, int n) which
computes the following sum given:
* n: how many things to add (not the last power to go to)
* x: the value to be raise to powers
1 + x + x2 + x3 + x4 + x5 + ...
As in the previous examples we have a summation of individual "blobs" (quantities) where:
* each blob is a power of x (including 1 which is just x0)
* each blob is equal to the previous blob multiplied by x
To solve the problem we need to:
* keep track of:
- result/total: overall result so far (initially 0)
- current number: what to add to the result for current cycle (initially 1)
- count: how many numbers added so far to total (initially 0)
* update the variables:
- total <-- total + curNum
- curNum <-- curNum*x
- count <-- count + 1
The Java code is:
double addPowers(double x, int n)
{
double result = 0; // start with empty result
double curNum = 1; // first quantity in the sequence
int count = 0; // no numbers added yet
while ( count < n )
{
result = result + curNum; // add current blob to total
curNum = curNum*x; // move on to next blob
count = count + 1; // one more quantity add to result
}
return result;
}
Testing in the DrJava Console
The procedures above do not produce a drawing that can be tested visually on the
screen. Instead, these procedures give back/produce/return single numbers as the
answer/result of their computation.
These procedure will be tested by printing their answers in the DrJava console
using System.out.println( ... )
Here is the run() procedure with test cases for the code written above. In general,
you should try the first few simplest cases, then a couple of slightly more complex
(but still checkable by hand); optionally, you may try a couple of "large" cases.
public void run()
{
// expecting to see in the console below:
System.out.println("addIntegers(1, 10): " + addIntegers(1, 10)); // addIntegers(1, 10): 55 // 55 is 1+2+3+..+10
System.out.println("addIntegers(15, 20): " + addIntegers(15, 20)); // addIntegers(15, 20): 105 // 105 is 15+16+...+20
System.out.println("addIntegers(7, 7): " + addIntegers(7, 7)); // addIntegers(7, 7): 7 // start at 7, stop at 7, thus only 7
System.out.println("addIntegers(100, 500): " + addIntegers(100, 500)); // addIntegers(100, 500): 1500 // 1500 is 100+101+102+...+500
// Note: In addPowers(...) we decided that n is how many things to add, not the last power to go to.
// So for addPowers(2, 5) we add the first 5 things: 1 + 2 + 2^2 + 2^3 + 2^4
// expecting to see in the console below:
System.out.println("addPowers(2, 0): " + addPowers(2, 0)); // addPowers(2, 0): 0 // 0 since asked to add 0 things
System.out.println("addPowers(2, 1): " + addPowers(2, 1)); // addPowers(2, 1): 1 // only add the first one thing in the series
System.out.println("addPowers(2, 2): " + addPowers(2, 2)); // addPowers(2, 2): 3 // 1+2 (add the first two things)
System.out.println("addPowers(2, 7): " + addPowers(2, 7)); // addPowers(2, 7): 127 // 1+2+2^2+2^3...+2^6 (add the first 7 quantities)
System.out.println("addPowers(2, 10): " + addPowers(2, 10)); // addPowers(2, 10): 1023 // 1+2+2^2+2^3...+2^9 (add the first 10 quantities)
System.out.println("addPowers(5, 0): " + addPowers(5, 0)); // addPowers(5, 0): 0 // 0 since asked to add 0 things
System.out.println("addPowers(5, 1): " + addPowers(5, 1)); // addPowers(5, 1): 1 // only add the first thing in the series
System.out.println("addPowers(5, 2): " + addPowers(5, 2)); // addPowers(5, 2): 6 // 1+5 (add the first two things)
System.out.println("addPowers(5, 7): " + addPowers(5, 7)); // addPowers(5, 7): 19531 // 1+5+5^5+5^3...+5^6 (add the first 7 quantities)
System.out.println("addPowers(5, 10): " + addPowers(5, 10)); // addPowers(5, 10): 2441406 // 1+5+5^5+5^3...+5^9 (add the first 10 quantities)
}