x123
x123

Reputation: 13

Learning basics about objects

public class Vector {
    private final double deltaX,deltaY;

    public Vector(double deltaX, double deltaY) {
        this.deltaX = deltaX;
        this.deltaY = deltaY;

    public Vector plus(Vector(a, b)){
        return new Vector(this.deltaX+a,this.deltaY+b);
    }

why does this not work when I am trying to create a method to add a new vector to my existing one? i am defining deltaX as the horizontal component and deltaY as the vertical.

Upvotes: 1

Views: 105

Answers (2)

Jameson
Jameson

Reputation: 6659

Should be:

public Vector plus(Vector v) {
    return new Vector(this.deltaX + v.getDeltaX(),
                      this.deltaY + v.getDeltaY());
}

Where you define those getter methods. Alternately, make deltaX and deltaY public and access them directly from v.

Upvotes: 0

kmell96
kmell96

Reputation: 1465

You aren't using the correct syntax. Your method should be:

public Vector plus(Vector other) {
    return new Vector(this.deltaX + other.deltaX, this.deltaY + other.deltaY);
}

That way, somebody can pass Vector instances into the method.

Upvotes: 1

Related Questions