Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Pt:2 Please program in JAVA: Program each option such as delete line and such. Your program will be a line editor . A line editor

Pt:2

Please program in JAVA: Program each option such as delete line and such.

Your program will be a line editor. A line editor is an editor where all operations are performed by entering commands at the command line. Commands include displaying lines, inserting text, editing lines, cutting and pasting text, loading and saving files. For example, a session where the user enters three lines of text and saves them as a new file may appear as:

 Menu: m Delete line: dl Load file: l Delete range: dr Show all: sa Copy range: cr Show line: sl Paste lines: pl Show range: sr Write to file: w New line: nl Quit: q Edit line: el Write and quit: wq -> nl type line? (y/n): y 1: this is the first line type line? (y/n): y 2: and a second line type line? (y/n): y 3: and this is the third line type line? (y/n): n -> w write to file: test.txt -> sa 1: this is the first line 2: and a second line 3: and this is the third line -> q 

The file created is:

this is the first line and a second line and this is the third line 

The editor will first display a menu after which it will prompt for commands with "-> ". The commands which can be entered can be seen in the menu appearing in the example.

This web page was written with the line editor described here.

The Commands

If you run the program with no command line arguments it will begin with an empty document. If you give the name of an existing file as a command line argument it will open and load the contents of that file. You can exit the program with either the quit, q, or write and quit, wq, command.

Displaying the menu

The menu is displayed when the program starts. It can be displayed with the command m.

Reading and writing files

The load file command l will ask the user for a file name, open the file for reading, read the file contents into the editor and close the file. If there is already text in the editor it is discarded. The write file command w will write the contents of the editor to a file. If a file has previously been opened with the read file command that same file is used for writing. Otherwise, the program will prompt the user for a file name. For either the read or write file commands if the requested file cannot be opened an appropriate error message is displayed.

Displaying text

Text can be displayed with three commands: show all (sa), show range (sr), and show line (sl). All three commands display text with line numbers. Show all displays the entire document. Show range will ask the user a range of lines ("from" and "to") and will display the lines in that range. If the "to" line number is greater than the number of lines in the document lines up to the end of the document will be displayed. Show line will ask the user for a line number and display that line.

Entering new lines of text

New lines of text are entered using the new line command nl. Before entering a new line the program will ask type line? (y/n). If the user chooses yes the line will be accepted after the line number is displayed. If the document is empty the new line will be the first line (line 1) of the document. If the document is notempty the program will prompt the user to enter the line after which the new line will be placed. If the line number is out of range the program will not accept a new line. A new first line can be added by asking to add a line after (the non-existent) line 0. After the user has entered a line the program will continue asking for new lines until the user answers no. As the new lines are being entered they are added sequentially to the document. As the user begins to enter lines at the new line command the line after which the new line is being added is first displayed (unless the user is entering lines at the beginning of the document). Examples:

-> sa 1: this is the first line 2: and a second line 3: and this is the third line -> nl insert after line number: 1 inserting after: this is the first line type line? (y/n): y 2: one and a half type line? (y/n): y 3: one and three quarters type line? (y/n): n -> sa 1: this is the first line 2: one and a half 3: one and three quarters 4: and a second line 5: and this is the third line -> 

The buffers

There are two buffers, the line buffer and the string buffer, used for moving blocks of text. The line buffer holds ranges of entire lines and the string buffer holds substrings of individual lines. Whenever a new range of lines is copied to the line buffer or a string is copied to the string buffer the previous contents of that buffer are deleted (but the other buffer is unaffected).

Moving ranges of lines

Blocks of lines can be moved using copy range, cr, and paste lines, pl. Copy range will ask the user for a range of line numbers ("from" and "to") and will copy the lines to the line buffer - the lines remain unchanged in the document. Paste lines will ask the user to enter the number of the line after which the lines from the line buffer will be pasted. The lines are then copied into the document - the line buffer is left unchanged. Ranges of lines are deleted with delete range, dr. Again, the user is asked for the range of line numbers to delete. The lines are then deleted (the line buffer is unchanged).

Editing a line of text

The edit line command, el will ask for the number of a line to edit then display the line preceded by a scalewhich allows the user to identify positions in the line by number (beginning with 0). Then the line menu is displayed and the program prompts for input.

