[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]

Only a few of you chose to do this extra credit assignment, which is fine, given the workload of assignment two. This set of notes is to help all students think about the assignment and to warn you about my grading.

**Points.** Those who turned in this assignment
received between zero and two points of extra credit. I realize that
that doesn't seem like much, but it's as much as a fifth of a grade.
(Five such assignments would bring you from a B to an A, or a C to a B.)

I am a stickler for comments and for uniform indenting. You don't have to use the same indenting style that I use, but you must use a reasonable one. You are expected to use the same commenting style that I use (and this commenting style will get even more extensive as the semester progresses).

You were expected to test your class *beyond the tests I already
gave*. In particular, I wrote ``Extend the SCTester class to do
additional testing and record the output.'' At the very least, you should
have ensured that it was possible to add to the sequence again after it
was reset.

Some of you did the extra credit portion, but did it incorrectly. In the future, incorrect extra credit may result in negative credit. If you turn in something, it should be correct.

`SequenceComputer`

/** * A simple set of methods for computing with sequences. Created * as an example and answer key for the first, optional, assignment * in Grinnell College's CSC152 99S. * * @author Samuel A. Rebelsky * @version 1.0 of February 1999 */publicclassSequenceComputer {// +--------+--------------------------------------------------// | Fields |// +--------+/** The sum of the numbers entered into the sequence. */protecteddoublesum;/** The count of the number externed into the sequence. */protectedintcount;/** The smallest number entered into the sequence. */protecteddoublesmallest;/** The second smallest number. */protecteddoublesecondSmallest;// +--------------+--------------------------------------------// | Constructors |// +--------------+/** * Initialize everything. We'll take advantage of the * <code>reset</code> method which we've developed. */publicSequenceComputer() {this.reset(); } // SequenceComputer// +-----------+-----------------------------------------------// | Modifiers |// +-----------+/** * Add another number to the sequence of numbers. */publicvoidaddNumber(doublenum) {// Update the sum.this.sum =this.sum + num;// Update the count.this.count =this.count + 1;// If the new number is sufficiently small, update the smallest// and/or second smallest numbers.if(num < smallest) {// The new second smallest number is the old smallest.this.secondSmallest =this.smallest;// And we update the smallestthis.smallest = num; }// Bigger than the smallest, smaller than the second smallest.elseif(num < secondSmallest) {this.secondSmallest = num; } } // addNumber()/** * Reset the sequence so that it appears as if we've seen no * input. */publicvoidreset() {this.sum = 0.0;this.count = 0;this.smallest = Double.MAX_VALUE;this.secondSmallest = Double.MAX_VALUE; } // reset()// +------------+----------------------------------------------// | Extractors |// +------------+/** * How many numbers have we seen? */publicintlength() {returnthis.count; } // length()/** * Get the second smallest element of the sequence. */publicdoublesecondSmallest() {returnthis.secondSmallest; } // secondSmallest()/** * Get the smallest element of the sequence. */publicdoublesmallest() {returnthis.smallest; } // smallest()/** * What is the sum of the numbers we've seen? Returns 0.0 if * no numbers have been placed in the sequence. */publicdoublesum() {returnthis.sum; } // sum()/** * What is the average of the numbers we've seen? Returns * Double.NaN (not a number) if we haven't seen any numbers. */publicdoubleaverage() {if(this.count == 0) {returnDouble.NaN; }else{return(this.sum /this.count); } } // average() } // SequenceComputer

`SCTester`

importSimpleOutput;importSequenceComputer;/** * Test the cool SequenceComputer class. * * @author Your Name Here * @version 1.0 of January 1999 */publicclassSCTester {/** * Test away! */publicstaticvoidmain(String[] args) {// Prepare for output.SimpleOutput out =newSimpleOutput();// Prepare for computation.SequenceComputer fred =newSequenceComputer();// 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()); out.print("The average in this empty sequence is"); out.println(fred.average());// Build a new sequence with two numbers to test the// smallest methods.fred.addNumber(1); fred.addNumber(2); out.println("Added 1,2"); out.println("Smallest:" + fred.smallest()); out.println("next:" + fred.secondSmallest());// Add a smaller number.fred.addNumber(0); out.println("Added 0"); out.println("Smallest:" + fred.smallest()); out.println("next:" + fred.secondSmallest());// Add some negative numbers. We'll duplicate the number to// see if we get the two smallest numbers.fred.addNumber(-1); fred.addNumber(-1); out.println("Added -1,-1"); out.println("Sum:" + fred.sum()); out.println("Length:" + fred.length()); out.println("Smallest:" + fred.smallest()); out.println("next:" + fred.secondSmallest());// Add another negative numberfred.addNumber(-123.45); out.println("Added -123.45"); out.println("Sum:" + fred.sum()); out.println("Length:" + fred.length()); out.println("Smallest:" + fred.smallest()); out.println("next:" + fred.secondSmallest()); } // main(String[]) } // class SCTester

**History**

- The two classes were created on Thursday, February 4, 1999.
- This page was created on Friday, February 5, 1999.

[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/notes.01.html

Source text last modified Fri Feb 5 10:17:20 1999.

This page generated on Fri Feb 5 10:19:22 1999 by SiteWeaver. Validate this page.

Contact our webmaster at rebelsky@math.grin.edu