Calling the copy contructor inside a method.

Hello everyone, Ive got a pretty annoying delima right now. Im trying to write a method that uses the copy contructor inside it and it is simply put, just not working.

So how do you do this?

Example I have a copy contructor for a Dll that makes a new copy of a Dll. Now I want to write a method that when its called on a Dll object and given a Dll parameter as input it makes a copy of the input and saves it into the calling Dll object.

publicvoid DLinkedList(DLinkedList theOriginal){

code

}

publicvoid methodWithCopyContructorInside(DLinkedList list){

DLinkedList newList =new DLinkedList(list);

? This is what I would consider to work but since im here you can guess its not.

}

Any suggestions, tips, comments would be most appreciated. Thank you.

Message was edited by:

venture

[1137 byte] By [venturea] at [2007-11-26 17:33:32]
# 1
public void DLinkedList(DLinkedList theOriginal) {If you want it to be a constructor then you might want to remove the void.
floundera at 2007-7-9 0:01:31 > top of Java-index,Java Essentials,Java Programming...
# 2
Right, right Sorry that was a typo. My program acutally just uses public.
venturea at 2007-7-9 0:01:31 > top of Java-index,Java Essentials,Java Programming...
# 3
Im not 100% aure what you are trying to do. Can you explain it better, perhaps more code to help illustrate.
floundera at 2007-7-9 0:01:32 > top of Java-index,Java Essentials,Java Programming...
# 4

Well in the Driver program. You would say something like this.

public static void main (String[] args) {

DLinkedList A = new DLinkedList();

DLinkedList B = new DLinkedList();

B.addvalues(5,6);

A.methodWithCopyConstructorInside(B); // Makes a copy of B using the method with the copy constructor inside.

But[ This code does not store the copy into the calling Dll. That is my problem Im trying to get it to store the copy into the calling Dll object.

}

Basically what I am trying to do is store polynomials inside Dll's and then use a method to add 2 of these (Dll's) Polynomials together. This method would just need the copy constructor inside it and another method to merge that value with another (Dll) Polynomial.

Message was edited by:

venture

Message was edited by:

venture

venturea at 2007-7-9 0:01:32 > top of Java-index,Java Essentials,Java Programming...
# 5
Instead of storing a copy of B into A. This is what it is storing into it. LinkedList@d9f9c3.
venturea at 2007-7-9 0:01:32 > top of Java-index,Java Essentials,Java Programming...
# 6

If I do this in the Driver class.

DLinkedList A = new LinkedList(B);

A.outputList();

It will make a copy of B and store it in A and then A.outputList() will actually output all the Nodes currectly.

Now if I do this

A.methodWithCopyConstructor(B);

A.outputList();

Im not sure what it is storing into A but outputList(); returns nothing. Also if I do a System.out.println on A.methodWithCopyConstructor(B) it returns LinkedList@d9f9c3.

venturea at 2007-7-9 0:01:32 > top of Java-index,Java Essentials,Java Programming...
# 7

You are giving us nothing.

Did you write these classes?

If so, can you at least post the classes?

Did you put anything in the copy construrctor besides the word "code"?

What did you put in it? Would you mind sharing with the forum so we can better understand what you are talking about?

All anyone can tell you based on what you've written here is that you didn't implement the method toString() in your class. That's why you get LinkedList@d9f9c3

jaylogana at 2007-7-9 0:01:32 > top of Java-index,Java Essentials,Java Programming...
# 8

For starters I wouldn't have a constructor that takes a list as a parameter (unless this is some weird requirement of the assignment). You should have to constructors, the default which creates an empty list and one the takes a Node which is the first element in the list.

For your copy method (I would get rid of the word constructor in the name as this is misleading), you should iterate over the other list and add each Node to the current list.

Warning: you won't end up with two separate list. You will end up with two lists that refer to the same set of Nodes.

floundera at 2007-7-9 0:01:32 > top of Java-index,Java Essentials,Java Programming...
# 9

Here is the code for the methods in question. I only didnt post all of it becuase it is alot of stuff and wasnt sure if anyone had the time and patience to read it. Sorry about that.

This is the Copy constructor.

public LinkedList( LinkedList theOriginal ) {

if( theOriginal == this )

;// do nothing

else {

// old contents is detached. Deleted by garbage collector.

makeEmpty();

// Thread the original list inserting all the values found

// into the copy. This code is short but inefficient.

theOriginal.toHeadNode();

while( theOriginal.toNextNode() ) {

int coe = theOriginal.getCurrentCoefficient();

int exp = theOriginal.getCurrentExponent();

insertInAscendingList(coe, exp);

//insertInAscendingList( value );

}

}

}

This is the add method which is supposed to use the copy constructor inside it. Obvisouly from the name of my post you can conclude that this code is incorrect. But to me it seems correct. thus my dilemma.

public void add(LinkedList b) {

LinkedList a = new LinkedList(b);

}

Here is the outputList method.

public void outputList()

{

toHeadNode();

while( toNextNode() ) // returns false if back at head

System.out.print( curr.item + " " );

System.out.println();

}

Now here is the driver program that uses these methods and constructors.

public static void main(String[] args)

{

LinkedList a,b;

a = new LinkedList();

b = new LinkedList(a);

b.outputList(); //This works

//Now if I call the copy constructor everything works fine. A copy is made and method outputList() will print everything out perfect. But!!, Now if I use the add() method

b.add(a);

b.outputList(); //This doesnt work

im not sure what is being stored into b and outputList() returns nothing. A system.out.println() on b.add(a); returns LinkedList@d9f9c3

Message was edited by:

venture

venturea at 2007-7-9 0:01:32 > top of Java-index,Java Essentials,Java Programming...
# 10
Java doesn't have copy constructors, and IMO any design pattern that uses constructors that look like C++ copy constructors should be re-thought. The copying idiom in Java is given by Object.clone().
ejpa at 2007-7-9 0:01:32 > top of Java-index,Java Essentials,Java Programming...
# 11

Assuming your copy constructor is working correctly...

You wrote:

public void add(LinkedList b) {

LinkedList a = new LinkedList(b);

}

All you've done in this method is created a copy of the list "b" and assigned it to reference "a". Then your method ends.

The contents of "a" is lost, because you didn't actually add anything to your internal data structure, whatever that is.

That's like me doing:

class Test

{

int instanceVariable = 0;

public void add(int a) {

int x = a;

}

}

And wondering why the instanceVariable is still 0 after i invoke the method add().

You need to append the elements to the internal data structure of your class.

jaylogana at 2007-7-9 0:01:32 > top of Java-index,Java Essentials,Java Programming...
# 12
That is where I fall short. I do not know the correct syntax to do this with Doubly linked list. I have been morphing that code for 15 hours now and still cant figure it out. If you could hint at the correct way I would be grateful.
venturea at 2007-7-9 0:01:32 > top of Java-index,Java Essentials,Java Programming...
# 13

> This is the Copy constructor.

>

> public LinkedList( LinkedList theOriginal ) {

>if( theOriginal == this )

This condition is impossible. If this is a constructor, 'this' is a brand new value.

>;// do nothing

> else {

> // old contents is detached. Deleted by

> y garbage collector.

> makeEmpty();

Ditto. At this point the new object being constructed is empty.

> public void add(LinkedList b) {

>LinkedList a = new LinkedList(b);

As the other poster has commented, there is something seriously wrong here and it's a conceptual problem. You can't actually use a 'copy constructor' here without creating a new object, and the objective of this method is surely to add the contents of 'b' to the current object. Surely all you need to do here is call insertInAscendingList() directly?

ejpa at 2007-7-9 0:01:32 > top of Java-index,Java Essentials,Java Programming...
# 14

Well, it seems to me that you already have code in the constructor that goes through

each element and adds the coefficient and exponent or whatever to your existing structure.

Have you tried refactoring that code into its own method and using it instead of making a copy of the existing list parameter?

jaylogana at 2007-7-9 0:01:32 > top of Java-index,Java Essentials,Java Programming...
# 15

> Java doesn't have copy constructors, and IMO any

> design pattern that uses constructors that look like

> C++ copy constructors should be re-thought. The

> copying idiom in Java is given by Object.clone().

Clone is generally regarded as icky, and copy c'tors (e.g. public List(List originalList)) are considered better. Not sure exactly why.

jverda at 2007-7-21 17:06:47 > top of Java-index,Java Essentials,Java Programming...
# 16

Well for some strange reason my professor says it can be done. So in 10 hours when class starts if I dont have it done then Im getting a zero for the assignment. Lol. I had a feeling this was a tuff problem to tackle I just didnt realize it was such a pain in the behine. Anyways thank you for your patience guys. If you can think of a way to do this I would be very appreciative. Otherwise Im just going to stay up for another 10 hours until I have to leave to go to school and If I dont have it figured out by then Im just going to have to accept a zero.

venturea at 2007-7-21 17:06:47 > top of Java-index,Java Essentials,Java Programming...
# 17

If it doesn't have to be an 'add' method you could actually make it a 'clone' method:

publlic Object clone()

{

return new LinkedList(this);

}

However IMO your professor has no business teaching copy constructors in a language that doesn't really have them.

ejpa at 2007-7-21 17:06:47 > top of Java-index,Java Essentials,Java Programming...
# 18

Here are the instructions pertaining to the add method.

Required:

Polymonial( )// default constructor

Polynomial( Polynomial theOriginal ) // copy constructor

output( )

input( )

addThis could have either one parameter or two. It is up to you.

If you choose to use one parameter, then a = b + c would be

computed by:

a.Polynomial(b); // calling the copy constructor, then

a.add(c);// adding c

venturea at 2007-7-21 17:06:47 > top of Java-index,Java Essentials,Java Programming...
# 19

> Java doesn't have copy constructors, and IMO any

> design pattern that uses constructors that look like

> C++ copy constructors should be re-thought. The

> copying idiom in Java is given by Object.clone().

To the contrary I would suggest that using Cloneable is ill advised. There's numerous problems with it ranging from the fact that Cloneable doesn't actually have a clone() method to the reliance on largely undocumented conventions for correct behavior.

> > Java doesn't have copy constructors, and IMO any

> > design pattern that uses constructors that look

> like

> > C++ copy constructors should be re-thought. The

> > copying idiom in Java is given by Object.clone().

>

> Clone is generally regarded as icky, and copy c'tors

> (e.g. public List(List originalList)) are

> considered better. Not sure exactly why.

I think this sums it up pretty well.

http://www.artima.com/intv/bloch13.html

kablaira at 2007-7-21 17:06:47 > top of Java-index,Java Essentials,Java Programming...
# 20

Well I just said screw it and did this. It works but its not pretty.

public LinkedList add(LinkedList theOriginal, LinkedList b) {

if( theOriginal == this )

;// do nothing

else {

// old contents is detached. Deleted by garbage collector.

makeEmpty();

// Thread the original list inserting all the values found

// into the copy. This code is short but inefficient.

theOriginal.toHeadNode();

while( theOriginal.toNextNode() ) {

int coe = theOriginal.getCurrentCoefficient();

int exp = theOriginal.getCurrentExponent();

insertInAscendingList(coe, exp);

}

}

if( b == theOriginal )

;// do nothing

else {

// Thread the original list inserting all the values found

// into the copy. This code is short but inefficient.

b.toHeadNode();

while( b.toNextNode() ) {

int coe = b.getCurrentCoefficient();

int exp = b.getCurrentExponent();

addTerm(coe, exp);

}

}

return this;

}

venturea at 2007-7-21 17:06:47 > top of Java-index,Java Essentials,Java Programming...
# 21
Don't pay attention to the side conversation about copy constructors...it has absolutely no bearing on your assignment.I think you can do better.Can you understand what you wrote?Show a working example of how to use the your add() method.
jaylogana at 2007-7-21 17:06:47 > top of Java-index,Java Essentials,Java Programming...
# 22
Let me get this Multiply method finished and my assignment ready to turn in then I will post a summary of everything.
venturea at 2007-7-21 17:06:47 > top of Java-index,Java Essentials,Java Programming...
# 23

> > Java doesn't have copy constructors, and IMO any

> > design pattern that uses constructors that look

> like

> > C++ copy constructors should be re-thought. The

> > copying idiom in Java is given by Object.clone().

>

> To the contrary I would suggest that using Cloneable

> is ill advised. There's numerous problems with it

> ranging from the fact that Cloneable doesn't actually

> have a clone() method to the reliance on largely

> undocumented conventions for correct behavior.

>

When you don't know what you're doing either will yield potentially dangerous (and certainly unwanted) sideeffects.

Cloneable works well when done properly, just as a contrived copy constructor works poorly if done poorly.

It might be argued that it's often easier to do using constructors, but that just means it's easier to do poorly ;)

jwentinga at 2007-7-21 17:06:47 > top of Java-index,Java Essentials,Java Programming...
# 24

I'll just add that the professor seems to be asking the students to use a 'copy constructor' without actually constructing an object:

a.Polynomial(b); // calling the copy constructor, then

Regardless of the copy/clone debate, it seems poor pedagogy and poor practice to define something as a constructor and then not use it that way.

ejpa at 2007-7-21 17:06:47 > top of Java-index,Java Essentials,Java Programming...