aboutsummaryrefslogtreecommitdiff
path: root/sem5/oop/m4/shapes/src/MyPoint.java
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-09-17 11:29:50 +0200
committerJulian T <julian@jtle.dk>2020-09-17 11:29:50 +0200
commit5de991dd326103647b28e7d95b92b32a9e1671b9 (patch)
treea66b2976081338deae84f2cc25fde9beabbca824 /sem5/oop/m4/shapes/src/MyPoint.java
parentd285183c42f5d377b197bd0efab3bb20b240ef34 (diff)
Added m5 assignments
Diffstat (limited to 'sem5/oop/m4/shapes/src/MyPoint.java')
-rw-r--r--sem5/oop/m4/shapes/src/MyPoint.java44
1 files changed, 44 insertions, 0 deletions
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());
+ }
+}