Skip to main content
Code Review

Return to Answer

Your code is performing integer division for the vector, which is bound to fail:

public void div(Vector vector) {
 this.x /= vector.x;
 this.y /= vector.y;
}

In this case, if the input vector is say x = 10 and y = 10, and we are x = 19 and y=19, the result will be:

this.x = 1;
this.y = 1;

which is counterintuitive.

I would recommend either not supporting div at all, or, alternatively, change your vector values to floating-point values (double).

I would also recommend that you change your model for vectors significantly, and instead of changing the current Vector with your arithmetic, that instead you return a new vector with the result:

class Vector {
 final double x;
 final double y;
 public Vector(double x, double y) {
 this.x = x;
 this.y = y;
 }
 public voidVector add(Vector vector) {
 return new Vector(this.x + vector.x, this.y + v.y);
 }
 ....
}

Note how the x and y components of the vector are final, and the add returns a new vector.

Your code is performing integer division for the vector, which is bound to fail:

public void div(Vector vector) {
 this.x /= vector.x;
 this.y /= vector.y;
}

In this case, if the input vector is say x = 10 and y = 10, and we are x = 19 and y=19, the result will be:

this.x = 1;
this.y = 1;

which is counterintuitive.

I would recommend either not supporting div at all, or, alternatively, change your vector values to floating-point values (double).

I would also recommend that you change your model for vectors significantly, and instead of changing the current Vector with your arithmetic, that instead you return a new vector with the result:

class Vector {
 final double x;
 final double y;
 public Vector(double x, double y) {
 this.x = x;
 this.y = y;
 }
 public void add(Vector vector) {
 return new Vector(this.x + vector.x, this.y + v.y);
 }
 ....
}

Note how the x and y components of the vector are final, and the add returns a new vector.

Your code is performing integer division for the vector, which is bound to fail:

public void div(Vector vector) {
 this.x /= vector.x;
 this.y /= vector.y;
}

In this case, if the input vector is say x = 10 and y = 10, and we are x = 19 and y=19, the result will be:

this.x = 1;
this.y = 1;

which is counterintuitive.

I would recommend either not supporting div at all, or, alternatively, change your vector values to floating-point values (double).

I would also recommend that you change your model for vectors significantly, and instead of changing the current Vector with your arithmetic, that instead you return a new vector with the result:

class Vector {
 final double x;
 final double y;
 public Vector(double x, double y) {
 this.x = x;
 this.y = y;
 }
 public Vector add(Vector vector) {
 return new Vector(this.x + vector.x, this.y + v.y);
 }
 ....
}

Note how the x and y components of the vector are final, and the add returns a new vector.

Source Link
rolfl
  • 98.1k
  • 17
  • 219
  • 419

Your code is performing integer division for the vector, which is bound to fail:

public void div(Vector vector) {
 this.x /= vector.x;
 this.y /= vector.y;
}

In this case, if the input vector is say x = 10 and y = 10, and we are x = 19 and y=19, the result will be:

this.x = 1;
this.y = 1;

which is counterintuitive.

I would recommend either not supporting div at all, or, alternatively, change your vector values to floating-point values (double).

I would also recommend that you change your model for vectors significantly, and instead of changing the current Vector with your arithmetic, that instead you return a new vector with the result:

class Vector {
 final double x;
 final double y;
 public Vector(double x, double y) {
 this.x = x;
 this.y = y;
 }
 public void add(Vector vector) {
 return new Vector(this.x + vector.x, this.y + v.y);
 }
 ....
}

Note how the x and y components of the vector are final, and the add returns a new vector.

lang-java

AltStyle によって変換されたページ (->オリジナル) /