aspose file tools*
The moose likes Beginning Java and the fly likes passing objects to constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "passing objects to constructors " Watch "passing objects to constructors " New topic
Author

passing objects to constructors

Mark Baumeister
Greenhorn

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

}
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Aside from the fact that you need to move your code inside the code tags,   MyList(MyList lst)   suggests to me that you are stuck on getting going. Giving you a direct answer will probably further drill you in. Let me ask you some questions:

  • What does the list contain ? Strings ?
  • Is there a base class for MyList that would serve as a list container ?


  • To pass an item / an object, from one constructor to the next suggests a class hirearchy, which is done by immediately calling super() as the first line in the constructor. You are providing contract statements of what the constructor Preconditions / Postconditions are, which sounds to me like you being instructed by someone who has had to disentangle this stuff many times. I suggest you focus on basics for a little while until you can fulfil conditions.

    For one thing, there is a rich toolkit in java Collections, try looking in there for something. If it is a class and not an interface, you can make it a base class for MyList and simplifiy some of your work. By keeping a reference to next in your MyList class, you may be able to construct a list. One of the OO ranchers will have to help you with that.

    I suggest not having a default constructor if the intent of the code is to pass two string objects ... but to answer your question:



    I have not tried to do a linked list in any programming language, I always go to a map or hashtable, but since you are trying to do a list, try something like LinkedList<E> as your base class for MyList. That is gonna be awful steep for a beginner, but no worse than trying to provide contract pre/post conditions without knowing how to get them fulfilled.


    "The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
     
    Don't get me started about those stupid light bulbs.
     
    subject: passing objects to constructors