Simple Program

Hi All,

I am not getting the output as expected.

package javaProg.completeReferance;

class Box2

{

doublewidth,height,depth;

Box2()

{

width=0;

height=0;

depth=0;

}

Box2(double width,double height,double depth)

{

this.width=width;

this.height=height;

this.depth=depth;

}

Box2(Box2 clone)

{

clone.width=this.width;

clone.height=this.height;

clone.depth=this.depth;

}

double volume()

{

return this.width*this.height*this.depth;

}

}

publicclass BoxDemo2

{

publicstaticvoid main(String [] args)

{

Box2 object1=new Box2(12,34,56);

Box2 object2 =new Box2();

Box2 object3 =new Box2(object1);

System.out.println("The volume for Object1 is "+object1.volume());

System.out.println("The volume for Object2 is "+object2.volume());

System.out.println("The volume for Object3 is "+object3.volume());

}

}

[2005 byte] By [confused_in_javaa] at [2007-11-27 10:37:01]
# 1

The assignments are backwards. Box2(Box2 clone) {

clone.width=this.width;

clone.height=this.height;

clone.depth=this.depth;

}

I might have missed some other mistakes. You can increase the helpfulness of replies by posting your expected and actual outputs.

bheilersa at 2007-7-28 18:45:19 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks but the output I am looking for is :

22848

0

22848

confused_in_javaa at 2007-7-28 18:45:19 > top of Java-index,Java Essentials,New To Java...
# 3

And the outputs you are getting are?

bheilersa at 2007-7-28 18:45:19 > top of Java-index,Java Essentials,New To Java...
# 4

0

0

0

confused_in_javaa at 2007-7-28 18:45:19 > top of Java-index,Java Essentials,New To Java...
# 5

And what are you getting?

Regards, Darryl

Darryl.Burkea at 2007-7-28 18:45:20 > top of Java-index,Java Essentials,New To Java...
# 6

Have you followed bheiler's advice? That should fix your problem!

petes1234a at 2007-7-28 18:45:20 > top of Java-index,Java Essentials,New To Java...
# 7

Right. So now the rest of us can help figure out what might be going wrong.

You just need to fix the constructor that takes a Box2 as a parameter. Look closely at what I pointed out in my first reply. If you don't see what's wrong, then just go ahead and reverse the assignments (make them "this.foo = clone.foo;" instead), and then run it again.

Try to figure out what was going wrong though.

I think it's an interesting bug. You see the 0s and the first thought is probably "hmm, none of the assignments are working" or "something is wrong with volume()".

bheilersa at 2007-7-28 18:45:20 > top of Java-index,Java Essentials,New To Java...
# 8

Thanks All I got the expected output but when i dry run on the old code I am confused.

Lets start from this code.

Box2 object1=new Box2(12,34,56);

This will call the constructor with 3 double argument , after that the object1 instant variable will hold the assigned the values in the parameter.Then when I called the volume method the output was 0 for the respective object.

This looksa bit wired to me as The constructor with double argument has nothing to do with Clone constructor.

Sorry if i am sence less.

confused_in_javaa at 2007-7-28 18:45:20 > top of Java-index,Java Essentials,New To Java...
# 9

but your subsequent call to the clone constructor whiped out all the data that you had had in the initial box2 object. Try doing a System.out.println immediately after constructiong the first box, and you'll see what I mean.

petes1234a at 2007-7-28 18:45:20 > top of Java-index,Java Essentials,New To Java...
# 10

Thanks All,

I understood everthing, an Interesting question to be asked during the interview. Predict the output.

confused_in_javaa at 2007-7-28 18:45:20 > top of Java-index,Java Essentials,New To Java...
# 11

> This looksa bit wired to me as The constructor with

> double argument has nothing to do with Clone

> constructor.

I think the problem might have started with the parameter name. With that constructor, "this" *is* the clone of "clone". When the goal is to construct a clone of an existing Box2, I can sympathize with not seeing what is wrong with the line "clone.width = this.width".

If you renamed the parameter to "original", then it will be more intuitive to see that "this.width = original.width" makes more sense than "original.width = this.width".

bheilersa at 2007-7-28 18:45:20 > top of Java-index,Java Essentials,New To Java...