Question: Exercise 4. (Text Editor Buffer) Implement a data type called Buffer to represent a buffer in a text editor. The data type must support the

 Exercise 4. (Text Editor Buffer) Implement a data type called Bufferto represent a buffer in a text editor. The data type mustsupport the following API: E Buffer Buffer() void insert (charc) char delete()

void left(int k) creates an empty buffer inserts c at the cursorposition deletes and returns the character immediately ahead of the cursor moves

Exercise 4. (Text Editor Buffer) Implement a data type called Buffer to represent a buffer in a text editor. The data type must support the following API: E Buffer Buffer() void insert (charc) char delete() void left(int k) creates an empty buffer inserts c at the cursor position deletes and returns the character immediately ahead of the cursor moves the cursor k positions to the left moves the cursor k positions to the right returns the number of characters in this buffer returns a string representation of this buffer with the "T" character (not part of the buffer) at the cursor position void right(int k) int size() String toString() >- */workspace/project2 $ java Buffer | There is grandeur in this view of life, with its several powers , having been originally breathed by the Creator into a few forms or into one; and that, whilst this planet has gone cycling on according to the fixed law of gravity, from so simple a beginning endless forms most beautiful and most wonderful have been, and are being, evolved. Charles Darwin, The Origin of Species Hint: Use two stacks left and right to store the characters to the left and right of the cursor, with the characters on top of the stacks being the ones immediately to its left and right. C Buffer.java import dsa. LinkedStack; import stdlib.Stdout; // A data type to represent a text editor buffer. public class Buffer { protected LinkedStack left; // chars left of cursor protected LinkedStack right; // chars right of cursor // Creates an empty buffer. public Buffer() { } // Inserts c at the cursor position. public void insert (char c) { } the character immediately ahead of the cursor. // Deletes and returns public char delete () { } // Moves the cursor k positions to the left. public void left (int k) { } // Moves the cursor k positions to the right. public void right (int k) { } // Returns the number of characters in this buffer. public int size () { Buffer.java } // Returns a string representation of the buffer with the "|" character (not part of the buffer) // at the cursor position. public String toString() { // Push chars from left into a temporary stack. // Append chars from temporary stack to sb. // Append "|" to sb. // Append chars from right to sb. // Return the string from sb. } + // Unit tests the data type (DO NOT EDIT). public static void main(String[] args) { Buffer buf = new Buffer (; Strings = "There is grandeur in this view of life, with its several powers, having been "originally breathed into a few forms or into one; and that, whilst this planet" "has gone cycling on according to the fixed law of gravity, from so simple a " + "beginning endless forms most beautiful and most wonderful have been, and are " + "being, evolved. Charles Darwin, The Origin of Species"; for (int i = 0; i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!