Fundamentals of Computer Science II (CSC-152 99S)

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.

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:

Your class must also support this method.

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


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.

Testing Your Class

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.
 * @author Your Name Here
 * @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.print("The sum of 3.5, 1, and 6 is: ");
    // Start a new sequence.
    out.print("After resetting, the sequence has 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.

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.

This page may be found at

Source text last modified Thu Feb 4 16:05:16 1999.

This page generated on Fri Feb 5 10:21:00 1999 by SiteWeaver. Validate this page.

Contact our webmaster at