1
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.

asked Jan 16, 2016 at 7:33
1
  • Java does not support value decomposition. Each parameter must be taken in as a single object/value. Commented Jan 16, 2016 at 7:39

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.