-> el line number: 3 0 5 10 15 20 |----+----|----+----|- one and three quarters Show line: s Copy to string buffer: c Cut: t Paste from string buffer: p Enter new substring: e Delete substring: d Quit line: q -> 

The scale is at least as long as the line displayed. Operations chosen from the line menu will then operate on this chosen line until quit line, q.

Show line will display the line (again) with the scale. Quit returns to the main editor menu. The remaining commands edit the chosen line.

Copy to string buffer and Paste from string buffer work like copy range and paste lines but they work with the string buffer rather than the line buffer. Also, Paste from string buffer will insert the substring so that it begins at the specified position. (Thus it does an "insert before" where paste lines does an "insert after.")

-> c 0 5 10 15 20 |----+----|----+----|- one and three quarters from position: 4 to position: 7 copied: 0 5 10 15 20 |----+----|----+----|- one and three quarters ^^^^ Show line: s Copy to string buffer: c Cut: t Paste from string buffer: p Enter new substring: e Delete substring: d Quit line: q -> p 0 5 10 15 20 |----+----|----+----|- one and three quarters insert at position: 14 0 5 10 15 20 25 |----+----|----+----|----+ one and three and quarters Show line: s Copy to string buffer: c Cut: t Paste from string buffer: p Enter new substring: e Delete substring: d Quit line: q -> 

Delete substring, t, will prompt for a range of characters to delete, will display the string with the substring underlined, ask the user if the range is ok, and then delete the substring.

-> el line number: 1 0 5 10 15 20 |----+----|----+----|- this is the first line Show line: s Copy to string buffer: c Cut: t Paste from string buffer: p Enter new substring: e Delete substring: d Quit line: q -> d 0 5 10 15 20 |----+----|----+----|- this is the first line from position: 12 to position: 18 delete: 0 5 10 15 20 |----+----|----+----|- this is the first line ^^^^^^^ y/n: y 0 5 10 15 |----+----|----+ this is the ine 

Cut, t, combines copy to string buffer and delete substring. It copies the substring to the string buffer and removes it from the line.

Enter new substring, e, will allow the user to type new text into an existing line.

line number: 1 0 5 10 15 20 |----+----|----+----|- this is the first line Show line: s Copy to string buffer: c Cut: t Paste from string buffer: p Enter new substring: e Delete substring: d Quit line: q -> e 0 5 10 15 20 |----+----|----+----|- this is the first line insert at position: 12 text: very very 0 5 10 15 20 25 30 |----+----|----+----|----+----|- this is the very very first line 

Internals

You will define (among others) two classes: a Line class and a Document class. The Line class will hold the text of a line from the document in a field of type String and provide basic operations on a line. Some of these operations are insert a string into the line at a specified position, delete a substring, copy a substring, etc. The Line class will also have a field with the number of characters stored in the line. The Document class will hold an entire document (each line in one String) and provide basic operations such as inserting lines, deleting, copying, and pasting ranges of lines. It will also have a field with the number of lines currently in the document. Elsewhere in your program you will provide code for the operations (chosen by the user from the menu) which will then call the member functions of the Line and Document classes.

The document will be a doubly linked list of lines (Strings) with a dummy node. I.e., if there are 100 lines in the document there will be 101 nodes in the linked list - 100 holding text and one dummy node. This will make insertions and deletions much easier by eliminating special boundary conditions. Line numbers will not be stored with lines. Instead, every time a line number is needed (e.g. for display in show range and show all, finding positions for new line, etc.) it will be found by traversing the list and counting.

All strings will be Java Strings. Lines of text can be manipulated with concatenation and the substring methods:

String substring(int beginIndex) // Returns a new string that is a // substring of this string. String substring(int beginIndex, int endIndex) // Returns a new string // that is a substring of // this string. 

To accept a new line of user input you must use nextLine since reading with next stops accepting input at the first white space and text lines may contain white space. You must usenextLine for all keyboard input: nextLine does not mix well with next, nextInt, etc., so to read integer input you will first read a String and then convert it to an integer using Integer.parseInt.

You will do appropriate input validation. When choosing operations (in response to the "->" prompt) your program will only accept input as stated in the menu. When accepting integer input (line numbers, character positions in a line) your program will check for valid numbers and ranges.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions