Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Overview:You have been tasked with writing a simple text editor. Your text editor will allow the user to create, read, and edit text files.Basic Idea:Your
Overview:You have been tasked with writing a simple text editor. Your text editor will allow the user to create, read, and edit text files.Basic Idea:Your text editor will be implemented using a modified doubly linked list. In lecture, you have been taught how to traverse, insert into, and delete from doubly linked lists. For this assignment, you will be applying this knowledge but also maintaining an internal cursor in your data structure. The cursor is the vertical bar that appears when you are typing.Implementation Details:The provided zip file on LATTE contains Java files in packages: main: Node.java : this file contains the implementation of a doubly linked list node. Like in lecture, this class has fields next and prev. However, instead of holding integer data, this class holds a character. Do not edit this file main: Editor.java : this file will contain the implementation of your text editor. It will contain a variety of methods used for reading, inserting into, deleting from, and tracking the cursor in a text file. This is the only class in the main package you need to write main: EditorDisplay.java : this file contains the implementation of a simple graphical user interface GUI for you to view your Editor class working. Do not edit this file. more on page main: EditorMain.java : this file starts the GUI version of your Editor. You will find it has commented lines of code. One will start your Editor with no input file line The second will start it with a file containing a single line of text line The third will start it with a file containing multiple lines of text line more on page test: ExampleEditorTests.java : this file contains basic tests of various capabilities of the text editor. They use and expand on the examples presented in sections IV of the implementation details of Editor.java on pages Do not edit this file test: StudentEditorTests.java : this file is where you should write your own JUnit tests of the various methods of the Editor class. The provided tests do not test edge cases or more advanced functionality. You should be testing your class thoroughly.Notes: The methods that you will have to implement in Editor.java are described in detail below up to page Furthermore, there are five public fields you will find in Editor.java. You must use all these fields in your implementation. The skeleton also includes text files that are used by lines and of EditorMain.java.Editor.javaYour text editor will have to support a variety of functionality. You have been provided with five instance fields and a variety of functions in the skeleton code, but here the implementation of the various capabilities of the editor will be explained one at a time.Note: The two constructors for this class are listed first in the skeleton as per Java convention. However, they will be discussed after most of the other required methods.You cannot use any Javaprovided data structures in your implementation. You cannot use any Javaprovided libraries unless they are specified below.I. The CursorSuppose that your text editor contains the string blue. In your editor, what are the possible locations of the cursor? Before b After b or before l After l or before u After u or before e After eIn general, if your text editor contains a string of n characters, the cursor can be placed in n possible locations.For the text editor you will be implementing, if a cursor is at position k then there are k characters in the editors string before the cursor. Let us consider the example string blue and the locations presented above Before b There are characters before the cursor. Hence, the cursor would be at position After b or before l There are characters b before the cursor. Hence, the cursor would be at position After l or before u There are characters b l before the cursor. Hence, the cursor would be at position After u or before e There are characters b l u before the cursor. Hence, the cursor would be at position After e There are characters b l u e before the cursor. Hence, the cursor would be at position In general, if your text editor contains a string of n characters, the cursors possible positions are nThis provides motivation for two of the fields you need to use in this class as well as for the implementation of two of the methods you are required to write.public int numChars this field will maintain a count of the number of characters currently stored in the editor.public int curPos this field will track the index of the cursor in the editor. It will take on values in the interval numCharspublic int size this method returns the number of characters stored in the editor. public int getCursorPosition this method returns the index of the cursor inthe editor.II Cursor Movement & Default ConstructorAt this point, you have been introduced to how the cursor will be tracked and where it will be placed in your editors string. In sections III and IV you will see how insertion and deletion will be done relative to the cursors position. However, you will first need to implement the capability of moving the cursors current position. This will introduce you to how the doubly linked list will be used as the underlying implementation of the editor.Returning to our example string blue stored in the editor, it will be stored as a doubly linked list of four characters: b l u e with one character per node.In your editor, you will be using the field cur to track the node located after the current cursor position or null. Consider the cursor locations described in section I for the string blue: Before ba curPos b cur holds a reference to the node containing bIn the stringAfter a bAfter a bAfter a bAfter a bb or before lcurPos cur holds a reference to the node containing ll or before ucurPos cur holds a reference to the node containing uu or before ecurPos cur holds a reference to the node containing eecurPos curisnullth cursor location, cur is null to indicate that the cursor is after the fourth character in the as opposed to being before it This distinction will make more sense in sections III and IVThe user will be able to move the cursor in ways: Moving the cursor right by one character. For instance, if curPos and cur references the node containing b Moving it right should result in curPos being and cur referencing the node containing l Moving the cursor left by one character. For instance, if curPos and cur references the node containing e Moving it left should result in curPos being and cur referencing the node containing u Moving the cursor to be before the first character. For instance, if curPos and cur references the node containing u This move should result in curPos being and cur referencing the node containing b Moving the cursor to be after the last character. For instance, if curPos and cur references the node containing u This move should result in curPos being and cur being null.This provides the motivation for the existence of the three Node variables in the Editor as well as the implementation of four of the methods you are required to write.public Node head this field will hold a reference to the head of the doubly linked list that is used in the underlying implementation of your text editor.public Node tail this field will hold a reference to the tail of the doubly linked list that is used in the underlying implementation of your text editor.public Node cur this field will hold a reference to the node that is after the current cursor index or null when curPos numCharspublic void moveRight this method should move the cursor one character to the right. As shown in the above example for the string blue, this should update both cur and curPos. This method should not throw an exception if the user tries to move right when curPos numChars. The movement should be ignored.public void moveLeft this method should move the cursor one character to the left. As shown in the above example for the string blue, this should update both cur and curPos. This method should not throw an exception if the user tries to move left when curPos The movement should be ignored.public void moveToHead this method shove move the cursor to the first character in the editor ie to curPos public void moveToTail this method shove move the cursor to the end of the string in the text editor ie to curPos numCharsNote that curPos should remain in the interval numChars when any of the methods above are called.Since you are now familiar with all fields you will need to implement this class, you can write the default constructor which should initialize the Editors fields to signify that it has no characters in itpublic Editor this method constructs an empty Editor. Make sure you initialize all your fields in the constructor.III. InsertionOne reason why a linked list should be used over an array in the implementation of this text editor is because the user should be able to insert characters at any cursor index in the editors string. If one were to use an array, a great deal of shifting would have to occur every time a character is inserted. For this text editor, insertions will always be done before the Node referenced by cur.Consider the example string blue from section I. If one wanted to insert $ after b then cur would have to reference the Node with data l and curPos The resulting doubly linked list should then be:However, there is still the question, what should the values of cur and curPos be after the insertion of $ Suppose we were to then insert the character @ We would want it to follow $ in the resulting string. That, is the new doubly linked list would be: Since insertions must occur before the Node referenced by cur, this would mean cur would still reference the Node with data l However, the Node with data l now has nodes b $ before it Therefore, the cursor index is no longer It is With cur remaining referencing the Node with l and curPos has been properly updated to we can insert @ into the doubly linked list before cur. This insertion will cause curPos to be incremented again as cur now has nodes before it Therefore, following the insertions of $ and @ into the list cur remains referencing the Node with l and curPos This should allow you to implement the required method insert below:public void insertchar c this should insert the provided character before curinto the doubly linked list. This includes insertion after the last Node in the list. Insertions should result in the fields head, tail, and numChars described in section IIshould be updated appropriately in addition to curPos.IV Delete and BackspaceAnother reason that a linked list should be used in place of an array in the implementation of a text editor is because the user should be able to remove characters from any cursor index in the editors string. If one were to use an array, a great deal of shifting would have to occur every time a character is removed. For this text editor, removal can be done in two ways via delete and backspace. Using delete will remove the Node referenced by cur from the doubly linked list. On the other hand, using backspace will remove the Node before the Node referenced by cur from the doubly linked list.Suppose that we wanted to remove the character l from the example string blue in section I. This could be done using delete or backspace. To delete l cur should reference the Node containing l and curPos to remove l using the delete key on a keyboard. This is because the cursor would visually be between b and l at cursor index The resulting doubly linked list following the deletion of l should be:Consider this as a realworld text editor, the visual cursor should still be between b and u This would mean a future delete would remove u Therefore, following the deletion of l cur should reference the Node containing u However, the cursor still only has character b before it Therefore, curPos should remain as A following deletion of u would result in cur referencing the Node with e and curPos remaining as One can also remove l using backspace. To backspace l cur should reference the Node containing u and curPos to remove l using the backspace key on a keyboard. This is because the cursor would visually be between l and u at cursor index The resulting doubly linked list following the backspace of l is the same as the list shown above.Following the backspace of l the visual cursor should still be between b and u This would mean a future backspace would remove b Therefore, following the backspace of l cur should remain referencing the Node containing u However, the cursor now has only one Node before it with data b Before, when the cursor was after l there were two Nodes b l before the cursor. Hence, curPos must be decremented from to A following backspace of b would result in cur still referencing the Node with u and curPos decremented to This should allow you to implement the two required methods:public void delete this method should remove the Node referenced by cur. If the user calls delete when the cursor has no following characters, you should not throw an exception. The movement should be ignored.public void backspace this method should remove the Node before the Node referenced by cur. If the user calls backspace when the cursor has no preceding characters, you should not throw an exception. The movement should be ignored.Removals via delete or backspace should result in the fields head, tail, and numChars described in section II should be updated appropriately in addition to cur and curPos.V Second Constructor & Remaining MethodsThe text editor has two modes. The first is where the editor has no characters in it The constructor relating to this is the default constructor which was presented in section II The second mode is where the editor can be loaded with characters from a text file. Thus, you are required to implement the following constructor:public EditorString filepath this should construct an Editor with the characters from a text file at the provided file path. To do this, you will be required to iterate over the text file character by character and insert them into the Editor. To do this, you will be required to use: java.util.Scanner and java.ioFile.Notes: You will find that the method in the skeleton has already been declared with a throws clause which you should leave there. You do not need to use trycatch. The cursor should be after the last character in the input file not on a new line after this constructor executesThere are three final methods that you are required to implement for this class:public String toString this method should return a String concatenation of all the characters stored in the text editor. If the Editor contains the characters b l u and e then toString should return blue.public void clear this method should remove all of the characters stored in the text editor. Hint: This can be done in O time, but make sure that you update all relevant fields.public void saveString savepath this method should save the contents of the text editor to a file at the provided save path. To do this, you will be required to use: java.ioPrintStream and java.ioFileNote: you will find that the method in the skeleton has already been declared with a throws clause which you should leave there. You do not need to use trycatch.Using the GUIWhile we recommend that you use JUnit tests to thoroughly test your code, you can utilize the provided GUI to see your text editor in action. To run the editor on an empty input file, uncomment line of EditorMain.java. A resizable GUI window will then open that uses your Editor in the background. To insert characters including new lines just type the desired characters. In the previous pages, delete means to remove the character after the cursor.o On Windows, use the delete key on the keyboard.o On Mac, use Ctrl D Furthermore, backspace means to remove the character before the cursor.o On Windows, use the backspace key on the keyboard.o On Mac, use the delete key. To move left and right, use the left and right arrow keys. To jump to the beginning of the text, use Ctrl H To jump to the end of the text, use Ctrl E To clear the contents of the editor, use Ctrl K To save the contents of the editor, use Ctrl S Then, you can type the name of the savefile in the text area below the text displayed on the window and press enter.o If you do not provide a full system file path, the file will be in the same folder asyour Eclipse project.o If you close the window without saving, your changes will be lost.These features will allow you to test most of the methods of the editor class. Furthermore, you can uncomment lines or of EditorMain.java to create the GUI with some text loaded from an input text file and test the second Editor constructorIMPORTANT NOTES For the classes in which specific fields are included, you must be using these fields and leaving them as public. Make sure that you have implemented all of the methods listed on pages through in Editor.java. You should use the provided JUnit tests in ExampleEditorTests.java to test your code based on the examples on this document and to ensure you understand the basic functionality of the Editor. However, you need to write more rigorous tests considering more advanced operations and edge cases of the various methods in the Editor class in the empty file StudentEditorTests.java. Hint: some of those edge cases have been mentioned in the implementation details of the Editor class. You cannot be using any Java provided data structures. The only classes you need for the implementation of Editor.java are Node, File,PrintStream, and FileNotFoundException which you can find in the skeleton.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started