# Assignment 1: Calculation

Assigned: Wednesday, January 26, 1999
Due: Wednesday, February 3, 1999

In this assignment, you will create and test a class, `SequenceComputer`, that will perform a number of sequence-based computations.

This is an optional assignment. It will provide a small amount of extra credit if completed on time. It will also provide a valuable exercise to help you learn Java.

## The SequenceComputer Class

Create a class, `SequenceComputer`, that does computations based on sequences of numbers presented to it. What computations do we typically do based on sequences? We might compute the sum of the numbers in the sequence or the average of the numbers in the sequence. Your class must support the following methods.

• `double average()` --- compute the average of the numbers in the sequence.
• `double sum()` --- compute the sum of the numbers in the sequence.
• `int length()` --- compute the length of the sequence.

You may be asking yourself how we present the sequence of numbers to `SequenceComputer`. We will do so through an `addNumber` method, defined as follows:

• `void addNumber(double num)` --- add a number to the sequence of numbers.

Your class must also support this method.

Finally, your `SequenceComputer` class should support a `reset` method, that resets the class to the empty sequence.

### Notes

You may be asking yourself ``How do I represent a sequence?'' It turns out that the particular computations above do not require you to remember the whole sequence, simply some facts about the sequence. You will use fields to remember those facts. (And yes, it is a challenge to you to decide what facts to remember.)

You may also be asking yourself ``What should I do if someone asks me for the average in an empty sequence?'' We'll say that it's illegal to do so, and you can do whatever you want in such cases. In class 7 we'll discuss some possibilities.

Once you've built your `SequenceComputer` class, how should you test it? One possibility is to create a class that does some basic tests. For example,

```import SimpleOutput;
import SequenceComputer;

/**
* Test the cool SequenceComputer class.
*
* @version 1.0 of January 1999
*/
public class SCTester {
/**
* Test away!
*/
public static void main(String[] args) {
// Prepare for output.
SimpleOutput out = new SimpleOutput();
// Prepare for computation.
SequenceComputer fred = new SequenceComputer();
// Tell fred about the sequence.
// Print some information.
out.print("The average of 3.5, 1, and 6 is: ");
out.println(fred.average());
out.print("The sum of 3.5, 1, and 6 is: ");
out.println(fred.sum());
// Start a new sequence.
fred.reset();
out.print("After resetting, the sequence has length ");
out.println(fred.length());
// And so on and so forth.
} // main(String[])
} // class SCTester
```

Extend the `SCTester` class to do additional testing and record the output.

## Extra Credit

Do none, some, or all.

• Extend `SequenceComputer` to compute the minimum and maximum values in the sequence. (Easy)
• Extend `SequenceComputer` to compute the second smallest value in the sequence. (Harder)
• Build a class that asks the user what to do (e.g., add an element to the sequence, reset the sequence, print the average, etc.) and, using `SequenceComputer`, does what the user has asked. (Not too hard, but requires loops, which we won't cover until class 6.)
• Build a class that reads a 0-terminated sequence of numbers and, using `SequenceComputer`, computes the average and sum of that sequence. (Not too hard, but requires loops, which we won't cover until class 6.)

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.