aboutsummaryrefslogtreecommitdiff
path: root/sem5
diff options
context:
space:
mode:
Diffstat (limited to 'sem5')
-rw-r--r--sem5/oop/m4/shapes/.classpath6
-rw-r--r--sem5/oop/m4/shapes/.gitignore1
-rw-r--r--sem5/oop/m4/shapes/.project17
-rw-r--r--sem5/oop/m4/shapes/src/Main.java23
-rw-r--r--sem5/oop/m4/shapes/src/MyCircle.java24
-rw-r--r--sem5/oop/m4/shapes/src/MyRectangle.java22
-rw-r--r--sem5/oop/m4/shapes/src/MyTriangle.java30
-rw-r--r--sem5/oop/m4/shapes/src/Shape2d.java13
8 files changed, 136 insertions, 0 deletions
diff --git a/sem5/oop/m4/shapes/.classpath b/sem5/oop/m4/shapes/.classpath
new file mode 100644
index 0000000..fb50116
--- /dev/null
+++ b/sem5/oop/m4/shapes/.classpath
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/sem5/oop/m4/shapes/.gitignore b/sem5/oop/m4/shapes/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/sem5/oop/m4/shapes/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/sem5/oop/m4/shapes/.project b/sem5/oop/m4/shapes/.project
new file mode 100644
index 0000000..093cfa8
--- /dev/null
+++ b/sem5/oop/m4/shapes/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>shapes</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
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();
+}