CMPS 144 Fall 2026
Prog. Assg. #4: Command Line Interface Editor, with Undo/Redo
Due: Nov. 1

Background

In an earlier era of computing, before the advent of the graphical user interface (or even its predecessor, the "full-screen" editor), editing a document was carried out using a command line interface (aka CLI). To insert a line of text into the document (which was viewed as a sequence of lines of text numbered starting at zero or one), you would issue a command (by typing it in response to a prompt symbol such as >) that might look something like this:

> insert 23 "The cat in the hat ate a rat."

As you might infer, the intended effect of this command is to insert, at line 23 of the document, the quoted text, meaning that this text would come to occupy line 23 while the text that had been in lines 23, 24, etc., etc., would become lines 24, 25, etc., etc.

As you would expect, in addition to an insert command, such editing tools supported commands such as delete (to delete a line) and replace (which, in effect, is a deletion of a line followed by an insertion of a line at the same place).

Other commonly supported commands included move and copy, which have the obvious interpretations. Among these, the delete, move, and copy commands often had versions that applied to a block of lines (as opposed to a single line), so that, for example, the command delete 20 24 would indicate the user's intent to delete four lines of text starting at line 20 (i.e., those whose locations were in the range [20..24)).

Some CLI editors of that time supported what was then considered an advanced feature: undo. This meta-command allowed the user to reverse the effects of one or more of the most recently executed edit commands. Going even one step further was the redo meta-command, which "undoes" the effects of the undo command by "re-doing" the command that was most recently "undone"!1


Assignment

For this assignment, the student is to complete the development of a command-line interface (CLI) text editor that supports the insert line, replace line, and delete line edit commands, as well as the undo and redo meta-commands. Here are links to the needed Java classes/interfaces, four of which are incomplete:


Approach

To insert, remove, and replace lines within a document is straightforward, given a Java class that implements the Document interface (which, you will notice, dictates the presence of insertLine(), replaceLine(), and deleteLine() methods). Such a class has been provided.

What makes this assignment interesting, and nontrivial, is the requirement that the implemented CLI editor supports the undo and redo meta-commands.

Our premise is that the current state of a document (i.e., its contents) is determined by its initial state (i.e., as it existed when editing began) and the modifications resulting from the sequence of edit commands that were applied to it since then. (Thus, a document could be reconstructed exactly if known are both its initial state and that sequence of edit commands.)

The idea of the undo meta-command is that, when applied, a document reverts back to the state it had been in before the most recent edit command, as though that command were never executed. But we need not stop there, as a second application of undo should take the document backwards one step further, to the state it had been in before the next-to-last edit command was executed. And so on and so forth, in case undo were applied three or more times. Indeed, if undo were applied as many times as edit commands had been applied, the document should revert back to its initial state!

By what logic can this be implemented?
Answer: Clearly, it is necessary to "remember" the exact sequence of edit commands that were executed upon the document.2 Because the order in which commands are undone is opposite to the order in which they were executed, the clear choice for an appropriate storage structure is the stack, which provides LIFO (last-in-first-out) access to its elements.

The solution, then, is that each time an edit command is executed, a representation of it is pushed onto the stack. When undo is applied, the command on top of the stack is popped and whatever actions are necessary to reverse its effects are taken. (In other words, the inverse of the popped command is executed.)

But what, exactly, would be the form taken by the "representation of an edit command"?
Answer: Java is an object-oriented programming language. Hence, an edit command would be naturally represented by an object whose instance variables contained the data/information sufficient to unambiguously describe the command. For the purposes of this assignment, such an object would be an instance of one of the classes that implements the EditCommand interface (see above).

Now, what about the redo meta-command? What is its intended effect?
Answer: The intent of redo is to have the effect of re-executing the most recently undone edit command! Like undo, redo can be invoked several times in a row, but is limited by the number of times that undo was invoked since the last time that an edit command was invoked. This guarantees that every application of redo puts the document back into a state that it had been in at some previous moment.

To illustrate using a generic example, suppose that ten edit commands have been executed, say C1, C2, ..., C10, in that order. Now the user realizes that he may have made a mistake when issuing the command C7, so he invokes undo four times, thereby reversing the effects of C10, C9, C8, and C7, in that order. At that point, the document would have reverted back to the state it had been in immediately after the application of C6.

But then the user realizes that he really did want to apply commands C7 and C8. To get that effect, he invokes redo two times, causing C7 and C8 to be "redone". At this point, it is possible to redo C9 (and C10 thereafter). It is also possible to undo C8 again (and, afterwards, to undo any number of C7, C6, etc.).

But suppose that, after having redone C7 and C8, the user chooses to invoke a "new" edit command C11. At that point, there is no longer an option to redo, and the fact that edit commands C9 and C10 were done and later undone can be forgotten.

Question: Why can neither C9 nor C10 be redone after the application of C11?
Answer: Suppose, after applying C11, C9 were redone. Then the document would be in the state obtained, beginning in its initial state, by the sequence of edit commands C1, C2, ..., C8, C11, C9. But this state is not guaranteed to be one that the document had been in previously, which is contrary to the intended effect of redo.

Implementing redo adds some complexity to our problem. In the first place, we now need a second stack, this one to store the edit commands that have been undone (since the last time a "new" edit command was issued), making them candidates for being "redone" later.

