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.

Dani AI

Generated

— you’re basically correct and the confusion is normal. When you call p1.distance(p2) there are two Point objects involved: the one whose method runs (the current object) and the one passed in as the parameter. Inside the method you read coordinates from both objects (via getX()/getY()) and compute the difference between them, then return the Euclidean length of that difference.

A quick runtime check you can try:

Point a = new Point(3, 4);
Point b = new Point(0, 0);
double d = a.distance(b); // expected 5.0

For the Rectangle methods: if your rectangles are axis-aligned, compute width and length as absolute differences of x and y from two corner Points, then area and perimeter from those values. Example helpers:

int width  = Math.abs(p1.getX() - p2.getX());
int height = Math.abs(p1.getY() - p2.getY());
int perimeter = 2 * (width + height);
int area = width * height;

A couple of practical tips: use Math.hypot(dx, dy) inside distance for better numeric stability than manual squaring plus Math.sqrt. Normalize corners in your Rectangle constructor (store left/right and top/bottom) so getWidth()/getLength() always return non-negative values. Add a null check in distance and write a few unit tests (for example the (0,0)-(3,4) case) to verify results. As pointed out, the values come from the objects you construct and call methods on, and ’s note about the two objects is exactly right.

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.