Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Total Newbie

Reply
Thread Tools

Total Newbie

 
 
David McCallum
Guest
Posts: n/a
 
      09-24-2003
I have defined the following classes:

When the main method is called, Circle.toString is called recursively. The
intention is to use Shape.toString to create the X and Y parts of the
string.

Can anyone tell me why?

public class Shape {
double x;
double y;

Shape(){
x=0;
y=0;
}

Shape(double newX, double newY){
x=newX;
y=newY;
}

public String toString() {
return "X="+new Double(x).toString()+"\n"+
"Y="+new Double(y).toString()+"\n";
}
}

public class Circle extends Shape {
double radius;

Circle(){
super();
}

Circle(double newX, double newY, double newRadius) {
super(newX, newY);
radius=newRadius;
}

public String toString() {
return ((Shape)this).toString()+
"R="+new Double(radius).toString()+"\n";
}

public static void main(String[] args){
Circle c1=new Circle();
Circle c2=new Circle(1, 2, 3);

System.out.println(c1);
System.out.println(c2);
}
}


 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      09-24-2003
David McCallum wrote:
> I have defined the following classes:
>
> When the main method is called, Circle.toString is called recursively. The
> intention is to use Shape.toString to create the X and Y parts of the
> string.
>
> Can anyone tell me why?


Because of this:

> return ((Shape)this).toString()+
> "R="+new Double(radius).toString()+"\n";


in Java, instance methods are bound polymorphically at runtime.
The cast to Shape achieves nothing, it still calls the toString
method of that particular *object*, and the object is of class
Circle.

What you do instead is call "super.toString()".

For completeness' sake, this runtime binding does not apply to static
methods or to the selection of overloaded methods in respect to the
parameters' type.

 
Reply With Quote
 
 
 
 
David McCallum
Guest
Posts: n/a
 
      09-24-2003
Michael

> What you do instead is call "super.toString()".


Thanks for that, that's what I did, problem is with the following methods
added, calling Circle.equals recurses.

Any clues, I can't see where super would fit in here

David

SHAPE

public boolean equals(Object o){
boolean result=false;
Shape myO;

if (o instanceof Shape) {
myO=(Shape)o;
result=(((this.x==myO.x) && (this.y==myO.y)));
}

return result;
}

CIRCLE

public boolean equals(Object o){
boolean result=true;

if (o instanceof Circle ){
result=result && ((Shape)this).equals((Shape)o);
result=(result && this.radius==((Circle)o).radius);
}
else {
result=false;
}

return result;
}



 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      09-24-2003
"David McCallum" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Michael
>
> > What you do instead is call "super.toString()".

>
> Thanks for that, that's what I did, problem is with the following methods
> added, calling Circle.equals recurses.
>
> Any clues, I can't see where super would fit in here
>


because of this line:

return ((Shape)this).toString()+"R="+new Double(radius).toString()+"\n";


what that says is call the toString() method of the current object, since it
is called inside the toString method of the current ovject it recurses.

No doubtably what you intend to happen as already explained is this

return super.toString()+"R="+new Double(radius).toString()+"\n";

this will call the super class toString() method and solve your problem

casting does not change what sort of object it is! You can not make your
Circle object a Shape object, or anything else for that matter, though
technically a Circle object already is a Shape object. Suggest reading up
on inheritence, it does click eventually.

--
Mike W


 
Reply With Quote
 
Niels Dybdahl
Guest
Posts: n/a
 
      09-24-2003
> Thanks for that, that's what I did, problem is with the following methods
> added, calling Circle.equals recurses.
> result=result && ((Shape)this).equals((Shape)o);


Try this instead:

if (!super.equals(o))
return false;

Niels Dybdahl


 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      09-25-2003
"David McCallum" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Michael
>
> > What you do instead is call "super.toString()".

>
> Thanks for that, that's what I did, problem is with the following methods
> added, calling Circle.equals recurses.
>
> Any clues, I can't see where super would fit in here


You're making *exactly* the same mistake again:

> result=result && ((Shape)this).equals((Shape)o);
> result=(result && this.radius==((Circle)o).radius);


Please try to understand what I wrote about *why* the problem ocurred.
As a sidenote: casting "this" *never* makes sense in Java.
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      09-25-2003
Michael Borgwardt <(E-Mail Removed)> scribbled the following:
> "David McCallum" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
>> Michael
>> > What you do instead is call "super.toString()".

>>
>> Thanks for that, that's what I did, problem is with the following methods
>> added, calling Circle.equals recurses.
>>
>> Any clues, I can't see where super would fit in here


> You're making *exactly* the same mistake again:


>> result=result && ((Shape)this).equals((Shape)o);
>> result=(result && this.radius==((Circle)o).radius);


> Please try to understand what I wrote about *why* the problem ocurred.
> As a sidenote: casting "this" *never* makes sense in Java.


Sure it does:

public class Foo {
public static void main(String[] args) {
new Foo().doAllTheStuff();
}

private void doSomething(Object arg) {
System.out.println("Hello");
}

private void doSomething(Foo arg) {
System.out.println("world!");
}

public void doAllTheStuff() {
doSomething((Object)this);
doSomething(this);
}
}

when executed, this class prints out:

Hello
world!

--
/-- Joona Palaste ((E-Mail Removed)) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Hasta la Vista, Abie!"
- Bart Simpson
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-25-2003
On 25 Sep 2003 10:42:39 GMT, Joona I Palaste <(E-Mail Removed)>
wrote or quoted :

> doSomething((Object)this);


a cast to Object is always nugatory. Everything is also an Object
since Object is the root class of everything.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      09-25-2003
Roedy Green <(E-Mail Removed)> scribbled the following:
> On 25 Sep 2003 10:42:39 GMT, Joona I Palaste <(E-Mail Removed)>
> wrote or quoted :
>> doSomething((Object)this);


> a cast to Object is always nugatory. Everything is also an Object
> since Object is the root class of everything.


Did you read the code I snipped away? Did you try it on your own Java
implementation? Did you try adding or deleting (Object) casts on the
doSomething() method calls? Do you understand parameter-type-based
method overloading?

--
/-- Joona Palaste ((E-Mail Removed)) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"How come even in my fantasies everyone is a jerk?"
- Daria Morgendorfer
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      09-25-2003
Joona I Palaste <(E-Mail Removed)> scribbled the following:
> Roedy Green <(E-Mail Removed)> scribbled the following:
>> On 25 Sep 2003 10:42:39 GMT, Joona I Palaste <(E-Mail Removed)>
>> wrote or quoted :
>>> doSomething((Object)this);


>> a cast to Object is always nugatory. Everything is also an Object
>> since Object is the root class of everything.


> Did you read the code I snipped away? Did you try it on your own Java

^ should read "you"

> implementation? Did you try adding or deleting (Object) casts on the
> doSomething() method calls? Do you understand parameter-type-based
> method overloading?


--
/-- Joona Palaste ((E-Mail Removed)) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Products like that make me wish I could menstruate."
- Andy Richter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BT Total Broadband vs BT Business Total John Y UK VOIP 2 02-02-2007 09:27 AM
Advice on which router/PIX (total newbie) no one Cisco 3 11-27-2004 08:10 PM
Total newbie: how to deactivate .NET passport dvt ASP .Net 13 09-19-2003 02:47 PM
Total newbie: ASP.NET or Coldfusion ? smurphy ASP .Net 0 06-25-2003 07:18 PM
Re: Total newbie: ASP.NET or Coldfusion ? Kevin Spencer ASP .Net 0 06-25-2003 06:11 PM



Advertisments