Question: Write a class named RingBuffer that implements the following API // constructor to create an empty buffer, with given max capacity RingBuffer (int capacity) //


Write a class named RingBuffer that implements the following API // constructor to create an empty buffer, with given max capacity RingBuffer (int capacity) // return number of items currently in the buffer int getSize () // is the buffer empty (size equals zero) ? boolean isEmpty () // is the buffer full (size equals capacity)? boolean isFull( // add item x to the end void enqueue (double x) // delete and return item from the front double dequeue () // return (but do not delete) item from the front double peek () Since the buffer has a known maximum capacity, you can implement a RingBuffer using a double array of that length. For efficiency, use cyclic wrap-around: Maintain one integer instance variable first that stores the index of the least recently inserted item; maintain a second integer instance variable last that stores the index one beyond the most recently inserted item. To insert an item, put it at index last and increment last. To remove an item, take it from index first and increment first. When either index equals capacity, make it wrap-around by changing the index to 0 6 9 10 first last capacity a ring buffer of capacity 10, with 4 elements 10 last first capacity a ring buffer of capacity 10, with 8 elements Note that we have a method, getSize(0. This method returns the number of elements currently in the ring buffer, NOT, the capacity of the ring buffer. Consider the above diagram; in the top example, the buffer has capacity of 10, but size of 4. In the lower example, the buffer also has capacity of 10, but size of 8. Note that elements can be of value 0.0. Thus, buffer is the number of 0-value elements! is not sufficient to say that the size of the ring Note that this data type is all about efficiency. By using a first and last value (which correspond to indices in the array), and allowing it to wrap around, no data needs to be shifted; when we remove the first element, we simply return the value at first, and increment the first value. When we add a value, we simply put it in the index specified by last, and increment last. Write a class named RingBuffer that implements the following API // constructor to create an empty buffer, with given max capacity RingBuffer (int capacity) // return number of items currently in the buffer int getSize () // is the buffer empty (size equals zero) ? boolean isEmpty () // is the buffer full (size equals capacity)? boolean isFull( // add item x to the end void enqueue (double x) // delete and return item from the front double dequeue () // return (but do not delete) item from the front double peek () Since the buffer has a known maximum capacity, you can implement a RingBuffer using a double array of that length. For efficiency, use cyclic wrap-around: Maintain one integer instance variable first that stores the index of the least recently inserted item; maintain a second integer instance variable last that stores the index one beyond the most recently inserted item. To insert an item, put it at index last and increment last. To remove an item, take it from index first and increment first. When either index equals capacity, make it wrap-around by changing the index to 0 6 9 10 first last capacity a ring buffer of capacity 10, with 4 elements 10 last first capacity a ring buffer of capacity 10, with 8 elements Note that we have a method, getSize(0. This method returns the number of elements currently in the ring buffer, NOT, the capacity of the ring buffer. Consider the above diagram; in the top example, the buffer has capacity of 10, but size of 4. In the lower example, the buffer also has capacity of 10, but size of 8. Note that elements can be of value 0.0. Thus, buffer is the number of 0-value elements! is not sufficient to say that the size of the ring Note that this data type is all about efficiency. By using a first and last value (which correspond to indices in the array), and allowing it to wrap around, no data needs to be shifted; when we remove the first element, we simply return the value at first, and increment the first value. When we add a value, we simply put it in the index specified by last, and increment last
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
