From cd19ef0c80ee078cf23c51391267fdfd6179fdde Mon Sep 17 00:00:00 2001 From: ANIL CHADDHA Date: Sat, 25 Jul 2020 14:30:43 -0400 Subject: [PATCH 1/5] Learner lab Part 1 completed --- .../java/com/github/curriculeon/Person.java | 20 ++++++++++++++++ .../com/github/curriculeon/TestPerson.java | 24 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 3c8350b..8c74d9d 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,5 +1,25 @@ package com.github.curriculeon; public class Person { + //non primitive Long + //Changing variables to Private + private final Long id; + private String name; + public 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..b697e61 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -1,5 +1,29 @@ package com.github.curriculeon; +import org.junit.Assert; +import org.junit.Test; + public class TestPerson { + @Test + public void testConstructor(){ + + //given + Long expectedId=0L; + String expectedName="Some name"; + + + + //when + //Giving the object we want to test, test when the constructor is called + Person person=new Person(expectedId, expectedName); + //Checking if actual output is same as expected + String actualName=person.getName(); + Long actualId=person.getId(); + + + //then + Assert.assertEquals(expectedId, actualId); + Assert.assertEquals(expectedName, actualName); + } } From 1f5a787b21b12d2882ef10e1f59d8e60d746444f Mon Sep 17 00:00:00 2001 From: ANIL CHADDHA Date: Sat, 25 Jul 2020 16:05:19 -0400 Subject: [PATCH 2/5] Learner Lab Part 2& Part3 completed --- .../java/com/github/curriculeon/Learner.java | 6 ++ .../java/com/github/curriculeon/Student.java | 24 ++++++++ .../com/github/curriculeon/TestStudent.java | 55 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 src/main/java/com/github/curriculeon/Learner.java create mode 100644 src/main/java/com/github/curriculeon/Student.java create mode 100644 src/test/java/com/github/curriculeon/TestStudent.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..5b2da1a --- /dev/null +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -0,0 +1,6 @@ +package com.github.curriculeon; + +public interface Learner { + void learn(Double numberOfHours); + Double getTotalStudyTime(); +} 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..ce61f34 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Student.java @@ -0,0 +1,24 @@ +package com.github.curriculeon; + +public class Student extends Person implements Learner { + + //Instance variable + private double totalStudyTime; + + //Constructor Student takes the argument of Long and String, passes the parameters to SuperConstructor + public Student(Long id, String name) { + super(id, name); + } +//Implement the method learn which takes the argument Double + @Override + public void learn(Double numberOfHours) { + //increments totalStudyTime variable by specified numberOfHours argument + this.totalStudyTime+=numberOfHours; + } + //Implement the method getTotalStudyTime which returns totalStudyTime instance variable + @Override + public Double getTotalStudyTime() { + return totalStudyTime; + + } +} diff --git a/src/test/java/com/github/curriculeon/TestStudent.java b/src/test/java/com/github/curriculeon/TestStudent.java new file mode 100644 index 0000000..9ccf33e --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -0,0 +1,55 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestStudent { + @Test + public void testImplementation(){ + + //given + Student student=new Student(null, null); + + + + //when + Boolean result=student instanceof Learner; + + + //then + Assert.assertTrue(result); + } + + @Test + public void testInheritance(){ + + + //given + Student student=new Student(null, null); + + + //when + Boolean result=student instanceof Person; + + //then + Assert.assertTrue(result); + } + + @Test + public void testLearn(){ + + //given + Student student=new Student(null, null); + Double numberOfHoursToLearn=98.0; + Double preStudyTime=student.getTotalStudyTime(); + Double expected=preStudyTime+numberOfHoursToLearn; + + //when + student.learn(numberOfHoursToLearn); + Double actual=student.getTotalStudyTime(); + + //then + Assert.assertEquals(expected, actual); + } + +} From 0498a386a5bb5e82a016cb19e4fc61176234a19a Mon Sep 17 00:00:00 2001 From: ANIL CHADDHA Date: Sat, 25 Jul 2020 19:48:05 -0400 Subject: [PATCH 3/5] Completed till Part 5 --- .../com/github/curriculeon/Instructor.java | 25 +++++ .../java/com/github/curriculeon/Person.java | 2 + .../java/com/github/curriculeon/Teacher.java | 6 ++ .../github/curriculeon/TestConstructor.java | 98 +++++++++++++++++++ .../com/github/curriculeon/TestPerson.java | 2 +- 5 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/github/curriculeon/Instructor.java create mode 100644 src/main/java/com/github/curriculeon/Teacher.java create mode 100644 src/test/java/com/github/curriculeon/TestConstructor.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..06d14b1 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -0,0 +1,25 @@ +package com.github.curriculeon; + +public class Instructor extends Person implements Teacher { + public Instructor(Long id, String name) { + super(id, name); + } + + @Override + public void teach(Learner learner, Double numberOfHours) { + //learner method invoking learn() method which invokes learn method on specified Learner object + learner.learn(numberOfHours); + } + + @Override + 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/Person.java b/src/main/java/com/github/curriculeon/Person.java index 8c74d9d..fdd8edc 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -22,4 +22,6 @@ public String getName() { public void setName(String name) { this.name = name; } + + } 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..a5b2831 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -0,0 +1,6 @@ +package com.github.curriculeon; + +public interface Teacher { + void teach(Learner learner, Double numberOfHours); + void lecture(Learner[] learners, double numberOfHours); +} diff --git a/src/test/java/com/github/curriculeon/TestConstructor.java b/src/test/java/com/github/curriculeon/TestConstructor.java new file mode 100644 index 0000000..2d11842 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestConstructor.java @@ -0,0 +1,98 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestConstructor { + //create a testImplementation method that asserts that an instructor is an instance of teacher + + @Test + public void testImplementation() { + + //given + Instructor instructor = new Instructor(null, null); + + //when + boolean outcome = instructor instanceof Teacher; + + + //then + Assert.assertTrue(outcome); + + // (all in one line)Assert.assertTrue(new Instructor(null, null)instanceof Teacher); + } + + @Test + public void testInheritance() { + + //given + Instructor instructor = new Instructor(null, null); + + //when + boolean outcome = instructor instanceof Person; + + + //then + Assert.assertTrue(outcome); + } + + @Test + public void testTeach(){ + + //given + Instructor instructor=new Instructor(null, null); + Learner learner=new Student(null, null); + Double numberOfHoursToTeach=134.0; + Double preStudyTime=learner.getTotalStudyTime(); + //total study time before giving the lecture + Double expected=preStudyTime + numberOfHoursToTeach; + + //when + //when an Instructor invokes teach method, respective student's totalStudyTime instance variable incremented by specified numberOfHours + //instructor is able to teach some learner for some hours, here teacher is teaching for some numberOfHoursToTeach + instructor.teach(learner, numberOfHoursToTeach); + Double actual=learner.getTotalStudyTime(); + + + + //then + //exected is equal to actual such that actual is equal to some TotalStudyTime + Assert.assertEquals(expected, actual); + + + } + + @Test + public void testLecture() { + //lecture method ensures when an instructor invokes lecture method, respective array of students totalStudyTime instance variables is incremented by numberOfHours/students.length + + //given + Teacher teacher=new Instructor(null, null); + Learner[] learners=new Learner[]{ + new Student(0L,"leon"), + new Student(1L, "Christopher"), + new Student(2L, "Leon Hunter") + + + }; + Double numberOfHours=128.0; + Double expected=numberOfHours/learners.length; + + //when + + teacher.lecture(learners, numberOfHours); + + + //then + + for (int i = 0; i < learners.length; i++) { + Learner learner = learners[i]; + Double actual=learner.getTotalStudyTime(); + Assert.assertEquals(expected, actual); + //Assert.assertEquals(numberOfHoursToTeach, totalStudyTime); + + } + + + } + } diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index b697e61..ddb9d85 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -15,7 +15,7 @@ public void testConstructor(){ //when //Giving the object we want to test, test when the constructor is called - Person person=new Person(expectedId, expectedName); + Person person = new Person(expectedId, expectedName); //Checking if actual output is same as expected String actualName=person.getName(); Long actualId=person.getId(); From 968648ef5cfb6e48ee9a95caba5ce0002a91391e Mon Sep 17 00:00:00 2001 From: ANIL CHADDHA Date: Sat, 25 Jul 2020 22:50:57 -0400 Subject: [PATCH 4/5] Part 6.0 completed --- .../java/com/github/curriculeon/People.java | 66 +++++++++++++++ .../java/com/github/curriculeon/Person.java | 10 ++- .../com/github/curriculeon/TestPeople.java | 81 +++++++++++++++++++ 3 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/github/curriculeon/People.java create mode 100644 src/test/java/com/github/curriculeon/TestPeople.java diff --git a/src/main/java/com/github/curriculeon/People.java b/src/main/java/com/github/curriculeon/People.java new file mode 100644 index 0000000..657547e --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -0,0 +1,66 @@ +package com.github.curriculeon; + + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + + + +//Created class People + +public class People implements Iterable { + + //Instantiated a List field of Person objects named personList + + List personList = new ArrayList<>(); + + //method named add adding Person to personList + public void add(Person personToBeAdded) { + personList.add(personToBeAdded); + } + +//Method named findById using long id as parameter, returning Person object with respective id field + public Person findById(long id) { + for (Person person : personList) { + if (person.getId() == id) { + return person; + } + } + + return null; + } + + //Method named contains using Person person parameter to return true if personList contains respective Person object + public Boolean contains(Person person) { + return personList.contains(person); + } + + //Method named remove using Person person parameter to remove respective Person object + public void remove(Person person) { + personList.remove(person); + } + + //Method named removeAll clearing personList field + public void removeAll(Person person) { + personList.clear(); + } + + //Method named count returning size of personList + public int count(){ + return personList.size(); + } + + //method named toArray returning an array representation of personList field + public Person[] toArray(){ + return personList.toArray(new Person[personList.size()]); + } + + //class implementing Iterable and defining a method named iterator which makes use of personList field to generate new Iterator + @Override + public Iterator iterator() { + return personList.iterator(); + } +} + + diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index fdd8edc..0d3eb17 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -6,9 +6,8 @@ public class Person { private final Long id; private String name; - public Person(Long id, String name) { - this.id = id; - this.name = name; + public Person() { + this.id = null; } public Long getId() { @@ -23,5 +22,8 @@ public void setName(String name) { this.name = name; } - + public Person(Long id, String name) { + this.id = id; + this.name = name; + } } diff --git a/src/test/java/com/github/curriculeon/TestPeople.java b/src/test/java/com/github/curriculeon/TestPeople.java new file mode 100644 index 0000000..582fb2c --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -0,0 +1,81 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestPeople { + @Test + public void testAdd() { + // given + People people = new People(); + Person person1 = new Person(); + Person person2 = new Person(); + Person person3 = new Person(); + + Assert.assertFalse(people.contains(person1)); + Assert.assertFalse(people.contains(person2)); + Assert.assertFalse(people.contains(person3)); + + // when + people.add(person1); + people.add(person2); + people.add(person3); + + // then + Assert.assertTrue(people.contains(person1)); + Assert.assertTrue(people.contains(person2)); + Assert.assertTrue(people.contains(person3)); + } + + @Test + public void testRemove() { + // given + People people = new People(); + Person person1 = new Person(); + Person person2 = new Person(); + Person person3 = new Person(); + + people.add(person1); + people.add(person2); + people.add(person3); + + Assert.assertTrue(people.contains(person1)); + Assert.assertTrue(people.contains(person2)); + Assert.assertTrue(people.contains(person3)); + + // when + people.remove(person1); + people.remove(person2); + people.remove(person3); + + // then + Assert.assertFalse(people.contains(person1)); + Assert.assertFalse(people.contains(person2)); + Assert.assertFalse(people.contains(person3)); + } + + @Test + public void testFindById() { + // given + People people = new People(); + Person expected = new Person(0L, null); + Person person2 = new Person(1L, null); + Person person3 = new Person(2L, null); + + people.add(expected); + people.add(person2); + people.add(person3); + + Assert.assertTrue(people.contains(expected)); + Assert.assertTrue(people.contains(person2)); + Assert.assertTrue(people.contains(person3)); + + // when + Person actual = people.findById(expected.getId()); + + // then + Assert.assertEquals(expected, actual); + } +} + + From ab66ee5a081fcbfae134e529577931c622b2417c Mon Sep 17 00:00:00 2001 From: ANIL CHADDHA Date: Tue, 28 Jul 2020 15:35:17 -0400 Subject: [PATCH 5/5] Parts 7 and 8 completed --- .../com/github/curriculeon/Instructors.java | 25 +++++++ .../java/com/github/curriculeon/People.java | 70 +++++++++++-------- .../java/com/github/curriculeon/Students.java | 28 ++++++++ .../github/curriculeon/TestInstructors.java | 19 +++++ .../com/github/curriculeon/TestPeople.java | 27 ++----- .../com/github/curriculeon/TestStudents.java | 26 +++++++ 6 files changed, 143 insertions(+), 52 deletions(-) create mode 100644 src/main/java/com/github/curriculeon/Instructors.java create mode 100644 src/main/java/com/github/curriculeon/Students.java create mode 100644 src/test/java/com/github/curriculeon/TestInstructors.java create mode 100644 src/test/java/com/github/curriculeon/TestStudents.java diff --git a/src/main/java/com/github/curriculeon/Instructors.java b/src/main/java/com/github/curriculeon/Instructors.java new file mode 100644 index 0000000..68b9c7f --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -0,0 +1,25 @@ +package com.github.curriculeon; + +import java.util.List; + + +public class Instructors extends People { + private static final Instructors instance = new Instructors(); + + public Instructors() { + this.add(new Instructor(0L, "Leon Hunter")); + this.add(new Instructor(1L, "Haseeb Muhammad")); + } + + @Override + public Instructor[] toArray() { + int sizeOfArray = count(); + Instructor[] destinationArray = new Instructor[sizeOfArray]; + List sourceList = personList; + return sourceList.toArray(destinationArray); + } + + public static Instructors getInstance() { + return instance; + } +} \ No newline at end of file diff --git a/src/main/java/com/github/curriculeon/People.java b/src/main/java/com/github/curriculeon/People.java index 657547e..e4fd19b 100644 --- a/src/main/java/com/github/curriculeon/People.java +++ b/src/main/java/com/github/curriculeon/People.java @@ -9,58 +9,68 @@ //Created class People -public class People implements Iterable { +public abstract class People implements Iterable { //Instantiated a List field of Person objects named personList +//Only Declaration + List personList; - List personList = new ArrayList<>(); + public People() { + this(new ArrayList<>()); + } + + public People(List personList) { + this.personList = personList; + } - //method named add adding Person to personList - public void add(Person personToBeAdded) { - personList.add(personToBeAdded); + public void add(SomeLearnerType person) { + this.personList.add(person); } -//Method named findById using long id as parameter, returning Person object with respective id field - public Person findById(long id) { - for (Person person : personList) { - if (person.getId() == id) { - return person; + public SomeLearnerType findById(long id) { + for (int i = 0; i < personList.size(); i++) { + SomeLearnerType person = personList.get(i); + if (person.getId() == id) { //if the id is correct, + return person; // return person + } else { // if its the wrong id, + continue; // keep lookin'! } - } + } // finished loop; we've finished lookin' + return null; // we were not able to find the person with the id + } - return null; + public SomeLearnerType findByIdExpanded(long id) { + for (int i = 0; i < personList.size(); i++) { + SomeLearnerType person = personList.get(i); + if (person.getId() == id) { //if the id is correct, + return person; // return person + } else { // if its the wrong id, + continue; // keep lookin'! + } + } // finished loop; we've finished lookin' + return null; // we were not able to find the person with the id } - //Method named contains using Person person parameter to return true if personList contains respective Person object - public Boolean contains(Person person) { - return personList.contains(person); + public Boolean contains(SomeLearnerType specifiedPerson) { + return personList.contains(specifiedPerson); } - //Method named remove using Person person parameter to remove respective Person object - public void remove(Person person) { - personList.remove(person); + public void remove(SomeLearnerType someSpecificPerson) { + personList.remove(someSpecificPerson); } - //Method named removeAll clearing personList field - public void removeAll(Person person) { + public void removeAll() { personList.clear(); } - //Method named count returning size of personList - public int count(){ + public int count() { return personList.size(); } - //method named toArray returning an array representation of personList field - public Person[] toArray(){ - return personList.toArray(new Person[personList.size()]); - } + abstract public SomeLearnerType[] toArray(); - //class implementing Iterable and defining a method named iterator which makes use of personList field to generate new Iterator @Override - public Iterator iterator() { + public Iterator iterator() { return personList.iterator(); } } - - diff --git a/src/main/java/com/github/curriculeon/Students.java b/src/main/java/com/github/curriculeon/Students.java new file mode 100644 index 0000000..f8b58f6 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Students.java @@ -0,0 +1,28 @@ +package com.github.curriculeon; + +import java.util.List; + +public class Students extends People { + private static final Students instance = new Students(); + + //private constructor to avoid client applications to use constructor + private Students(){ + super(); // optional invocation + this.add(new Student(0L, "Julia")); + this.add(new Student(1L, "David Y")); + this.add(new Student(2L, "Ghasan")); + } + + + @Override + public Student[] toArray() { + int sizeOfArray = count(); + Student[] destinationArray = new Student[sizeOfArray]; + List sourceList = personList; + return sourceList.toArray(destinationArray); + } + + public static Students getInstance(){ + return instance; + } +} \ No newline at end of file diff --git a/src/test/java/com/github/curriculeon/TestInstructors.java b/src/test/java/com/github/curriculeon/TestInstructors.java new file mode 100644 index 0000000..df26f37 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructors.java @@ -0,0 +1,19 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + + +public class TestInstructors { + @Test + public void test() { + Instructors instructors = Instructors.getInstance(); + List expectedNames = Arrays.asList("Leon Hunter", "Haseeb Muhammad"); + for(Instructor instructor : instructors) { + Assert.assertTrue(expectedNames.contains(instructor.getName())); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/github/curriculeon/TestPeople.java b/src/test/java/com/github/curriculeon/TestPeople.java index 582fb2c..6708106 100644 --- a/src/test/java/com/github/curriculeon/TestPeople.java +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -7,30 +7,22 @@ public class TestPeople { @Test public void testAdd() { // given - People people = new People(); + People people = Instructors.getInstance(); Person person1 = new Person(); - Person person2 = new Person(); - Person person3 = new Person(); Assert.assertFalse(people.contains(person1)); - Assert.assertFalse(people.contains(person2)); - Assert.assertFalse(people.contains(person3)); // when people.add(person1); - people.add(person2); - people.add(person3); // then Assert.assertTrue(people.contains(person1)); - Assert.assertTrue(people.contains(person2)); - Assert.assertTrue(people.contains(person3)); } @Test public void testRemove() { // given - People people = new People(); + People people = Instructors.getInstance(); Person person1 = new Person(); Person person2 = new Person(); Person person3 = new Person(); @@ -57,18 +49,11 @@ public void testRemove() { @Test public void testFindById() { // given - People people = new People(); + People people = Instructors.getInstance(); + people.removeAll(); Person expected = new Person(0L, null); - Person person2 = new Person(1L, null); - Person person3 = new Person(2L, null); - people.add(expected); - people.add(person2); - people.add(person3); - Assert.assertTrue(people.contains(expected)); - Assert.assertTrue(people.contains(person2)); - Assert.assertTrue(people.contains(person3)); // when Person actual = people.findById(expected.getId()); @@ -76,6 +61,4 @@ public void testFindById() { // then Assert.assertEquals(expected, actual); } -} - - +} \ No newline at end of file diff --git a/src/test/java/com/github/curriculeon/TestStudents.java b/src/test/java/com/github/curriculeon/TestStudents.java new file mode 100644 index 0000000..75eaf17 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudents.java @@ -0,0 +1,26 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + + +public class TestStudents { + @Test + public void test() { + Students students = Students.getInstance(); + String[] studentNameArray = {"Julia", "David Y", "Ghasan"}; + List studentNameList = Arrays.asList(studentNameArray); + + Person[] studentArray = students.toArray(); + for (int i = 0; i < studentArray.length; i++) { + Person person = studentArray[i]; + String personName = person.getName(); + boolean hasPersonWithName = studentNameList.contains(personName); + Assert.assertTrue(hasPersonWithName); + } + } +} \ No newline at end of file