I need to create a Point and Rectangle class for a homework assignment and I'm unsure if I have the right idea with what im trying to do. In my Point class it much have a accesor and mutator, and also a distance method which is what im haveing trouble with.

public class Point {

    private int x;
    private int y;


    public Point ( int x, int y ){
       this.x = x;
       this.y = y;
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

    // not sure if this will work yet
    public double distance ( Point otherPoint)
    {
        double dx = getX() - otherPoint.getX();
        double dy = getY() - otherPoint.getY();
        double dist = Math.sqrt(dx*dx + dy*dy);
        return dist;
    }

}

I am given that the method must be structured as "public double distance (Point otherPoint)" but I don't fully understand how this method should work with the point objects. I will use these point methods in my Rectangle class in order create a getLength() getWidth() getPerimeter() and getArea() methods. So everything builds off of getting the Point() class working correctly.

Recommended Answers

All 3 Replies

yoink:

As you have created method Double Distance, with object as an argument.
so at line 23 first getX() method have value when constructor is called, and otherpoint.getX() have value when you called distance method.

Whats confusing me is I would need two sets of points in my distance method, and I assume that Point otherPoint is a Point object called otherPoint. But I dont think I understand how to use whats being passed in to calculate the distance. To me it just seems like I have one Point object being used, not two.

There are two. One is passed in to the method and the other is the the class object that holds the method being executed. The call to the getX() method calls the local class's method for the value of x in the currrent object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.