From 4bfbc29c7ea0b711f96346deb25dde0b2298ecde Mon Sep 17 00:00:00 2001
From: Julian T <julian@jtle.dk>
Date: Tue, 15 Sep 2020 18:51:31 +0200
Subject: Added more oop assignments

---
 sem5/oop/m4/shapes/src/Main.java        | 23 +++++++++++++++++++++++
 sem5/oop/m4/shapes/src/MyCircle.java    | 24 ++++++++++++++++++++++++
 sem5/oop/m4/shapes/src/MyRectangle.java | 22 ++++++++++++++++++++++
 sem5/oop/m4/shapes/src/MyTriangle.java  | 30 ++++++++++++++++++++++++++++++
 sem5/oop/m4/shapes/src/Shape2d.java     | 13 +++++++++++++
 5 files changed, 112 insertions(+)
 create mode 100644 sem5/oop/m4/shapes/src/Main.java
 create mode 100644 sem5/oop/m4/shapes/src/MyCircle.java
 create mode 100644 sem5/oop/m4/shapes/src/MyRectangle.java
 create mode 100644 sem5/oop/m4/shapes/src/MyTriangle.java
 create mode 100644 sem5/oop/m4/shapes/src/Shape2d.java

(limited to 'sem5/oop/m4/shapes/src')

diff --git a/sem5/oop/m4/shapes/src/Main.java b/sem5/oop/m4/shapes/src/Main.java
new file mode 100644
index 0000000..3c06699
--- /dev/null
+++ b/sem5/oop/m4/shapes/src/Main.java
@@ -0,0 +1,23 @@
+import java.util.ArrayList;
+
+public class Main {
+
+	public static void main(String[] args) {
+		System.out.println("Doing the stuff.");
+		
+		ArrayList<Shape2d> shp = new ArrayList<>();
+
+		shp.add(new MyCircle(new MyPoint(3, 4), 10));
+		shp.add(new MyTriangle(
+				new MyPoint(0, 0), 
+				new MyPoint(4, 5), 
+				new MyPoint(23, 4)));
+		shp.add(new MyRectangle(new MyPoint(0, 3), new MyPoint(-10, 10)));
+		
+		
+		for (Shape2d s : shp) {
+			System.out.printf("area: %f, p: %f%n", s.area(), s.perimeter());
+		}
+	}
+
+}
diff --git a/sem5/oop/m4/shapes/src/MyCircle.java b/sem5/oop/m4/shapes/src/MyCircle.java
new file mode 100644
index 0000000..45aa4e3
--- /dev/null
+++ b/sem5/oop/m4/shapes/src/MyCircle.java
@@ -0,0 +1,24 @@
+
+public class MyCircle implements Shape2d {
+	private MyPoint center;
+	private double r;
+	
+	/**
+	 * Creates a 2d circle object 
+	 * 
+	 * @param center Center of the circle
+	 * @param r Radius of the circle
+	 */
+	public MyCircle(MyPoint center, double r) {
+		this.center = center;
+		this.r = Math.abs(r);
+	}
+	
+	public double area() {
+		return Math.PI * this.r * this.r;
+	}
+	
+	public double perimeter() {
+		return 2 * this.r * Math.PI;
+	}
+}
diff --git a/sem5/oop/m4/shapes/src/MyRectangle.java b/sem5/oop/m4/shapes/src/MyRectangle.java
new file mode 100644
index 0000000..591d7d1
--- /dev/null
+++ b/sem5/oop/m4/shapes/src/MyRectangle.java
@@ -0,0 +1,22 @@
+
+public class MyRectangle implements Shape2d{
+	private MyPoint a, b;
+	
+	/**
+	 * Creates a new rectangle where a, b are opposing corners.
+	 */
+	public MyRectangle(MyPoint a, MyPoint b) {
+		this.a = a;
+		this.b = b;
+	}
+	
+	public double area() {
+		MyPoint size = this.b.sub(this.a);
+		return Math.abs(size.getX() * size.getY());
+	}
+	
+	public double perimeter() {
+		MyPoint size = this.b.sub(this.a).abs();
+		return size.getX() * 2 + size.getY() * 2;
+	}
+}
diff --git a/sem5/oop/m4/shapes/src/MyTriangle.java b/sem5/oop/m4/shapes/src/MyTriangle.java
new file mode 100644
index 0000000..bf9f21f
--- /dev/null
+++ b/sem5/oop/m4/shapes/src/MyTriangle.java
@@ -0,0 +1,30 @@
+
+public class MyTriangle implements Shape2d {
+	private MyPoint a, b, c;
+	
+	/**
+	 * Creates a new triangle with the 3 corners a, b, c
+	 */
+	public MyTriangle(MyPoint a, MyPoint b, MyPoint c) {
+		this.a = a;
+		this.b = b;
+		this.c = c;
+	}
+	
+	// https://www.mathopenref.com/coordtrianglearea.html
+	public double area() {
+		return Math.abs((
+				this.a.getX() * (this.b.getY() - this.c.getY()) +
+				this.b.getX() * (this.c.getY() - this.a.getY()) +
+				this.c.getX() * (this.a.getY() - this.b.getY())) / 2);
+	}
+	
+	public double perimeter() {
+		// Calculate the length of the 3 lines between points
+		double ab = b.sub(a).modulo();
+		double bc = c.sub(b).modulo();
+		double ca = a.sub(c).modulo();
+		
+		return ab + bc + ca;
+	}
+}
diff --git a/sem5/oop/m4/shapes/src/Shape2d.java b/sem5/oop/m4/shapes/src/Shape2d.java
new file mode 100644
index 0000000..09767fa
--- /dev/null
+++ b/sem5/oop/m4/shapes/src/Shape2d.java
@@ -0,0 +1,13 @@
+
+public interface Shape2d {
+	/**
+	 * Calculates the area of the 2d shape.
+	 * @return Area in UNIT^2
+	 */
+	public double area();
+	/**
+	 * Returns the perimeter around the shape.
+	 * @return Perimeter in UNIT
+	 */
+	public double perimeter();
+}
-- 
cgit v1.2.3