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.
-
Java does not support value decomposition. Each parameter must be taken in as a single object/value.user2864740– user28647402016年01月16日 07:39:01 +00:00Commented Jan 16, 2016 at 7:39
2 Answers 2
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.
answered Jan 16, 2016 at 7:35
kmell96
1,4751 gold badge17 silver badges40 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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.
answered Jan 16, 2016 at 7:35
Jameson
6,7396 gold badges37 silver badges58 bronze badges
Comments
lang-java