[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
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.
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.
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.
*
* @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.
fred.addNumber(3.5);
fred.addNumber(1);
fred.addNumber(6);
// 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.
Do none, some, or all.
SequenceComputer to compute the minimum and
maximum values in the sequence. (Easy)
SequenceComputer to compute the second smallest
value in the sequence. (Harder)
SequenceComputer, does what the user has asked.
(Not too hard, but requires loops, which we won't cover until class 6.)
SequenceComputer, computes the average and sum
of that sequence.
(Not too hard, but requires loops, which we won't cover until class 6.)
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
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 http://www.math.grin.edu/~rebelsky/Courses/CS152/99S/Assignments/assign.01.html
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 rebelsky@math.grin.edu