aboutsummaryrefslogtreecommitdiff
path: root/sem5/oop/m1
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-09-03 10:21:33 +0200
committerJulian T <julian@jtle.dk>2020-09-03 10:21:33 +0200
commitb64f5e9e206ba1f9254199a48bbec1846201d0ae (patch)
treeb7dde25371571dddebf523afb4e723c8f9b0bc1d /sem5/oop/m1
parent0e3cafc48570e4b675ecac11663a5f777b24d339 (diff)
Added first oop assignment
Diffstat (limited to 'sem5/oop/m1')
-rw-r--r--sem5/oop/m1/notes.md29
-rw-r--r--sem5/oop/m1/src/Main.java14
-rw-r--r--sem5/oop/m1/src/Person.java16
-rw-r--r--sem5/oop/m1/src/Student.java36
-rw-r--r--sem5/oop/m1/src/Uniperson.java7
5 files changed, 102 insertions, 0 deletions
diff --git a/sem5/oop/m1/notes.md b/sem5/oop/m1/notes.md
new file mode 100644
index 0000000..e879057
--- /dev/null
+++ b/sem5/oop/m1/notes.md
@@ -0,0 +1,29 @@
+# Introduction
+
+## Interfaces
+
+Abstract class used to define object API.
+
+Classes can then implement this API.
+
+```
+public interface Car {
+ public void turn(blabla);
+ blabla;
+}
+```
+
+### Polymorphism
+
+Most general interface is Object (like golangs interface{} ? ).
+
+## Encapsulations
+
+Hide internal details.
+This can be done with getters and setters.
+
+These getters and setters can then be used to to input validation.
+
+## Private vs protected
+
+Protected are accessible to subclasses which inherit from it.
diff --git a/sem5/oop/m1/src/Main.java b/sem5/oop/m1/src/Main.java
new file mode 100644
index 0000000..4ccb5a4
--- /dev/null
+++ b/sem5/oop/m1/src/Main.java
@@ -0,0 +1,14 @@
+
+public class Main {
+ public static void main(String[] args) {
+ Student std = new Student("Julian", (short)21, "Male", (long)123213213, "COMTEK");
+ Uniperson hej = std;
+
+ hej.addGPA(0.32);
+ hej.addGPA(12);
+
+ System.out.printf("deg: %s, sem: %d, avg: %f\n", hej.getDeg(), hej.getSem(), hej.avgGPA());
+
+ System.out.printf("person: %s\n", std);
+ }
+}
diff --git a/sem5/oop/m1/src/Person.java b/sem5/oop/m1/src/Person.java
new file mode 100644
index 0000000..4ed517c
--- /dev/null
+++ b/sem5/oop/m1/src/Person.java
@@ -0,0 +1,16 @@
+
+public class Person {
+ protected String name;
+ protected String gender;
+ protected short age;
+
+ public Person(String name, String gender, short age) {
+ this.name = name.equals("Victor") ? "Viktor" : name;
+ this.gender = gender;
+ this.age = age;
+ }
+
+ public String toString() {
+ return String.format("%s[gen: %s, age: %d]", name, gender, age);
+ }
+}
diff --git a/sem5/oop/m1/src/Student.java b/sem5/oop/m1/src/Student.java
new file mode 100644
index 0000000..cc9938c
--- /dev/null
+++ b/sem5/oop/m1/src/Student.java
@@ -0,0 +1,36 @@
+public class Student extends Person implements Uniperson {
+ // Yes, we do support very long educations
+ private long semester;
+ private String degree;
+
+ private double GPA;
+ private int GPAcount;
+
+ public Student(String name, short age, String gender, long semester, String degree) {
+ super(name, gender, age);
+ this.semester = semester;
+ this.degree = degree;
+ this.GPA = 0;
+ this.GPAcount = 0;
+ }
+
+ public void addGPA(double GPA) {
+ this.GPA += GPA;
+ this.GPAcount++;
+ }
+
+ public long getSem() {
+ return this.semester;
+ }
+
+ public String getDeg() {
+ return this.degree;
+ }
+
+ public double avgGPA() {
+ if (this.GPAcount == 0) {
+ return 0;
+ }
+ return this.GPA / this.GPAcount;
+ }
+}
diff --git a/sem5/oop/m1/src/Uniperson.java b/sem5/oop/m1/src/Uniperson.java
new file mode 100644
index 0000000..7493dea
--- /dev/null
+++ b/sem5/oop/m1/src/Uniperson.java
@@ -0,0 +1,7 @@
+
+public interface Uniperson {
+ public long getSem();
+ public String getDeg();
+ public double avgGPA();
+ public void addGPA(double GPA);
+}