• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals and to string method

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
How's everything? Busy in learning java?
A question is that I can use the equal and to string methods?
For example:

1. How to write the 'equals() method?
2. How use the toString method that if the Point object had the coordinates(3,4) then the string "(3.0, 4.0)" would be returned.
3. if I need to use the equal() method, what would be the output of the following Java code fragement?

I think it's a little bit hard ...anyone knows that how to solve that ?
Thanks
Larry
[ May 29, 2002: Message edited by: Dirk Schreckmann ]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
easy answer - Point p will never equal Point q,
either equals() or ==
Point a = new Point();
Point b = a;
if (a.equals(b)) is true because they both reference the same object, the default requirement for the equals method.
I posted a answer for possible solution to the equals problem in the intermediate forum
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Larry,
I could not get your first question. Here is the reply for second and third questions.
(2) Here is the code that overrides the toString() method to get the string you want :


(3) To get the output of code fragment, you have to do some modifications. Here is the modified code :


The output is


And if you see the javadocs for equals() method of point, it makes things very clear :


equals
public boolean equals(Object obj)
Determines whether two points are equal. Two instances of Point are equal if the values of their x and y member fields, representing their position in the coordinate space, are the same.
Overrides:
equals in class Point2D
Parameters:
obj - an object to be compared with this point.
Returns:
true if the object to be compared is an instance of Point and has the same values; false otherwise



[ May 29, 2002: Message edited by: Dirk Schreckmann ]
 
Larry Lai
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

Many many thanks your help....
I get it what you mean...tnaks a lot.
See you soon
Larry
reply
    Bookmark Topic Watch Topic
  • New Topic