# I cant analyze this code

• 11-20-2010, 04:03 PM
eng_hyzoom
I cant analyze this code
i want to analyze this code please
ــــــــــــــــــــــــــــــــــــــــــــــــــ ــــــــــــــــــــــــــــــــــ
public class Test{
public static void main(String[] args) {
MyPoint p1 = new MyPoint();
MyPoint p2 = new MyPoint(10, 30.5);
System.out.println(p1.distance(p2));
System.out.println(MyPoint.distance(p1, p2));
}
}

class MyPoint {
private double x;
private double y;

public MyPoint() {
}

public MyPoint(double x, double y) {
this.x = x;
this.y = y;
}

public double distance(MyPoint secondPoint) {
return distance(this, secondPoint);
}

public static double distance(MyPoint p1, MyPoint p2) {
return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) +
(p1.y - p2.y) * (p1.y - p2.y));
}

public double getX() {
return x;
}

public double getY() {
return y;
}
}

ــــــــــــــــــــــــــــــــــــــــــــــــــ ـــــــــــــــــــــــــــــــــ
thanks very much
• 11-20-2010, 04:15 PM
Fubarable
Quote:

Originally Posted by eng_hyzoom
i want to analyze this code please

Do you have a specific question about it? Is something not working? Are errors occurring?
• 11-20-2010, 04:35 PM
eng_hyzoom
Quote:

Originally Posted by Fubarable
Do you have a specific question about it? Is something not working? Are errors occurring?

yes i cant under stand this method

public double distance(MyPoint secondPoint) {
return distance(this, secondPoint);
}

what is MyPoint secondPoint???
and (this, secondPoint) what are they refers to???

• 11-20-2010, 04:43 PM
Fubarable
Quote:

Originally Posted by eng_hyzoom
yes i cant under stand this method

public double distance(MyPoint secondPoint) {
return distance(this, secondPoint);
}

what is MyPoint secondPoint???
and (this, secondPoint) what are they refers to???

When you call this method, you will pass a MyPoint object into it via the parameters, and that will be the secondPoint.

e.g.,

Code:

```MyPoint pointA = MyPoint(3.0, 1.0); MyPoint pointB = MyPoint(6.0, 2.0); // let's call distance on pointA and pass pointB into the method: double distance = pointA.distance(pointB); // here pointB is the secondPoint```
• 11-20-2010, 04:54 PM
eng_hyzoom
is secondPoint of data type MyPoint????????
(this, secondPoint) this here used for what ??
is this refers to x,y ??
and secondPoint refers to p1,p2??
• 11-20-2010, 05:07 PM
Fubarable
Quote:

Originally Posted by eng_hyzoom
is secondPoint of data type MyPoint????????

One question mark is usually sufficient.

Yes, the method signature tells you this:
Code:

```public double distance([color="red"]MyPoint[/color] secondPoint) {  // here   return distance(this, secondPoint); }```
Quote:

(this, secondPoint) this here used for what ??
These are the parameters for the static distance method called in this method above. To sum, you have two distance methods in this class, one that is an instance method that you see above, and takes one MyPoint object as a parameter and the other which is a static method and takes two MyPoint objects as parameters. The first method calls the second.

Quote:

is this refers to x,y ??
and secondPoint refers to p1,p2??
Not sure what you mean here.
• 11-20-2010, 07:33 PM
DarrylBurke
Quote:

i want to analyze this code please
It's customary to cite the source when you copy code from somewhere and post it here.
http://faculty.ncf.edu/lkaganovskiy/...gInObjects.ppt (Slide 24) based on Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved. 0136012671

I'd guess posting the code on a public forum is a violation of the copyright, but I could be wrong.

Also, you'd be better off learning Java and doing your homework from scratch than posting someone else's code and struggling to understand enough to claim that it's your own.

db