Assignment #8 Instructions

Create a RingBuffer class that holds doubles and uses an array to implement the buffer.

A ring buffer is a fixed sized FIFO buffer that behaves as if it were connected end-to-end (as a snake eating its tail). Ring buffers have the benefit of never running out of space, i.e. you can always add more elements to it. However, adding elements when the buffer is full causes the oldest elements to be overwritten. Ring buffers are commonly used in Digital Signal Processing (DSP) to handle audio buffers. An example is a situation in which your code is sending data to a device (such as a sound card) but the device is not able to keep up with the data flow. The ring buffer will then continually cycle the data, tossing the oldest, until the sound card is able to take more.

When implemented as an array, the first item in the buffer (i.e. the next to be removed) is not necessarily in the first element of the array. For example, consider the following actions and their result in a ring buffer of capacity 5:

  • Add 1.0: actual contents [1.0, -, -, -, -], displayed contents [1.0]
  • Add 2.0: actual contents [1.0, 2.0, -, -, -], displayed contents [1.0, 2.0]
  • Add 3.0: actual contents [1.0, 2.0, 3.0, -, -], displayed contents [1.0, 2.0, 3.0]
  • Remove: actual contents [ -, 2.0, 3.0, -, -], displayed contents [2.0, 3.0]
    • notice no elements are shifted...the new "1st" element is actually now in the 2nd array element slot
  • Add 4.0: actual contents [ -, 2.0, 3.0, 4.0, -], displayed contents [2.0, 3.0, 4.0]
  • Add 5.0: actual contents [ -, 2.0, 3.0, 4.0, 5.0], displayed contents [2.0, 3.0, 4.0, 5.0]
  • Add 6.0: actual contents [6.0, 2.0, 3.0, 4.0, 5.0], displayed contents [2.0, 3.0, 4.0, 5.0, 6.0]
    • notice this addition is placed at the beginning of the actual array even though it's the last in the ring buffer
  • Add 7.0: actual contents [6.0, 7.0, 3.0, 4.0, 5.0], displayed contents [3.0, 4.0, 5.0, 6.0, 7.0]
    • here the buffer was full so the oldest entry was overwritten
  • Remove: actual contents [6.0, 7.0, -, 4.0, 5.0], displayed contents [4.0, 5.0, 6.0, 7.0]
    • removing the "1st" item now leaves a "hole" in the middle of the buffer
  • Peek: returns the value 4.0

Your RingBuffer class will provide the following public methods:

  1. RingBuffer(int capacity): constructor that creates a ring buffer with the given capacity.
  2. void add(double d): adds the given double to the ring buffer.
  3. double remove(): removes and returns the first/oldest item in the ring buffer.
    • Throws NoSuchElementException if attempting to remove from an empty buffer.
  4. double peek(): returns (without removing) the first/oldest item in the ring buffer.
    • Throws NoSuchElementException if attempting to peek in an empty buffer.
  5. int capacity(): returns the capacity (maximum number of elements) of the ring buffer.
  6. int size(): returns the current number of elements held in the ring buffer.
  7. void clear(): empties the ring buffer (removes all elements) ... after the operation, buffer size is zero.
  8. String toString(): returns a string representation of this ring buffer using normal toString formatting. Does not destroy the buffer, i.e. the buffer remains the same after a call to this method.

Submit the assignment 8 source code file to me as usual via DropItToMe:

Follow class programming standards and formatting/indentation rules. Make sure you comment your code! In your method headers you must include pre and postconditions.

Assignment Grading

The assignment is worth 35 points.

This is pretty simple...you get 100% if your output exactly matches the expected output...and...0% if it has ANY differences.

Make sure you get all the spelling, capitalization, punctuation, spacing, etc. correct! Hint: copy-n-paste is a wonderful thing! :)