Is there a way to have a static method such that it returns a Vector object (with a simple integer x and y value as fields) which is a Vector multiplied by an int value argument. However, there are no new objects made, i.e, the object assigned to the return value is changed instead of there being a new Vector created?
The following code does not achieve this:
public class Vector{
public int x,y;
public Vector(int x,int y){
this.x = x;
this.y = y;
}
//Important code starts
public static Vector mult(Vector v,int a){
return new Vector(v.x*a,v.y*a);
}
//Important code stops
}
This code is what I’m after but it’s too messy:
public static Vector mult(Vector v1,Vector v2,int a){
v1.x = v2.x*a;
v1.y = v2.y*a;
}
Is there an alternative?
Why not add:
to your Vector class.