From ddf790a6d8e03142c7490267e408ec4ccaa371c2 Mon Sep 17 00:00:00 2001 From: Resilient08 <58645288+Resilient08@users.noreply.github.com> Date: Tue, 21 Jul 2020 08:29:27 -0400 Subject: [PATCH 1/3] Part 1 --- .../java/com/github/curriculeon/Person.java | 15 +++++++++ .../com/github/curriculeon/TestPerson.java | 33 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 3c8350b..423eb21 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,5 +1,20 @@ package com.github.curriculeon; public class Person { + final long id; + private String name; + Person(long id, String name){ + this.id = id; + this.name = name; + } + public long getId(){ + return id; + } + public String getName(){ + return name; + } + public void setName(String name){ + this.name = name; + } } diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index 6c523fe..e22026f 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -1,5 +1,38 @@ package com.github.curriculeon; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + public void testConstructor() { + // given + Long expectedId = 0L; + String expectedName = "Some name"; + + // when + Person person = new Person(expectedId, expectedName); + String actualName = person.getName(); + Long actualId = person.getId(); + + // then + Assert.assertEquals(expectedId,actualId); + Assert.assertEquals(expectedName,actualName); + } + + @Test + public void testSetName() { + // given + Person person = new Person(-1,null); + String expectedName = "Some name"; + Assert.assertNotEquals(expectedName, person.getName()); + // when + person.setName(expectedName); + String actualName = person.getName(); + + // then + Assert.assertEquals(expectedName,actualName); + } } + + From c9ea87327e83d7c1025ddac95ce92ff415746a9e Mon Sep 17 00:00:00 2001 From: Resilient08 <58645288+Resilient08@users.noreply.github.com> Date: Mon, 27 Jul 2020 01:35:31 -0400 Subject: [PATCH 2/3] completed Parts 2.0 and 3.1 with helpful comments added --- .../java/com/github/curriculeon/Learner.java | 7 +++++++ .../java/com/github/curriculeon/Person.java | 13 +++++++++---- .../java/com/github/curriculeon/Student.java | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/github/curriculeon/Learner.java create mode 100644 src/main/java/com/github/curriculeon/Student.java diff --git a/src/main/java/com/github/curriculeon/Learner.java b/src/main/java/com/github/curriculeon/Learner.java new file mode 100644 index 0000000..9657f6b --- /dev/null +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -0,0 +1,7 @@ +package com.github.curriculeon; + +public interface Learner { + //Learner interface and all methods are implemented by Person class + void learn(Double numberOfHours); + Double getTotalStudyTime(); +} diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 423eb21..5f21aec 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,8 +1,13 @@ package com.github.curriculeon; -public class Person { - final long id; +public class Person { //Person is parent class + final long id; //final cannot change private String name; + + public Person() { + this.id = null; + } + Person(long id, String name){ this.id = id; this.name = name; @@ -10,10 +15,10 @@ public class Person { public long getId(){ return id; } - public String getName(){ + public String getName(){ //get method/getter used because name is private return name; } - public void setName(String name){ + public void setName(String name){ //set method/setter used because name is private this.name = name; } diff --git a/src/main/java/com/github/curriculeon/Student.java b/src/main/java/com/github/curriculeon/Student.java new file mode 100644 index 0000000..cff6e8c --- /dev/null +++ b/src/main/java/com/github/curriculeon/Student.java @@ -0,0 +1,19 @@ +package com.github.curriculeon; + +public class Student extends Person implements Learner { //Student is child of Person class and accesses public methods via extends + private double totalStudyTime; + + public Student(Long id, String name) { + super(id, name);//super is used to call a nonparameterless constructor in the parent class Person + } + + @Override//allows new version of method in child class + public void learn(Double numberOfHours) { + this.totalStudyTime = totalStudyTime + numberOfHours; + } + + @Override//allows new version of method in child class + public Double getTotalStudyTime() { + return totalStudyTime; + } +} From d8773e7a45d87462ae66367ee06d596d0109d5c1 Mon Sep 17 00:00:00 2001 From: Resilient08 <58645288+Resilient08@users.noreply.github.com> Date: Tue, 28 Jul 2020 15:24:07 -0400 Subject: [PATCH 3/3] completed 3.1, 4, and 5.1 with comments --- .../com/github/curriculeon/Instructor.java | 19 +++++++++++++++++++ .../java/com/github/curriculeon/Teacher.java | 6 ++++++ 2 files changed, 25 insertions(+) create mode 100644 src/main/java/com/github/curriculeon/Instructor.java create mode 100644 src/main/java/com/github/curriculeon/Teacher.java diff --git a/src/main/java/com/github/curriculeon/Instructor.java b/src/main/java/com/github/curriculeon/Instructor.java new file mode 100644 index 0000000..2df95e4 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -0,0 +1,19 @@ +package com.github.curriculeon; //package is a grouping of related classes and interfaced; prevents naming conflicts + +public class Instructor extends Person implements Teacher{ //Instructor is child of Person + public Instructor(Long id, String name) { //Long id and String name are the 2 params + super(id, name); //super is used to call a nonparameterless constructor in Parent (Person) class + } + @Override //new version of method in child class + public void teach(Learner learner, Double numberOfHours) { + learner.learn(numberOfHours); + } + @Override //new version of method in child class + public void lecture(Learner[] learners, Double numberOfHours) { + double numberOfHoursPerLearner = numberOfHours / learners.length; + for (int i = 0; i < learners.length; i++) { + Learner learner = learners[i]; + learner.learn(numberofHoursPerLearner); + } + } +} diff --git a/src/main/java/com/github/curriculeon/Teacher.java b/src/main/java/com/github/curriculeon/Teacher.java new file mode 100644 index 0000000..fb969d6 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -0,0 +1,6 @@ +package com.github.curriculeon; + +public interface Teacher {//this interface is implemented by Person class + void teach(Learner learner, Double numberOfHours); + void lecture(Learner[] learners, Double numberOfHours); +}