Referring to the two stacks as the Done-stack (which holds the sequence of edit commands that put the document into its current state) and the Undone-stack (which holds the edit commands that have been undone since the last time a "new" edit command was issued), the logic now is that whenever an undo occurs, the edit command on top of the Done-stack must be transferred to the Undone-stack. Meanwhile, whenever the user issues an edit command, the Undone-stack must be cleared (i.e., made empty), because invoking redo at that point is prohibited.

In the second place, we must ensure that the objects that represent edit commands (i.e., instances of the classes InsertCommand, DeleteCommand, and ReplaceCommand) contain data sufficient not only to make clear how to undo them but also sufficient to make clear how to redo them.


Sample Dialog with CLI_Editor

Making use of a file containing names of Star Trek episodes, here is a sample dialog with the CLI_Editor program:

$ java CLI_Editor star_trek.txt
Welcome to the CLI_Editor
-------------------------

> help
Command templates:
  insert <line_number> <text>
  delete <line_number>
  replace <line_number> <text>
  display <line_number> <line_number>
  display
  saveAs <file_name>
  save
  undo
  redo
  exit


> display
[0]: Arena
[1]: Mirror, Mirror
[2]: City on the Edge of Forever
[3]: Amok Time
[4]: Devil in the Dark


> insert 3 The Enterprise Incident
Executed: INSERT "The Enterprise Incident" at line 3

> display
[0]: Arena
[1]: Mirror, Mirror
[2]: City on the Edge of Forever
[3]: The Enterprise Incident
[4]: Amok Time
[5]: Devil in the Dark

> replace 3 This Side of Paradise
Executed: REPLACE "The Enterprise Incident" 
in line 3 by "This Side of Paradise"

> display
[0]: Arena
[1]: Mirror, Mirror
[2]: City on the Edge of Forever
[3]: This Side of Paradise
[4]: Amok Time
[5]: Devil in the Dark

> delete 0
Executed: DELETE line 0

> display
[0]: Mirror, Mirror
[1]: City on the Edge of Forever
[2]: This Side of Paradise
[3]: Amok Time
[4]: Devil in the Dark

> undo
Undid: DELETE line 0

> display
[0]: Arena
[1]: Mirror, Mirror
[2]: City on the Edge of Forever
[3]: This Side of Paradise
[4]: Amok Time
[5]: Devil in the Dark


continued in next column


> undo
Undid: REPLACE "The Enterprise Incident" 
in line 3 by "This Side of Paradise"

> display
[0]: Arena
[1]: Mirror, Mirror
[2]: City on the Edge of Forever
[3]: The Enterprise Incident
[4]: Amok Time
[5]: Devil in the Dark

> redo
Redid: REPLACE "The Enterprise Incident"
in line 3 by "This Side of Paradise"

> display
[0]: Arena
[1]: Mirror, Mirror
[2]: City on the Edge of Forever
[3]: This Side of Paradise
[4]: Amok Time
[5]: Devil in the Dark

> insert 4 Spock's Brain
Executed: INSERT "Spock's Brain" at line 4

> redo
Nothing left to redo!

> undo
Undid: INSERT "Spock's Brain" at line 4

> display
[0]: Arena
[1]: Mirror, Mirror
[2]: City on the Edge of Forever
[3]: This Side of Paradise
[4]: Amok Time
[5]: Devil in the Dark

> undo
Undid: REPLACE "The Enterprise Incident"
in line 3 by "This Side of Paradise"

> display
[0]: Arena
[1]: Mirror, Mirror
[2]: City on the Edge of Forever
[3]: The Enterprise Incident
[4]: Amok Time
[5]: Devil in the Dark

> redo
Redid: REPLACE "The Enterprise Incident"
in line 3 by "This Side of Paradise"

> display
[0]: Arena
[1]: Mirror, Mirror
[2]: City on the Edge of Forever
[3]: This Side of Paradise
[4]: Amok Time
[5]: Devil in the Dark

> insert 6 Plato's Stepchildren
Executed: INSERT "Plato's Stepchildren" at line 6

> display
[0]: Arena
[1]: Mirror, Mirror
[2]: City on the Edge of Forever
[3]: This Side of Paradise
[4]: Amok Time
[5]: Devil in the Dark
[6]: Plato's Stepchildren

> exit
Goodbye!


Program Submission

As usual, use the appropriate Brightspace drop box to submit your Java classes. You should submit the Java source code (i.e., the .java files) of only the four classes that you were to have completed. In each one, make sure to give credit to any "agent" (human or AI) that gave you aid in completing it. Also note any defects.


Footnotes

[1] Software architecture and user interface design have advanced over the years, so that features such as undo/redo are commonly supported. Software components built to handle such features are sometimes called command history management tools or history engines.

In the study of software design patterns, the pattern referred to as Command applies, as it turns a user action (such as typing text, deleting a file, or drawing a shape) into a stand-alone object containing all the information about that action, making it possible to undo or redo it later.

[2] The astute reader will recognize that what really is necessary to remember is not literally each executed edit command in the sequence, but rather, for each such command, information sufficient to reverse its effects (i.e., the command's inverse), in case the command is later "undone". Later we will see that, for purposes of implementing redo, it's necessary to remember the commands themselves, and not just their inverses.