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


Notes on Assignment 1: A Sequence Computer

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.)

Important Issues

Programming Style

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).

Testing

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.

Extra Credit

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
 */
public class SequenceComputer {

  // +--------+--------------------------------------------------
  // | Fields |
  // +--------+

  /** The sum of the numbers entered into the sequence. */
  protected double sum;

  /** The count of the number externed into the sequence. */
  protected int count;

  /** The smallest number entered into the sequence. */
  protected double smallest;

  /** The second smallest number. */
  protected double secondSmallest;


  // +--------------+--------------------------------------------
  // | Constructors |
  // +--------------+

  /**
   * Initialize everything.  We'll take advantage of the
   * <code>reset</code> method which we've developed.
   */
  public SequenceComputer() {
    this.reset();
  } // SequenceComputer


  // +-----------+-----------------------------------------------
  // | Modifiers |
  // +-----------+

  /**
   * Add another number to the sequence of numbers.
   */
  public void addNumber(double num) {
    // 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 smallest
      this.smallest = num;
    }
    // Bigger than the smallest, smaller than the second smallest.
    else if (num < secondSmallest) {
      this.secondSmallest = num;
    }
  } // addNumber()

  /**
   * Reset the sequence so that it appears as if we've seen no
   * input.
   */
  public void reset() {
    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?
   */
  public int length() {
    return this.count;
  } // length()

  /** 
   * Get the second smallest element of the sequence.
   */
  public double secondSmallest() {
    return this.secondSmallest;
  } // secondSmallest()

  /** 
   * Get the smallest element of the sequence.
   */
  public double smallest() {
    return this.smallest;
  } // smallest()

  /**
   * What is the sum of the numbers we've seen?  Returns 0.0 if
   * no numbers have been placed in the sequence.
   */
  public double sum() {
    return this.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.
   */
  public double average() {
    if (this.count == 0) {
      return Double.NaN;
    }
    else {
      return (this.sum / this.count);
    }
  } // average()

} // SequenceComputer


SCTester


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());
    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 number
    fred.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


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