Use a doubly-linked list of characters to represent lines in a text editor.
Practice checking for edge cases.
Submit these files:Line.java
In this lab, you'll use a doubly-linked list of characters to represent the lines of text in your own text editor called MiddDocs.
As in previous labs, you’ll work in groups. Recall that only one group member should start the Live Share server (by clicking on Live Share). Then send this link to the other group member(s).
Complete the Line class.
You do not need to change any of the code in TextEditor.java, but feel free to investigate if you’re curious about certain things! The only file you need to change, and upload to Gradescope is the Line.java file, which you can download using the starter code button above.
Line.java contains two class definitions: CharacterNode and Line. The CharacterNode class represents a character in a line of text, which is designed as a node in a doubly-linked list. Hence, it has prev and next fields (which are public). It also stores the character (private), which is like the data field we used in our examples in class.
The Line class is a doubly-linked list which keeps track of the head and cursor nodes. The head is the first character in the line (if any) and the cursor is the current location of the cursor, which can be moved with the arrow keys. There is no tail in the Line linked list. All operations (insertion, deletion, cursor movement) operate on the current location of the cursor node. When the cursor is drawn (as a vertical bar |), it is drawn immediately after the character where the cursor is located. For example, the sequence of characters abcd|efgh means the cursor is at the letter d (and the head is at the letter a).
NoteImportant Note
In order to represent the cursor being at the beginning of a line, the cursor can be null (while the head is non-null). For example, the line |abcd means the cursor is null, and the head is a.
Your task is to complete 5 methods, which we suggest implementing in the following order. Make sure to check and update the next and prev fields (as necessary).
A companion worksheet is provided to help you figure out how to update your fields for the insert(char c) and delete() methods. I recommend completing the worksheet before writing any code!
1. insert(char c)
Inserts a character right after the current cursor location. For example, calling insert('a') when the current Line is ban|na will produce bana|na. Note that the head and cursor are initially null, which is an edge case; some edge cases are handled for you (indicated below).1
Edge cases to consider:
What if the head is null? (✅ already handled)
What if the cursor is null? (✅ already handled)
What if cursor.next is null?
Remember to update cursor to the new node in all cases.
2. delete()
Deletes the character located at the cursor. For example, calling delete when the current Line is spider|rman will produce spide|rman.
Edge cases to consider:
What if the head and/or cursor are null? (✅ already handled)
What if the head is the cursor? (✅ already handled)
What if cursor.prev is null?
What if cursor.next is null?
3. moveCursorLeft()
Moves the cursor to the previous CharacterNode (if the cursor isn’t null). This is called when the left arrow key is pressed. For example, calling moveCursorLeft() when the Line is polyg|on will produce poly|gon.
Edge case to consider:
Can the cursor move left if it is null?
4. moveCursorRight()
Moves the cursor to the next CharacterNode (if the next isn’t null). This is called when the right arrow key is pressed. For example, calling moveCursorRight() when the Line is middleb|ury will produce middlebu|ry.
Edge cases to consider:
What should the cursor be set to if it is currently null (see the Important note above)?
Can the cursor move right if cursor.next is null?
5. Line(String line)
Creates an entire line from an input String. This is used when loading a file into the text editor. The cursor should be at the beginning of the line after reading the entire line. For example, creating a Line using new Line("middlebury") will render as "|middlebury".
Hints: 1. Use your insert method to add characters to this 1. Remember you can use s.charAt(i) to get the i-th character from a String s 1. Remember at the end to reset cursor so it is at the beginning of the line.
Development Process
A PSVM has been set up for you to get started testing these methods. Please use this to develop your Line class. You can then test the fully-featured MiddDocs editor by running TextEditor.java.
You can use the Escape key to exit MiddDocs. You’ll then be prompted (in the command-line) about saving the file: respond with either y (yes) or n (no). If you save the file, you can then re-open it directly from the command-line, which will require compiling your code separately with javac and then running it with java. For example if you write a poem and save it to poem.txt:
$ javac TextEditor.java$ java TextEditor poem.txt
Footnotes
Read through the code for these edge cases and make sure you understand it!↩︎