This is my first time posting here.
So, if my question is too large or inappropriate, please
let me know or refer me somewhere that is better.
I am still new to
Java. I'm taking an online course in Data Structures
(my 2nd course) and having trouble with my 3rd project which is to:
code a linked list class and a
string editor class according to the following specifications below. I should also mention that the string editor is used by a StringEditorGUI class that contians the main method - below.
I think I can figure out some of the LinkedList class but I'm kind of confused about how to approach the rest of the project because I've never used a StringEditorGUI. I am particularly confused with how the string editor works and writing the methods for the string editor. I'm not asking someone to do anything for me here, but can someone take a look and give me some direction or at least a better overall view of how these classes are supposed to work with eachoter?
Thanks,
MB
CIS256 Project #3
The MyList Class
For all linked list needs in this project you are to use the MyList class. You are to code this class as a singly list. The MyList class is to provide the following methods:
Effect: new MyList object created
Precondition: None
Postcondition: an empty MyList object exists
public MyList() Effect: new MyList object created that is copy of lst
Precondition: lst is not null
Postcondition: an MyList object that is identical to lst exists
public MyList(MyList lst) Effect: determines if there are any objects in the list
Precondition: None
Postcondition: the list is unchanged
Returns: true if there are no objects in the list, false otherwise
public boolean isEmpty() Effect: the list will no longer contain any objects
Precondition: None
Postcondition: the list contains no objects
public void makeEmpty() Effect: a new node is added to the list
Precondition: None
Postcondition: ch is contained in a node at the end of the list
public void insertAtEnd(Object ch) Effect: the first node is removed from the list
Precondition: this list is not empty
Postcondition: the list contains one fewer nodes
Returns: the data that was contained in the deleted node
public Object deleteFirst() Effect: the last node is removed from the list
Precondition: this list is not empty
Postcondition: the list contains one fewer nodes
Returns: the data that was contained in the deleted node
public Object deleteLast() Effect: a new node is added to the front of the list
Precondition: None
Postcondition: ch is contained in a node at the front of the list
public void insertAtFront(Object ch) Effect: a string representation of this object is created
Precondition: None
Postcondition: no changed is made to the list
Returns: a string containing data items from the list
an empty string is returned if the list is empty
public String toString() The StringEditor class A StringEditor object represents a string, with a cursor position. You are to implement the StringEditor class using two MyList objects for instance variables. One list, let�s call it �leftChars�, contains all the characters which are to the left of the cursor. The other list, call it �rightChars� contains all the characters to the right of the cursor. You will need to keep in mind that although you want to store �chars� in your list, MyList can only store objects, so the wrapper class Character would be handy.
Methods:
Effect: StringEditor object is created
Postcondition: no characters are in the string
public StringEditor() Effect: StringEditor object is created
Precondition: parameters strings are non-null (not non-empty)
Postcondition: characters from strings left and right are
represented in the editor
public StringEditor(String left, String right) Effect: determines if the cursor is at the end of the string
Precondition: None
Postcondition: object is unchanged
Returns: true if the cursor is at the front of the string
public boolean isCursoratfront() Effect: determines if the cursor is at the end of the string
Precondition: None
Postcondition: object is unchanged
Returns: true if the cursor is at the end of the string
public boolean isCursoratend() Effect: insert new character at cursor position
Precondition: None
Postcondition: cursor should follow new character
public void insertChar(char c) Effect: removes the character directly following the cursor
Precondition: None
Postcondition: if there was a character directly following the
cursor, it has been removed from the string
public void delete() Effect: removes character directly prior to cursor
Precondition: None
Postcondition: if there was a character directly prior to the
cursor, it has been removed from the string
public void backspace() Effect: cursor is moved to the beginning of the string
Precondition: None
Postcondition: if cursor ws not already there, it is now at the
front of the string
public void frontOfString() Effect: cursor is moved to the end of the string
Precondition: None
Postcondition: if cursor was not already there, it is now at the
end of the string
public void endOfString() Effect: moves the cursor one position to the left
Precondition: None
Postcondition: If the cursor was not at the end of the string,
it has been moved one character to the left
public void moveCursorLeft() Effect: moves the cursor one position to the right
Precondition: None
Postcondition: If the cursor was not at the end of the string,
it has been moved one character to the right
public void moveCursorRight() Effect: returns a String with characters an a visible �cursor�
For example: [how now brown^
cow]
Empty string: [^]
Precondition: None
Postcondition: StringEditor object is unchanged
public String toString() The Application
An application has been provided which uses a StringEditor object to allow the user to enter a string, and then perform the following operations on that string:
-insert a character at the cursor position
-move the cursor to the right one position
-move the cursor to the left one position
-move the cursor to the front of the string
-move the cursor to the rear of the string
-delete the character directly following the cursor
-backspace (remove) the character directly preceding the cursor
Use it to
test your classes thoroughly.
/*
* StringEditorGUI.java
*/
// CIS 256 StringEditor test program
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class StringEditorGUI extends JFrame implements ActionListener {
// create buttons and output frame
private JButton buttonSet = new JButton("Set...");
private JButton buttonInsert = new JButton("Insert...");
private JButton buttonDelete = new JButton("Delete");
private JButton buttonBackspace = new JButton("Backspace");
private JButton buttonHome = new JButton("Home");
private JButton buttonPrev = new JButton("<");
private JButton buttonNext = new JButton(">");
private JButton buttonEnd = new JButton("End");
private JTextArea textOutput = new JTextArea(12, 20);
// create empty StringEditor
private StringEditor ed = new StringEditor();
static public void main(String [] args) {
new StringEditorGUI().setVisible(true);
}
public StringEditorGUI() {
setTitle("StringEditorGUI");
//Terminate application when user clicks 'X'
setDefaultCloseOperation(EXIT_ON_CLOSE);
//adjust output frame
textOutput.setLineWrap(true);
textOutput.setEditable(false);
//attach listener to each button
buttonSet.addActionListener(this);
buttonInsert.addActionListener(this);
buttonDelete.addActionListener(this);
buttonBackspace.addActionListener(this);
buttonHome.addActionListener(this);
buttonPrev.addActionListener(this);
buttonNext.addActionListener(this);
buttonEnd.addActionListener(this);
// set layout and add Textarea to frame
setLayout(new BorderLayout());
add(new JScrollPane(textOutput), BorderLayout.CENTER);
// create panel, add buttons, and put it on frame
JPanel panelControls = new JPanel();
panelControls.add(buttonSet);
panelControls.add(buttonInsert);
panelControls.add(buttonDelete);
panelControls.add(buttonBackspace);
panelControls.add(buttonHome);
panelControls.add(buttonPrev);
panelControls.add(buttonNext);
panelControls.add(buttonEnd);
add(panelControls, BorderLayout.SOUTH);
pack();
// display StringEditor
textOutput.setText(ed.toString());
Dimension size = getSize();
Dimension sizeScreen = Toolkit.getDefaultToolkit
().getScreenSize();
setLocation(sizeScreen.width/2 - (size.width/2),
sizeScreen.height/2 - (size.height/2));
}
//handle the button event which was performed
// by calling appropriate StringEditor method
public void actionPerformed(ActionEvent ae) {
Object objSource = ae.getSource();
if (objSource == buttonSet) {
String strSet = JOptionPane.showInputDialog
("Please enter text to set to editor:");
if (strSet != null) ed = new StringEditor
(strSet, "");
}
else if (objSource == buttonInsert) {
String strInsert = JOptionPane.showInputDialog
("Please enter text to insert into editor at cursor
position:");
for (int i = 0; strInsert != null && i < strInsert.length(); i++)
ed.insertChar(strInsert.charAt(i));
} else if (objSource == buttonDelete) {
ed.delete();
} else if (objSource == buttonBackspace) {
ed.backspace();
} else if (objSource == buttonHome) {
ed.frontOfString();
} else if (objSource == buttonPrev) {
ed.moveCursorLeft();
} else if (objSource == buttonNext) {
ed.moveCursorRight();
} else if (objSource == buttonEnd) {
ed.endOfString();
}
textOutput.setText(ed.toString());
}
}