From 5de991dd326103647b28e7d95b92b32a9e1671b9 Mon Sep 17 00:00:00 2001 From: Julian T Date: Thu, 17 Sep 2020 11:29:50 +0200 Subject: Added m5 assignments --- sem5/oop/m4/shapes/src/MyPoint.java | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 sem5/oop/m4/shapes/src/MyPoint.java (limited to 'sem5/oop/m4') diff --git a/sem5/oop/m4/shapes/src/MyPoint.java b/sem5/oop/m4/shapes/src/MyPoint.java new file mode 100644 index 0000000..46f1ffb --- /dev/null +++ b/sem5/oop/m4/shapes/src/MyPoint.java @@ -0,0 +1,44 @@ + +public class MyPoint { + private double x, y; + + public MyPoint(double x, double y) { + this.x = x; + this.y = y; + } + + public double getX() { + return this.x; + } + public double getY() { + return this.y; + } + + /** + * Subtracts two vectors, thus returning the difference. + * @param b point to subtract from this. + * @return returns a MyPoint which is actually a vector + */ + public MyPoint sub(MyPoint b) { + // This is actually a vector and not a point + return new MyPoint(this.getX() - b.getX(), this.getY() - b.getY()); + } + /** + * Returns a new MyVector with positive components + */ + public MyPoint abs() { + return new MyPoint(Math.abs(this.getX()), Math.abs(this.getY())); + } + + /** + * Calculates the length of MyPoint as if it was a vector. + * @return + */ + public double modulo() { + return Math.sqrt(this.getX() * this.getX() + this.getY() * this.getY()); + } + + public String toString() { + return String.format("[%f, %f]", this.getX(), this.getY()); + } +} -- cgit v1.2.3