Skip to content

Instantly share code, notes, and snippets.

@Krisztiaan
Created December 5, 2017 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Krisztiaan/86c99f4b367d8848d529a59c7f710319 to your computer and use it in GitHub Desktop.
Save Krisztiaan/86c99f4b367d8848d529a59c7f710319 to your computer and use it in GitHub Desktop.
public class Vector {
public final double x;
public final double y;
public Vector() {
this(0, 0);
}
public Vector(double x) {
this(x, 0);
}
public Vector(double x, double y) {
this.x = x;
this.y = y;
}
public Vector add(Vector v) {
double newX = this.x + v.x;
double newY = this.y + v.y;
return new Vector(newX, newY);
}
public Vector substract(Vector v) {
double newX = this.x - v.x;
double newY = this.y - v.y;
return new Vector(newX, newY);
}
public double dot(Vector v) {
return this.x * v.x + this.y * v.y;
}
public static void main(String[] args) {
Vector t1 = new Vector();
boolean isNoParameterConstructorInitializing = t1.x == 0 && t1.y == 0;
assert isNoParameterConstructorInitializing;
Vector t2 = new Vector(1);
boolean isOneParameterConstructorInitializing = t2.x == 1 && t2.y == 0;
assert isOneParameterConstructorInitializing;
Vector t3 = new Vector(2, 3);
boolean isTwoParameterConstructorInitializing = t3.x == 2 && t3.y == 3;
assert isTwoParameterConstructorInitializing;
Vector t4 = t1.add(t2);
boolean isAddWorking = (t4.x == (t1.x + t2.x)) && (t4.y == (t1.y + t2.y));
assert isAddWorking;
Vector t5 = t1.substract(t2);
boolean isSubstractWorking = (t4.x == (t1.x - t2.x)) && (t4.y == (t1.y - t2.y));
assert isSubstractWorking;
double dotProd = t2.dot(t3);
boolean isDotProductWorking = dotProd == t2.x * t3.x + t2.y * t3.y;
assert isDotProductWorking;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment