This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes string editor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string editor" Watch "string editor" New topic
Author

string editor

Mark Baumeister
Greenhorn

Joined: Sep 27, 2007
Posts: 6
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());
}

}
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

i've just skimmed the requirements, but i don't think you're actually building a GUI anything - the GUI stuff is already in the class file you are given.

You will implement the MyList class as a singly linked list. Then, you will write your StringEditor class, that will have two of your MyList objects as members. Each of those lists will contain Character objects, one per node, representing the characters of the string.

The 'cursor' is a sort of vitual cursor. Normally, a cursor divides a string into two parts - the part before and the part after the cursor.

in your computers memory, the position of the cursor will be represented by the two separate lists. assume you are given the string

"Fred likes Java"

you will have a linked list with 15 nodes (one for each char), and one with no nodes. if the user places the cursor between the 'i' and the 'k', you need to move all the nods from the 'k' on to the second list. as the user 'moves' the cursor left and right, you will delete nodes from one list and add to the other.

that's a quick, high level overview of what i THINK you are supposed to do.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tony Smith
Ranch Hand

Joined: Jul 07, 2007
Posts: 229
Man, looks like you have one mean teacher if it's one of those introductory courses. I guess if it's online, you don't even know who the teacher is? I am not sure if you have any classmates available, if will certainly help to discuss with them.

As for the program, just do one thing at a time. Finish MyList Class first. Then understand what StringEditor class is all about and make it work with MyList class. You should be able to write simple test cases without having the gui part.

I am not sure what the deadline is, but hopefully you will get the first 2 parts done. If you have additional time, then hopefully it will work with the gui automatically without creating much errors. Good luck!
Mark Baumeister
Greenhorn

Joined: Sep 27, 2007
Posts: 6
Thanks for both of your suggestions.
They are helpful. I have a better idea,
however, I can't figure out how I am supposed to
get the two MyList objects to the StringEditor class.

From what I have understood, the StringEditorGUI
(which has the main method) will allow the user to
input strings to then be passed to the StringEditor
object. But to get the MyList objects to the StringEditor
is not clear to me.

Thanks,
MB
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

I think you are overthinking things a little.

How would you get two Integers into a class you create? or two Strings? you just put them there.


you'll do the same thing when you write your StringEditor class, but put in two MyList object references.
Mark Baumeister
Greenhorn

Joined: Sep 27, 2007
Posts: 6
Ok,
Thanks that makes sense. But, I am now having a problem
in my second StringEditor constructor when I want
to equate the MyList variables to String left and String right (these
parameters must remain as String). I know there must be some way to convert or cast these variables so that I can equate them to left and right.
Any hints?

Thanks,
MB

public class StringEditor {

MyList leftChars;
MyList rightChars;

/** Creates a new instance of StringEditor */
// 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
// Postcondition: characters from strings left and right are represented
// in the editor
public StringEditor(String left, String right) {

left = leftChars;
right = rightChars;
}
 
Consider Paul's rocket mass heater.
 
subject: string editor
 
Similar Threads
Linked List
reverse for stacks??
toString
passing objects to constructors
casting objects?