# Thread: I cant analyze this code

1. Member Join Date
Oct 2010
Posts
20
Rep Power
0

## 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  Reply With Quote

2. ##  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?  Reply With Quote

3. Member Join Date
Oct 2010
Posts
20
Rep Power
0

##  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???  Reply With Quote

4. ##  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.,

Java 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```  Reply With Quote

5. Member Join Date
Oct 2010
Posts
20
Rep Power
0

## 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??  Reply With Quote

6. ##  Originally Posted by eng_hyzoom is secondPoint of data type MyPoint????????
One question mark is usually sufficient.

Yes, the method signature tells you this:
Java Code:
```public double distance([color="red"]MyPoint[/color] secondPoint) {  // here
return distance(this, secondPoint);
}```
(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.

is this refers to x,y ??
and secondPoint refers to p1,p2??
Not sure what you mean here.  Reply With Quote

7. ## 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
Last edited by DarrylBurke; 11-20-2010 at 07:37 PM.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•