Results 1 to 7 of 7
  1. #1
    eng_hyzoom is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by eng_hyzoom View Post
    i want to analyze this code please
    Do you have a specific question about it? Is something not working? Are errors occurring?

  3. #3
    eng_hyzoom is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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???

    thanks for reply

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by eng_hyzoom View Post
    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???

    thanks for reply
    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

  5. #5
    eng_hyzoom is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default

    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??

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by eng_hyzoom View Post
    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.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,235
    Rep Power
    19

    Default

    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 06:37 PM.

Similar Threads

  1. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  2. Analyze the SCJP Exam pattern for good score
    By kumarfair30 in forum Java Certification
    Replies: 0
    Last Post: 11-02-2009, 01:32 PM
  3. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  4. Analyze a string of words
    By zoe in forum Advanced Java
    Replies: 2
    Last Post: 07-26-2007, 10:01 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

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