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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

equals and to string method

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
---------------------------------- | |
| public class Point |
| ------------------------------| |
| private doulbe x,y |
| |
| public Point (double x,y) |
| |
| public double x() |
| public doulbe y() |
| |
| public boolean equal(PointP) |
| |
| public String toString() |
| |
----------------------------------
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?
Point p = new Point (2, 3);
println("p.x() = " + p.x() + ", p.y()= " + p.y());
println ("p = " + p);
Point q = new Point (7, 4);
println ("q = " + q);
if (q.equals(p))
println ("q equals p");
else
println ("q does not equal p");
q = new Point (2, 3);
println ("q = " + q);
if (q.equals(p))
println ("q equals p");
else
println ("q does not equal p");
if (q == p)
println ("q == p");
else
println ("q != p");
I think it's a little bit hard ...anyone knows that how to solve that ?
Thanks
Larry
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
the default behavior, the equals method of object class, equates to true if both references point to the same object.
http://java.sun.com/products/jdk/1.1/docs/api/java.lang.Object.html
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
You can override the equals method within your class by using the reference passed to the equals
method
equals(Point p){
if ((this.x == p.x) && (this.y == p.x)){
return true;
}
else {return false;}
}
overriding the toString method
you can return whatever string or string concatenation you beleive to represent the objec
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please don't start the same topic in more than one forum; it wastes time for people who don't realize if something's already been answered elsewhere. Followups go here. Thanks...
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic