Results 1 to 7 of 7
Thread: I cant analyze this code
- 11-20-2010, 03:03 PM #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
-
- 11-20-2010, 03:35 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
-
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
- 11-20-2010, 03:54 PM #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??
-
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); }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.(this, secondPoint) this here used for what ??
Not sure what you mean here.is this refers to x,y ??
and secondPoint refers to p1,p2??
- 11-20-2010, 06:33 PM #7
It's customary to cite the source when you copy code from somewhere and post it here.i want to analyze this code please
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.
dbLast edited by DarrylBurke; 11-20-2010 at 06:37 PM.
Similar Threads
-
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Analyze the SCJP Exam pattern for good score
By kumarfair30 in forum Java CertificationReplies: 0Last Post: 11-02-2009, 01:32 PM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM -
Analyze a string of words
By zoe in forum Advanced JavaReplies: 2Last Post: 07-26-2007, 10:01 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks