• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

passing objects to constructors

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;

}
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 moustache of a titan! The ad of a flea:
    New web page for Paul's Rocket Mass Heaters movies
    https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
    reply
      Bookmark Topic Watch Topic
    • New Topic