I have a question about passing objects to constructors with different type object parameters.
I am using three classes:
The main class StringEditorGUI class allows me to enter two
string objects which are passed to constructors (a default constructor and one with specified parameters) in a StringEditor class. These string objects then I beleive need to be passed to a linked list (MyList) class (that contains two constructors and together create two linked objects. So the problem I am having is that the MyList constructor with specified parameter is of the MyList type and the objects being sent are of the String type. Here is the abbreviate code. I need to understand how to pass the String objects from
the StringEditor (String left, String right) constructor to the
MyList(MyList lst) constructor.
Thanks,
MB
public class StringEditorGUI extends JFrame implements ActionListener {
// create empty StringEditor
private StringEditor ed = new StringEditor();
static public void main(String [] args) {
new StringEditorGUI().setVisible(true);
}
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));
public class StringEditor {
/** 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) {
}
public class MyList {
private int numItems;
private Object item;
private MyList next;
/** Creates a new instance of MyList */
// Effect: new MyList object created
// Precondition: None
// Postcondition: an empty MyList object exists
public MyList() {
next = null;
}
// 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) {
next = lst;
}