Question

JAVA DATA STRUCTURES

Write a line-based text editor. The command syntax is similar to the Unix line editor ed. The internal copy of the file is maintained as a linked list of lines. To be able to go up and down in the file, you have to maintain a doubly linked list. Most commands are represented by a one-character string. Some are two characters and require an argument (or two). Support the commands shown below:

Command Function Go to the top. Add text after current line until on its own line Delete current line Delete several lines. C

0 0
Add a comment Improve this question Transcribed image text
Answer #1

TextEditor.java
import java.util.Iterator;
import java.io.*;

import WattBrown.LinkedList;

public class TextEditor {

// Each TextEditor object represents a text subject to editing. The text
// consists of lines numbered 0, 1, �. If the text is nonempty, at any time
// one line is selected, initially line 0.

// This text is represented as a list of lines, text. The number of the
// currently-selected line (or -1 if the text is empty) is held in selection.
private LinkedList text;
private int selection;

public TextEditor () {
// Construct a text, initially empty.
text = new LinkedList();
selection = -1;
}

public void insertBefore (String line) {
// Insert line immediately before the selected line in this text.
if (selection < 0) {
warn("There is no line to insert before."); return;
}
text.add(selection++, line);
}

public void insertAfter (String line) {
// Insert line immediately after the selected line in this text, and select the
// inserted line.
text.add(++selection, line);
}

public void delete () {
// Delete the selected line in this text, and select the following line (or the
// previous line, if the last line was deleted).
if (selection < 0) {
warn("There is no line to delete."); return;
}
text.remove(selection);
if (selection == text.size())
selection--;
}

public void replace (String line) {
// Replace the selected line in this text by line, and select the replacement
// line.
if (selection < 0) {
warn("There is no line to replace."); return;
}
text.set(selection, line);
}

public void select (int ln) {
// Select the line numbered ln in this text.
if (ln < 0 || ln >=text.size())
warn("There is no such line to select.");
selection = ln;
}

public void find (String s) {
// Find the first line, between the selected line and the end of this text, that
// contains s as a substring, and select that line.
if (selection < 0) {
warn("There are no lines to search."); return;
}
int length = text.size();
for (int ln = selection; ln < length; ln++) {
String line = (String) text.get(ln);
if (line.indexOf(s) >= 0) {
selection = ln;
return;
}
}
warn("There is no line containing '" + s + "'.");
}

public void load (BufferedReader input)
throws IOException {
// Insert the text contained in input after the last line of this text, and
// select the last line inserted.
for (;;) {
String line = input.readLine();
if (line == null) break;
text.add(line);
}
selection = text.size()-1;
}

public void save (BufferedWriter output)
throws IOException {
// Write this text to output.
Iterator lines = text.iterator();
while (lines.hasNext()) {
String line = (String) lines.next();
output.write(line + '\n');
}
}

public void display () {
// Display this text, highlighting the selected line.
int ln = 0; // � line number
Iterator lines = text.iterator();
while (lines.hasNext()) {
String line = (String) lines.next();
boolean selected = (ln == selection);
System.out.println(ln + (selected ? " * " : " : ") + line);
ln++;
}
}

private void warn (String msg) {
System.out.println("WARNING: " + msg);
}

public static void main (String[] args) {
try {
TextEditor editor = new TextEditor();
for (int i = 0; i < args.length; ) {
char op = args[i++].charAt(0);
System.out.println("Operation: " + op);
switch (op) {
case 'i':
editor.insertBefore(args[i++]);
break;
case 'a':
editor.insertAfter(args[i++]);
break;
case 'd':
editor.delete();
break;
case 'r':
editor.replace(args[i++]);
break;
case 's':
editor.select(Integer.parseInt(args[i++]));
break;
case 'f':
editor.find(args[i++]);
break;
case 'l': {
BufferedReader input =
new BufferedReader(
new InputStreamReader(
new FileInputStream(args[i++])));
editor.load(input);
input.close();
break;
}
case 'v': {
BufferedWriter output =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(args[i++])));
editor.save(output);
output.close();
break;
}
default:
System.out.println(" -- INVALID");
}
editor.display();
}
}
catch (IOException e) { System.out.println(e.getMessage()); }
}

}

