Question: Hi, I'm having some trouble solving this code, specificaly the RESEQUENCE command. How would I go about writing that particular command? For this lab, you
Hi, I'm having some trouble solving this code, specificaly the RESEQUENCE command. How would I go about writing that particular command?
For this lab, you are going to write a simple-minded text editor. It should repeatedly prompt the user to enter a line of text. If the line begins with an integer, the line will be entered into a linked list in order sorted by the line number used. If there is no number, the first word entered should be interpreted as a command. To begin with, I would like you to implement the following commands: LIST - this should list the current lines READ - should read data from a text file SAVE - should save data to a text file RESEQUENCE - re-number all the lines starting from 10 and incrementing the line numbers by 10. EXIT QUIT - synonyms that cause the program to exit. I am including a skeleton for the program, including the code for the parts that may be using Java features that you have not seen. The skeleton will indicate what code you need to write. You may (and should) use the class I wrote for the course LLComp.java which itself extends LL.java. You will find those in the directory that the home page makes available for you. SKELETON: import java.util.Scanner; import java.io.*; public class Editor { private LLComp theText; private String prompt; private enum Keywords {READ, SAVE, LIST, RESEQUENCE, QUIT, EXIT, UNDEFINED;}; private Scanner console; public Editor() { this.theText = new LLComp(); this.prompt = ">"; this.console = new Scanner(System.in); } public String getPrompt() { } public void setPrompt(String p) { } private static boolean isInt(String s) // see if a string represents { // an integer. boolean retval = false; try { Integer.parseInt(s); retval = true; } catch (NumberFormatException e) { retval = false; } return retval; } public void process() { boolean done = false; String line; while (!done) { System.out.print(this.prompt); line = console.nextLine().toUpperCase(); // Work only with upper case String splitString[] = line.split(" ", 2); // at this point, the line that was read in has been split into two // arrays. splitString[0] contains the first token, splitString[1] // contains all the rest of the line. //At this point, you need to decide whether this is a command or //a line of text to be entered. if (this.isInt(splitString[0])) { // Here we have a line of text to be entered. Write the code to //insert it into the LLComp named theText. } else //otherwise, it is a command, so call doCommand to perform it. done = this.doCommand(splitString[0]); } } private boolean doCommand(String com) { boolean retval = false; Keywords command; //This first bit takes the string in the first word of the line //and turns it into one of the manifest constants of the //enumerated data type. This makes it fairly easy to add new //commands later. try { command = Keywords.valueOf(com);// command is a Keywords and can } // can be used as the target of a switch. catch (IllegalArgumentException e) { command = Keywords.UNDEFINED; //An undefined Keywords will cause } //an exception. switch (command) { case READ: this.read(); break; case SAVE: this.save(); break; case LIST: this.list(); break; case RESEQUENCE: this.resequence(); break; case QUIT: case EXIT: retval = true; break; case UNDEFINED: System.out.println("Undefined command:" + com); } return retval; } // You need to implement the following routines. private void read() { } private void save() { } private void list() { } private void resequence() { } public static void main(String args[]) { Editor e = new Editor(); e.process(); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
