![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2005
Posts: 2
Rep Power: 0
![]() |
various questions on class Point methods
Hi everyone! This is my first post (after a long time). I figured the best way to present my questions is as comments. If you find this difficult to read, let me know.
----------------------------------------------------- //package aueb.util.api; import java.io.*; import java.lang.*; import java.util.*; public class Point { private int x,y; public Point() //constructors don't have return type don't they? { super(); x=y=0; } public Point (int x, int y) { super(); this.x=x; this.y=y; } public Point (Point p) { this(p.x,p.y); } public void setX (int x) { this.x=x<0?0:x; } public void setY(int y) { this.y=y<0?0:y; } public int getX() { return this.x; //difference between "return this.x" & "return x"? } public int getY() { return this.y; //same as above one } public Point move (int dx,int dy) { setX(x+dx); setY(y+dy); return this; } public void printPoint() { int p1= getX(); int p2= getY(); System.out.print(p1+" "+p2); } public double distance(Point p1) { return Math.sqrt(p1.getX()*p1.getX()+p1.getY()*p1.getY()); } public boolean equals (Object o) { if (this==o) return true; if (!(o instanceof Point)) return false; Point p= (Point) o; return (this.getX()== p.getX()) && (this.getY()== p.getY()); } /*method equals has just been overloaded. Why does this have to be declared as static, in contrary to the above one? */ public static boolean equals (Object o1, Object o2) { if (o1==o2) return true; if ((!(o1 instanceof Point)) || (!(o2 instanceof Point))) return false; Point p1= (Point) o1; Point p2= (Point) o2; return (p1.getX()== p2.getX()) && (p1.getY()== p2.getY()); } public static void main (String[] args) { Point p= new Point(2,2); p.printPoint(); System.out.println(); Point p2= new Point(2,3); p2.printPoint(); System.out.println(); System.out.println(p.equals(p2)); //what's the difference between these 2 "equals" System.out.println(equals(p,p2)); //methods? Which one is better? } } Last edited by nocturna_gr; May 7th, 2005 at 3:12 AM. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: May 2005
Posts: 10
Rep Power: 0
![]() |
1. Correct, constructors do not have a return type
2 & 3. For your program, there is no difference between return this.x and return x. However consider this: public class test
{
int x = 9;
public addToX(int x)
{
this.x += x;
}
}In the above method, the object has a variable x and the method has a variable x. The methods variable x takes precedence, so to reference the objects x you have to type this.x. If the method didn't have a variable x, then you could replacethis.x with just x. 4 & 5. Static methods are constant for the whole class. It doesn't matter which one of the above are used since they both do the same thing. In fact, I would just delete the non static since it is just taking up space. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|