due: Tuesday October 22nd at 11:59pm (submit on Gradescope here).
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 Lab 4, 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). Please work in pairs. If necessary, you can form a group of 3 but no higher than this please (this limit has been set in Gradescope).
Line
class.You do not need to change any of the code in TextEditor.java
, but feel free to investiagte if you're curious about certain things! The only file you need to change, and upload to Gradescope is the Line.java
file. The starter code can be downloaded here.
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
).
Important 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 I would suggest implementing in the following order. Make sure to check and update the next
and prev
fields (as necessary).
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
, so be sure to treat the case when the first character is inserted. Edge cases to consider: (1) what if the head
is null
? (2) what if the cursor
is null
? (3) what if cursor.next
is null
?
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: (1) what if the head
and/or cursor
are null
? (2) what if cursor.next
is null
? (3) what if cursor.prev
is null
?
moveCursorLeft()
: moves the cursor to the previous CharacterNode
(if it 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
?
moveCursorRight()
: moves the cursor to the next CharacterNode
(if it 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: can the cursor move right if cursor.next
is null
? (2) what should the cursor
be set to if it is currently null
(see the Important note above)?
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"
.
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
Please upload Line.java
to Gradescope. Since you worked on the code as a group, you can submit as a group or you can submit individually if you prefer. Remember to check the style of your code!
Last updated: 2024-12-10