Find Jobs
Hire Freelancers

359113 java - editor class

N/A

In Progress
Posted over 14 years ago

N/A

Paid on delivery
This assignment consists of two parts: 1. To implement a line editor using [login to view URL] 2. To write a Graphic User Interace program for the line editor using Event-Oriented Programming Part 1: Implementation of Editor class Overview A line editor program manipulates a text, line by line. We assume that the editor allows at most 30 characters in each line. The first line of the text is thought of as line 1. There are five functions of this editor: insert, delete, replace, moveCursorTo and undo. EditorADT interface Interface EditorADT declares these functions and you will program class called Editor which implements EditorADT. public interface EditorADT { public void delete(int m, int n); public void insert(String s); public void moveCursorTo(int m); public void replace(String oldValue, String newValue); public void undo(); } public class Editor implements EditorADT You will program a class called Editor that implements EditorADT. Since we often make deletions and insertions with an editor, linked list is chosen as the underlying data structure. More specifically, [login to view URL] is the data structure of this line editor. Instance Variables of Editor class * private LinkedList<String> text A reference to a LinkedList. When a line is inserted, the line is added to this linked list. For example, the first and second lines of the text are Yesterday, upon the stair, I shot an arrow the air. Then, Yesterday, upon the stair, and I shot an arrow the air. becomes the first and second elements of the linked list, respectively. * private ListIterator <String> current A listIterator that keeps track of the current line. Initially, there is no current line in a text. * text and current are [login to view URL] needed, you can declare more instance variables in this class. Method Specification 1. public void delete(int m, int n); current is positioned at line m and lines m through n will be removed. After deletion, the line after line n will be the current line. 2. public void insert(String s); The parameter string is the new line to be inserted. If there is no current line in the text, the line is attached to the end of the text. Otherwise, the new line will be added before the current line. 3. public void moveCursorTo(int m); current is positioned at m. 4. public void replace(String oldValue, String newValue); All the occurrences of oldValue is replaced by newValue in the current line. 5. public void undo(); This method will undo the one step preceding function. 6. public String display(); It returns a String that represents the text. Suppose there are two lines in the text, Yesterday, upon the stair, I shot an arrow the air. and the second line is the current line. Then it returns a String " Yesterday, upon the stair, \n > I shot an arrow the air.", where \n is a new line character and > is a mark of the current line. Although display() is not the one of the basic editing functions, you may want to use it when you write the Part II. Note: you may want to test [login to view URL] using your own test program before moving on to part 2. Here is a simple tester and the expected output . Part 2: [login to view URL] Reading: * Appendix C of the text book: Event-Oriented Programming or * Chapter 12.1 ~ 12.3 of Java Concepts 4th edition, Cay Horstmann: Event Handling In the part 2, you will write a program that provides the user with a nice GUI interface to the editor. The EditorUI program will be using event-oriented programming. Download the skeleton of the class from here . 1. User manual of EditorUI First of all, assume the user always enters a correct input. The EditorUI program diplays five Buttons for basic editor functions, one TextField and one TextArea. If the user wants to * insert a line, the user writes the line in the TextField and clicks on the Insert button. To insert a line at a specified position, the user first moves the cursor to the desired position before insertion. * delete a line, the user specifies two integers m and n in the form of m:n in the TextField and clicks on the Delete button. Then, current is positioned at m and lines m through n will be deleted. After deletion, the next line to line n becomes current line. If there is no such a line, there will be no current line in the text. * replace a word(s) in a line, the user should first move the cursor to the line to make it current line. In the TextField, the input is entered in the form of oldValue$newValue. When the user clicks on the Replace button, all the occurrences of oldValue will be replace by newValue in the current line. * undo the one step preceding function, the user clicks on the Undo button. * moveCursorTo to the desired position, the user enters the line number in the TextField and clicks on the MoveCursorTo button. To see how the program is supposed to work, download this. Run SampleUI, the GUI program of the line editor. 2. Structure of [login to view URL] Instance Variables * private Editor editor; // the Editor object * private JTextField elementText; // TextField where the user enters input * private JTextArea feedback; // TextArea where the text is displayed. Note that the text field and the text area are the intance variables because they will be accessed by the methods of this class. However, as we will see later, Buttons are locally defined and created within the constructor because they are not accessed by other methods. Constructor public EditorUI() The constructor 1. initializes the instance variables 2. creates a frame and a panel to GUI components. The codes are given in the skeleton. 3. creates buttons and add them to the panel. The text book has an example at ppp. 767 ~ pp. 768. 4. adds the textfield and text are to the panel. 5. adds the panel to the frame and configures the frame. The codes are given in the skeleton. private inner classes - action listeners The purpose of an action listener is to describe the actions that are carried out when a button is pushed. You need to place each action listener definition within the EditorUI class(as an inner class), so that the action listener can access the instance variables of the EditorUI class. The names of the action classes are listed in the first column of the following table. Kind of Action Listener Describes the actions to be taken when the following button is clicked. DeleteListener deleteButton InsertListener insertButton MoveCursorToListener moveCursorToButton ReplaceListener replaceButton UndoListener undoButton Here is a skeleton of the InsertListener which handles the clicking of the insertButton. class InsertListener implements ActionListener { public void actionPerformed(ActionEvent event) { //your work goes here. } } The following describes the actions that the listeners should take when the corresponding buttons are clicked. InsertListener: It gets a line the TextField and inserts it to the text. It also flushes the TextArea and displays the text with line number including the newly added line. If there is current line, '<' marks the line. DeleteListener: It gets the user input m:n from the TextField, and deletes the specified lines from m to n. It also flushes the TextArea and displays the text excluding the removed lines. MoveCursorToListener: It gets a user input m from the TextField and makes the line m to be the current line of the text. It also flushes the TextArea and displays the text marking the current line with >. ReplaceListener: It gets the user input oldValue$newValue from the TextField and replaces oldValues with newValue in the current line. It flushes the TextArea and displays the text with replaced words. The current line will be marked by '>' UndoListener: It will undo the one step preceding function. It flushes TextArea and displays the previous text on the TextArea. Other methods * private void updateFields() is given. * public static void main (String [] args) is given. * If you wish, you may define and use your own private methods.
Project ID: 2104947

About the project

Remote project
Active 12 yrs ago

Looking to make some money?

Benefits of bidding on Freelancer

Set your budget and timeframe
Get paid for your work
Outline your proposal
It's free to sign up and bid on jobs

About the client

Flag of UNITED STATES
Hayward, United States
5.0
1
Member since May 24, 2009

Client Verification

Thanks! We’ve emailed you a link to claim your free credit.
Something went wrong while sending your email. Please try again.
Registered Users Total Jobs Posted
Freelancer ® is a registered Trademark of Freelancer Technology Pty Limited (ACN 142 189 759)
Copyright © 2024 Freelancer Technology Pty Limited (ACN 142 189 759)
Loading preview
Permission granted for Geolocation.
Your login session has expired and you have been logged out. Please log in again.