Add a comment
Know the answer?
Add Answer to:
JAVA DATA STRUCTURES Write a line-based text editor. The command syntax is similar to the Unix...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Execute the following UNIX commands and paste the results in a text editor like Microsoft Word...

    Execute the following UNIX commands and paste the results in a text editor like Microsoft Word Create a directory using “mkdir” and name the directory “test” Change directory to test In the test directory, create a file using nano and name the file “NewDoc.c” Use “cd ..” Execute “ls” Create a file “temp.c” using nano Delete “temp.c” using the “rm” command. Execute each command in the question, copy and paste the result from the terminal window into Microsoft word or...

  • Create working Unix syntax commands for the following and answer the questions below. Answer the following...

    Create working Unix syntax commands for the following and answer the questions below. Answer the following simple questions. List at least four commonly used shells. How would you start editing the file first-file using the pico (or nano) editor? Write down the pico/nano command that will: Move to the beginning of the line. Move to the end of the line. Move back one page. Cut selected text. Exit pico. Name two other common Unix text editors besides pico/nano.

  • In a linux command line Give AWK commands for accomplishing each of the following: Print the...

    In a linux command line Give AWK commands for accomplishing each of the following: Print the 2nd last field (the field directly before the last field) of each line from a file named 'last.txt' Assume you have a file called 'names' that contains a list of people, one person per line. Also, assume that on each line the 3rd field on that line contains the age of the person. Some of the people do not have an age listed, and...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Write a program Adder.java that calculates and prints the sum of command line parameters, so if...

    Write a program Adder.java that calculates and prints the sum of command line parameters, so if you make the command line parameters be 2 5 22 then the program prints 29. Write a program ShowFiles.java that assumes the command line parameters are each a text file name directly readable by the program. For each file named, print out the three heading lines in the format shown, and then each line of the file contents. If file q.txt contains What is...

  • Vim and Unix Questions In a vim window, how do you set a tab to be...

    Vim and Unix Questions In a vim window, how do you set a tab to be two spaces wide? In a vim window, how do you, with a simple and single command, move the cursor to 5 lines below (don’t worry about the actual number of lines) and do nothing else? In a vim window, how do you, with a simple and single command, assuming your cursor is at the beginning of the word, delete the current word and do...

  • JAVA Primitive Editor The primary goal of the assignment is to develop a Java based primitive...

    JAVA Primitive Editor The primary goal of the assignment is to develop a Java based primitive editor. We all know what an editor of a text file is. Notepad, Wordpad, TextWrangler, Pages, and Word are all text editors, where you can type text, correct the text in various places by moving the cursor to the right place and making changes. The biggest advantage with these editors is that you can see the text and visually see the edits you are...

  • LINUX 140U Create a file named script1.sh that contains a bash script that: Accepts NO command...

    LINUX 140U Create a file named script1.sh that contains a bash script that: Accepts NO command line arguments and does NOT use the read command. I will give you no credit if your bash script includes a read command. Also, you need to have at least one for..in loop in your code even if there may be other ways to do this problem. The script will go through a list of entries in the current directory (do not process subdirectories):...

  • do numbers 4-8 4. Given any directory, use the Is command to display: • all files...

    do numbers 4-8 4. Given any directory, use the Is command to display: • all files and sub-directories starting with the letter "D" (note do not list anything in any sub-directory) • its immediate sub-directories (sub-directories only, and no other ordinary files) its immediate hidden sub-directories only - take a screenshot (#3-3) that clearly shows the command and the result. 5. Assume that the following files are in the working directory: $ ls intro notesb ref2 section 1 section3 section4b...

  • Java program Program: Grade Stats In this program you will create a utility to calculate and...

    Java program Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT