From 322bc9500968f6ea9b989347c0defa64a8d60591 Mon Sep 17 00:00:00 2001 From: monicadeshmukh Date: Mon, 20 Jul 2020 11:09:09 -0400 Subject: [PATCH 01/10] completed part 1.0, 1.1 --- .../java/com/github/curriculeon/Person.java | 33 +++++++++++++- .../com/github/curriculeon/TestPerson.java | 44 +++++++++++++++++++ .../com/github/curriculeon/TestSetName.java | 39 ++++++++++++++++ 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/github/curriculeon/TestSetName.java diff --git a/src/main/java/com/github/curriculeon/Person.java b/src/main/java/com/github/curriculeon/Person.java index 3c8350b..057ccc1 100644 --- a/src/main/java/com/github/curriculeon/Person.java +++ b/src/main/java/com/github/curriculeon/Person.java @@ -1,5 +1,36 @@ package com.github.curriculeon; - +/** + * Create a Person class. + * The class should declare a final field named id of type long. + * The class should declare a field named name of type String. + * Person constructor should have a parameter of type long and String which sets the id and name field to the respective values. + * The class should define a getId() method which returns the Person object's id field. + * The class should define a getName() method which returns the Person object's name field. + * The class should define a setName() method which sets the Person object's name field. + */ +/** + * Implemented Person Class + * Monica Deshmukh + *07/20/2020 + */ public class Person { + final long id; + String name; + + Person(long id, String name){ + this.id = id; + this.name = name; + } + + public void setName(String name){ + this.name = name; + } + + public long getId(){ + return id; + } + public String getName(){ + return name; + } } diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index 6c523fe..b802d17 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -1,5 +1,49 @@ package com.github.curriculeon; +/** + * Create a TestPerson class. + * Create a testConstructor method which ensures that a Person object's id and name field + * are being set upon construction. + */ + +import org.junit.Assert; +import org.junit.Test; + +import java.time.LocalDate; +import java.time.Period; +import java.time.ZoneId; +import java.util.Date; + +/** + * Implemented by Monica Deshmukh + * 07/20/2020 + */ public class TestPerson { + private void testConstructor(long expectedId, String expectedName) { + + // When + Person person = new Person(expectedId, expectedName); + + // Then + long actualId = person.getId(); + String actualName = person.getName(); + + Assert.assertEquals(expectedId, actualId); + Assert.assertEquals(expectedName, actualName); + } + @Test + public void test0() { + testConstructor(0, null); + } + + @Test + public void test1() { + testConstructor(1, "Monica"); + } + + @Test + public void test2() { + testConstructor(2, "Deshmukh"); + } } diff --git a/src/test/java/com/github/curriculeon/TestSetName.java b/src/test/java/com/github/curriculeon/TestSetName.java new file mode 100644 index 0000000..dbf848a --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestSetName.java @@ -0,0 +1,39 @@ +package com.github.curriculeon; + +/*** Create a testSetName method which ensures that a Person object's name variable is being set + * by invoking the .setName method. + */ + +import org.junit.Assert; +import org.junit.Test; + +/** Implemented by Monica Deshmukh + * 07/20/2020 + */ +public class TestSetName { + private void testSetName(String expectedName) { + String name = expectedName + 1; //initialize name to a different value than expectedName + Person person = new Person(1, name); + person.setName(expectedName); //change the name using setName() method + String actualName = person.getName(); //assert if the name is changed + + Assert.assertEquals(expectedName, actualName); + } + + @Test + public void test0() { + testSetName("Monica"); + } + + @Test + public void test1() { + testSetName(""); + } + + @Test + public void test2() { + testSetName(null); + } +} + + From 102cb9173d808c8e536bebb1686c8ceeb9da3c2c Mon Sep 17 00:00:00 2001 From: monicadeshmukh Date: Mon, 20 Jul 2020 11:29:05 -0400 Subject: [PATCH 02/10] added more tests to person class --- .../java/com/github/curriculeon/TestPerson.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index b802d17..ba2ca09 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -35,15 +35,22 @@ private void testConstructor(long expectedId, String expectedName) { public void test0() { testConstructor(0, null); } - @Test public void test1() { - testConstructor(1, "Monica"); + testConstructor(1, ""); } - @Test public void test2() { - testConstructor(2, "Deshmukh"); + testConstructor(-1, "Monica"); + } + + @Test + public void test3() { + testConstructor(Long.MAX_VALUE, "Deshmukh"); + } + + public void test4() { + testConstructor(Long.MIN_VALUE, "Deshmukh"); } } From dca36750c9ca1257c86235442abce66f7c9c4f90 Mon Sep 17 00:00:00 2001 From: monicadeshmukh Date: Tue, 21 Jul 2020 15:05:26 -0400 Subject: [PATCH 03/10] 2, 3, 4, 5 completed --- .../com/github/curriculeon/Instructor.java | 37 +++++++++ .../java/com/github/curriculeon/Learner.java | 21 +++++ .../java/com/github/curriculeon/Student.java | 34 ++++++++ .../java/com/github/curriculeon/Teacher.java | 31 +++++++ .../github/curriculeon/TestInstructor.java | 83 +++++++++++++++++++ .../com/github/curriculeon/TestPerson.java | 6 +- .../com/github/curriculeon/TestSetName.java | 5 ++ .../com/github/curriculeon/TestStudent.java | 63 ++++++++++++++ 8 files changed, 277 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/github/curriculeon/Instructor.java 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/main/java/com/github/curriculeon/Teacher.java create mode 100644 src/test/java/com/github/curriculeon/TestInstructor.java create mode 100644 src/test/java/com/github/curriculeon/TestStudent.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..77daf4a --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructor.java @@ -0,0 +1,37 @@ +package com.github.curriculeon; + +/** + * Create Instructor Class + * Create an Instructor class such that: + * Instructor is a subclass of Person + * Instructor implements the Teacher interface + * Instructor should have a concrete implementation of the teach method + * which invokes the learn method on the specified Learner object. + * Instructor should have a concrete implementation of the lecture method + * which invokes the learn method on each of the elements in the specified array of Learner objects. + * numberOfHours should be evenly split amongst the learners. + * double numberOfHoursPerLearner = numberOfHours / learners.length; + */ + +/** + * Implemented By Monica Deshmukh + * 07/21/2020 + */ +public class Instructor extends Person implements Teacher { + + Instructor(long id, String name) { + super(id, name); + } + + @Override + public void teach(Learner learner, double numberOfHours) { + learner.learn(numberOfHours); + } + + @Override + public void lecture(Learner[] learners, double numberOfHours) { + for (Learner learner : learners) { + learner.learn(numberOfHours); + } + } +} 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..465b74a --- /dev/null +++ b/src/main/java/com/github/curriculeon/Learner.java @@ -0,0 +1,21 @@ +package com.github.curriculeon; + +/** + * Create a Learner interface. + * Learner should declare method signature: + * Method name: learn + * Method parameters: double numberOfHours + * Method return-type: void + * Learner should declare method signature: + * Method name: getTotalStudyTime + * Method return-type: Double + */ + +/** + * Implemeted By Monica Deshmukh + * 07/20/2020 + */ +public interface Learner { + public void learn (double numberOfHours); + public 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..823c2f6 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Student.java @@ -0,0 +1,34 @@ +package com.github.curriculeon; + +/** + * Create Student Class + * Create a Student class such that: + * Student is a subclass of Person + * Student implements the Learner interface + * Student should have an instance variable totalStudyTime of type double + * Student should have a concrete implementation of the learn method which increments + * the totalStudyTime variable by the specified numberOfHours argument. + * Student should have a getTotalStudyTime() method which returns the totalStudyTime instance variable. + */ + +/** + * Implemented By Monica Deshmukh + * 07/20/2020 + */ +public class Student extends Person implements Learner{ + double totalStudyTime; + + public Student(long id, String name){ + super(id, name); + totalStudyTime = 0; + } + @Override + public void learn(double numberOfHours) { + totalStudyTime += numberOfHours; + } + + @Override + public Double getTotalStudyTime() { + return totalStudyTime; + } +} 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..5fcf0a7 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Teacher.java @@ -0,0 +1,31 @@ +package com.github.curriculeon; + +/** + * Create Teacher Interface + * + * Create a Teacher interface. + * Teacher should declare a teach method signature: + * + * Method name: teach + * Method parameters: + * Learner learner + * double numberOfHours + * Method return-type: void + * + * Teacher should declare a lecture method signature: + * + * Method name: lecture + * Method parameters: + * Learner[] learners + * double numberOfHours + * Method return-type: void + */ + +/** + * Implemented by Monica Deshmukh + * 07/21/2020 + */ +public interface Teacher { + void teach(Learner learner, double numberOfHours); + void lecture(Learner[] learners, double numberOfHours); +} diff --git a/src/test/java/com/github/curriculeon/TestInstructor.java b/src/test/java/com/github/curriculeon/TestInstructor.java new file mode 100644 index 0000000..5145b51 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -0,0 +1,83 @@ +package com.github.curriculeon; + +/** + * Create a TestInstructor class. + * Create a testImplementation method that asserts that an Instructor is an instanceof a Teacher. + * Create a testInheritance method that asserts that a Instructor is an instanceof a Person. + * Create a testTeach method that ensures when an Instructor invokes the teach method, + * a respective student's totalStudyTime instance variable is incremented by the specified numberOfHours. + * Create a testLecture method that ensures when an Instructor invokes the lecture method, + * a respective array of students' totalStudyTime instance variables is incremented by numberOfHours/students.length. + */ + +import org.junit.Assert; +import org.junit.Test; + +/** + * Implemented by Monica Deshmukh + * 07/21/2020 + */ +public class TestInstructor { + @Test + public void testImplementation() { + //given + Instructor instructor = new Instructor(1, "Monica"); + + //when + Boolean outcome = instructor instanceof Teacher; + + // then + Assert.assertTrue(outcome); + } + + @Test + public void testInheritance() { + //given + Instructor instructor = new Instructor(1, "Monica"); + + //when + Boolean outcome = instructor instanceof Person; + + // then + Assert.assertTrue(outcome); + } + + @Test + public void testTeach() { + //given + Instructor instructor = new Instructor(1, "Monica"); + Student student = new Student(2, "student1"); + Double expectedStudyTime = 10D; + //when + instructor.teach(student, expectedStudyTime); + Double actualStudyTime = student.getTotalStudyTime(); + + //then + Assert.assertEquals(actualStudyTime, expectedStudyTime); + } + + @Test + public void testLecture() { + //given + Instructor instructor = new Instructor(11, "Monica"); + Student[] students = new Student[3]; + students[0] = new Student(1, "student1"); + students[1] = new Student(2, "student2"); + students[2] = new Student(3, "student3"); + + Double expectedStudyTime = 10D; + + //when + instructor.lecture(students, expectedStudyTime); + + Double actualTotalStudyTime = 0D; + for (Student s: students) { + actualTotalStudyTime += s.getTotalStudyTime(); + } + Double actualStudyTime = actualTotalStudyTime/students.length; + + //then + Assert.assertEquals(actualStudyTime, expectedStudyTime); + + } +} diff --git a/src/test/java/com/github/curriculeon/TestPerson.java b/src/test/java/com/github/curriculeon/TestPerson.java index ba2ca09..04c23c0 100644 --- a/src/test/java/com/github/curriculeon/TestPerson.java +++ b/src/test/java/com/github/curriculeon/TestPerson.java @@ -20,14 +20,14 @@ */ public class TestPerson { private void testConstructor(long expectedId, String expectedName) { - + //given: expectedId, expectedName // When Person person = new Person(expectedId, expectedName); - // Then + long actualId = person.getId(); String actualName = person.getName(); - + //then Assert.assertEquals(expectedId, actualId); Assert.assertEquals(expectedName, actualName); } diff --git a/src/test/java/com/github/curriculeon/TestSetName.java b/src/test/java/com/github/curriculeon/TestSetName.java index dbf848a..c1a21e0 100644 --- a/src/test/java/com/github/curriculeon/TestSetName.java +++ b/src/test/java/com/github/curriculeon/TestSetName.java @@ -12,9 +12,14 @@ */ public class TestSetName { private void testSetName(String expectedName) { + //given String name = expectedName + 1; //initialize name to a different value than expectedName Person person = new Person(1, name); + + //when person.setName(expectedName); //change the name using setName() method + + //then String actualName = person.getName(); //assert if the name is changed Assert.assertEquals(expectedName, actualName); 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..5955fb0 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -0,0 +1,63 @@ +package com.github.curriculeon; + +/** + * Test Student + * Create a TestStudent class. + * Create a testImplementation method that asserts that a Student is an instanceof a Learner. + * Create a testInheritance method that asserts that a Student is an instanceof a Person. + * Create a testLearn method that ensures a Student's totalStudyTime instance variable + * is incremented by the specified numberOfHours by invoking the .learn method. + */ + +import org.junit.Assert; +import org.junit.Test; + +/** + * Implemented By Monica Deshmukh + * 07/20/2020 + */ +public class TestStudent { + @Test + public void testImplementation() { + //given a studentObject + long id = 1; + String name = "Monica"; + Student studentObject = new Student(id,name); + + //when a learner object could be created using a student object + Learner learnerObject = studentObject; + + //then asserts that a student is an instance of a learner object + Assert.assertEquals(studentObject, learnerObject); + } + + @Test + public void testInheritance() { + //given a student object + long id = 1; + String name = "Monica"; + Student studentObject = new Student(id,name); + + //when a personObject could be created using a studentObject + Person personObject = studentObject; + + //then asserts that a student is an instance of a person + Assert.assertEquals(studentObject, personObject); + } + + @Test + public void testLearn(){ + //given a student object + long id = 1; + String name = "Monica"; + Student student = new Student(id, name); + double expectedStudyHours = 10; + //when + student.learn(expectedStudyHours); + Double actualStudyTime = student.getTotalStudyTime(); + //then + Assert.assertEquals((Double)expectedStudyHours,actualStudyTime); + } + + +} From 95dc45cf1d4721e32b02f5bc13bbe91e6ecdc1d8 Mon Sep 17 00:00:00 2001 From: monicadeshmukh Date: Tue, 21 Jul 2020 15:21:46 -0400 Subject: [PATCH 04/10] updated testLecture --- src/test/java/com/github/curriculeon/TestInstructor.java | 9 +++++++-- src/test/java/com/github/curriculeon/TestStudent.java | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/github/curriculeon/TestInstructor.java b/src/test/java/com/github/curriculeon/TestInstructor.java index 5145b51..ec7ba05 100644 --- a/src/test/java/com/github/curriculeon/TestInstructor.java +++ b/src/test/java/com/github/curriculeon/TestInstructor.java @@ -64,6 +64,11 @@ public void testLecture() { students[0] = new Student(1, "student1"); students[1] = new Student(2, "student2"); students[2] = new Student(3, "student3"); + Double initialStudyTime = 0D; + + for (Student s: students) { + initialStudyTime += s.getTotalStudyTime(); + } Double expectedStudyTime = 10D; @@ -72,9 +77,9 @@ public void testLecture() { Double actualTotalStudyTime = 0D; for (Student s: students) { - actualTotalStudyTime += s.getTotalStudyTime(); + actualTotalStudyTime += s.getTotalStudyTime(); } - Double actualStudyTime = actualTotalStudyTime/students.length; + Double actualStudyTime = (initialStudyTime + actualTotalStudyTime)/students.length; //then Assert.assertEquals(actualStudyTime, expectedStudyTime); diff --git a/src/test/java/com/github/curriculeon/TestStudent.java b/src/test/java/com/github/curriculeon/TestStudent.java index 5955fb0..6e85109 100644 --- a/src/test/java/com/github/curriculeon/TestStudent.java +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -51,10 +51,11 @@ public void testLearn(){ long id = 1; String name = "Monica"; Student student = new Student(id, name); + Double initialStudyHours = student.getTotalStudyTime(); double expectedStudyHours = 10; //when student.learn(expectedStudyHours); - Double actualStudyTime = student.getTotalStudyTime(); + Double actualStudyTime = initialStudyHours + student.getTotalStudyTime(); //then Assert.assertEquals((Double)expectedStudyHours,actualStudyTime); } From bf6d3a961896c2f96cb7bf22211c6923c7e9238f Mon Sep 17 00:00:00 2001 From: monicadeshmukh Date: Wed, 22 Jul 2020 16:29:46 -0400 Subject: [PATCH 05/10] part 6 completed --- .../java/com/github/curriculeon/People.java | 96 +++++++++++++++++++ .../com/github/curriculeon/TestPeople.java | 79 +++++++++++++++ 2 files changed, 175 insertions(+) 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..3c9cb04 --- /dev/null +++ b/src/main/java/com/github/curriculeon/People.java @@ -0,0 +1,96 @@ +package com.github.curriculeon; + +import java.util.*; + +/** + * Implemented By Monica Deshmukh + * 07/22/2020 + */ + +/** + * Create a People class. +*/ +public class People implements Iterable{ + /* + * The class should instantiate a List field of Person objects named personList. + */ + List personList = new ArrayList(); + + /* + * The class should define a method named add which adds a Person to the personList. + */ + public void add(Person person) { + personList.add(person); + } + /* + * The class should define a method named findById which makes use of + * a long id parameter to return a Person object with the respective id field. + */ + public Person findById(long id) { + for (Person person: personList) { + if (person.getId() == id) + return person; + } + return null; //person with id not found + } + /* + * The class should define a method named contains which makes use of + * a Person person parameter to return true if the personList contains the respective Person object. + */ + public Boolean contains(Person person) { + if (personList.contains(person)) + return true; + return false; + } + /** + * The class should define a method named remove which makes use of + * * a Person person parameter to remove a respective Person object. + */ + public void remove(Person person) { + if (this.contains(person)) + personList.remove(person); + } + + /** + * * The class should define a method named remove which makes use of + * * a long id parameter to remove a Person object with the respective id field. + * + */ + public void remove(long id) { + remove(findById(id)); + } + /** + * * The class should define a named removeAll which clears our personList field. + */ + public void removeAll() { + personList.clear(); + } + /** + * * The class should define a method named count which returns the size of personList. + */ + public int count() { + return personList.size(); + } + + /** + * * The class should define a method named toArray + * which returns an array representation of the personList field. + * + */ + public Person[] toArray() { + Person[] personArray = new Person[personList.size()]; + personList.toArray(personArray); + return personArray; + } + + /** + * * The class should implement Iterable and define a method named iterator + * * which makes use of the personList field to generate a new a Iterator. + */ + @Override + public Iterator iterator() { + return personList.iterator(); + //return null; + } + +} 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..d5fc777 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -0,0 +1,79 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Create a TestPeople class. + * Create a testAdd method which ensures that our personList in our People class + * populated with respective Person objects following invokation of the add method. + * Create a testRemove method which ensures that the personList in a People object + * is depopulated with a respective Person object following the invokation of the remove method. + * Create a testFindById method which ensures that a respective Person object with a respective id field + * is returned upon invokation of the findById method on a respective People object. + */ +public class TestPeople { + @Test + public void testAdd() { + //given + People people = new People(); + + //when + Person person1 = new Person(1, "person1"); + Person person2 = new Person(2, "person2"); + Person person3 = new Person(3, "person3"); + 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(); + + //when + //add two persons to the list. + //make sure they are added to the list. + //then remove a person from the list. + Person person1 = new Person(1, "person1"); + Person person2 = new Person(2, "person2"); + people.add(person1); + people.add(person2); + + Assert.assertTrue(people.contains(person1)); + Assert.assertTrue(people.contains(person2)); + + people.remove(person1); + + //then + //assert that the person is removed from the list + Assert.assertFalse(people.contains(person1)); + } + + @Test + public void testFindById() { + //given + People people = new People(); + + //when + Person person1 = new Person(1, "person1"); + Person person2 = new Person(2, "person2"); + Person person3 = new Person(3, "person3"); + people.add(person1); + people.add(person2); + people.add(person3); + + //then + Assert.assertEquals(person1,people.findById(1)); + Assert.assertEquals(person2,people.findById(2)); + Assert.assertEquals(person3,people.findById(3)); + + Assert.assertNotEquals(person1,people.findById(4)); + } +} From 43880957971a8784b5b4197ec8f22c9eb6fad781 Mon Sep 17 00:00:00 2001 From: monicadeshmukh Date: Mon, 27 Jul 2020 21:58:57 -0400 Subject: [PATCH 06/10] students class part 7 completed --- .../java/com/github/curriculeon/People.java | 7 +++++ .../java/com/github/curriculeon/Students.java | 31 +++++++++++++++++++ .../com/github/curriculeon/TestStudents.java | 31 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 src/main/java/com/github/curriculeon/Students.java create mode 100644 src/test/java/com/github/curriculeon/TestStudents.java diff --git a/src/main/java/com/github/curriculeon/People.java b/src/main/java/com/github/curriculeon/People.java index 3c9cb04..db1c67a 100644 --- a/src/main/java/com/github/curriculeon/People.java +++ b/src/main/java/com/github/curriculeon/People.java @@ -33,6 +33,13 @@ public Person findById(long id) { } return null; //person with id not found } + + + /* findById using stream*/ + /** public Person findById(long id) { + return personList.stream().filter(person -> person.getId() == id).findFirst().get(); + }*/ + /* * The class should define a method named contains which makes use of * a Person person parameter to return true if the personList contains the respective Person object. 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..ae60d18 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Students.java @@ -0,0 +1,31 @@ +package com.github.curriculeon; + +/** + * Note: The creation of this class will demonstrate an implementation of singleton design pattern. + * Create a Students class. + * The class should be an unextendable subclass of the People class. + * The class should statically instantiate a final field named INSTANCE of type Students. + * The class should define a private nullary constructor which populates the INSTANCE field + * with respective Student representations of your colleagues. + * Each student should have a relatively unique id field. + * The class should define a getInstance method which returns the INSTANCE field. + */ + +/** + * Implemented By Monica Deshmukh 07/24/2020 + */ +public class Students extends People{ + private static final Students INSTANCE = new Students(); + private Students() { + this.add(new Person(1, "student1")); + this.add(new Person(2, "student2")); + this.add(new Person(3, "student3")); + } + + public static Students getInstance() { + + return INSTANCE; + } +} + + 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..ec60427 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestStudents.java @@ -0,0 +1,31 @@ +package com.github.curriculeon; +import org.junit.Assert; +import org.junit.Test; +/** + * Create a TestStudents class. + * Create a test method which ensures that each of the students in your current cohort + * are in your Students singleton. + */ + +/** + * Implemented by Monica Deshmukh 07/24/2020 + */ +public class TestStudents { + @Test + public void testStudents() { + //given + Students students = Students.getInstance(); + Person[] studentCohort = { new Person(1, "student1"), + new Person(2, "student2"), + new Person(3, "student3")}; + //when + Person[] studentArray = students.toArray(); + + //then + for (int i = 0; i< 3; i++){ + Assert.assertEquals(studentArray[0].getId(), studentCohort[0].getId()); + //Assert.assertTrue(students.contains(studentCohort[i])); : physical Person objects are different, their properties are same + } + } + +} From 208065705ae2b23b6caead1c61597b882f760baa Mon Sep 17 00:00:00 2001 From: monicadeshmukh Date: Mon, 27 Jul 2020 22:58:07 -0400 Subject: [PATCH 07/10] Part 8 Instructors completed --- .../com/github/curriculeon/Instructors.java | 24 +++++++++++++++++++ .../github/curriculeon/TestInstructors.java | 22 +++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/main/java/com/github/curriculeon/Instructors.java create mode 100644 src/test/java/com/github/curriculeon/TestInstructors.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..45710f3 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -0,0 +1,24 @@ +package com.github.curriculeon; + +/** + * Use Part 7 as a reference. + * Create a Instructors singleton which represents the set of instructors. + * Create a TestInstructors class. + */ + +/** + * Implemented by Monica Deshmukh + * 07/27/2020 + */ +public class Instructors extends People{ + private static final Instructors INSTANCE = new Instructors(); + + private Instructors() { + this.add(new Person(11,"instructor1")); + this.add(new Person(12,"instructor2")); + } + + public static Instructors getInstance() { + return INSTANCE; + } +} 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..bceb565 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestInstructors.java @@ -0,0 +1,22 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +public class TestInstructors { + @Test + public void testInstructors(){ + //given + Instructors instructors = Instructors.getInstance(); + Person[] instructorsExpected = {new Person(11, "instructor1"), + new Person(12, "instructor2")}; + + //when + Person[] instructorsActual = instructors.toArray(); + + //then + for(int i = 0; i < 2; i++){ + Assert.assertEquals(instructorsExpected[i].getName(), instructorsActual[i].getName()); + } + } +} From 93f6693f965ec544fc9df115f7b6085ca2dd12eb Mon Sep 17 00:00:00 2001 From: monicadeshmukh Date: Tue, 28 Jul 2020 17:28:52 -0400 Subject: [PATCH 08/10] completed part 9 classroom --- .../com/github/curriculeon/Classroom.java | 47 +++++++++++++++++++ .../com/github/curriculeon/TestClassroom.java | 39 +++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/main/java/com/github/curriculeon/Classroom.java create mode 100644 src/test/java/com/github/curriculeon/TestClassroom.java diff --git a/src/main/java/com/github/curriculeon/Classroom.java b/src/main/java/com/github/curriculeon/Classroom.java new file mode 100644 index 0000000..b7ffe40 --- /dev/null +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -0,0 +1,47 @@ +package com.github.curriculeon; + +import java.util.*; + +/** + * Create a Classroom singleton. + * The class should declare a field that references the instance of Students called students. + * The class should declare a field that references the instance of Instructors called instructors. + * The class should define a method hostLecture which makes use of a Teacher teacher, + * double numberOfHours parameter to host a lecture + * to the composite personList field in the students reference. + * The class should define a method hostLecture which makes use of a long id, + * double numberOfHours parameter to identify a respective Instructor + * to host a lecture to the composite personList field in the cohort reference. + * The class should define a method getStudyMap which returns a new instance of a mapping + * from Student objects to Double objects, representative of each respective student's totalStudyTime. + */ +public class Classroom { + private static final Students students = Students.getInstance(); + private static final Instructors instructors = Instructors.getInstance(); + + public void hostLecture(Teacher teacher, double numberOfHours) { + teacher.lecture((Learner[]) students.toArray(), numberOfHours); + } + + public void hostLecture(long id, double numberOfHours){ + Teacher teacher; + if (instructors.findById(id) instanceof Teacher) { + teacher = (Teacher) instructors.findById(id); + this.hostLecture(teacher, numberOfHours); + } + } + + public Map getStudyMap(){ + Iterator iterator = students.iterator(); + Map map = new HashMap<>(); + Student s ; + + while(iterator.hasNext()){ + if (iterator.next() instanceof Student){ + s = (Student)iterator.next(); + map.put(s, s.getTotalStudyTime()); + } + } + return map; + } +} diff --git a/src/test/java/com/github/curriculeon/TestClassroom.java b/src/test/java/com/github/curriculeon/TestClassroom.java new file mode 100644 index 0000000..274e142 --- /dev/null +++ b/src/test/java/com/github/curriculeon/TestClassroom.java @@ -0,0 +1,39 @@ +package com.github.curriculeon; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +/** + * Create a Classroom class. + * Create a testHostLecture method which ensures that each of the Student's totalStudyTime instance variable + * is incremented by the specified numberOfHours upon invoking the hostLecture method. + */ +public class TestClassroom { + @Test + public void testHostLecture(){ + //given + Classroom classroom = new Classroom(); + double initialStudyHours, studyHoursAfterLecture; + double numberOfHoursStudied = 10; + + Map initialMap = classroom.getStudyMap(); + + //when + classroom.hostLecture(11, numberOfHoursStudied); + Map mapAfterHostLecture = classroom.getStudyMap(); + + //then + //for each student get initial study hours and compare with studyhours after lecture + for (Student student : initialMap.keySet()) + { + // search for value + initialStudyHours = initialMap.get(student); + studyHoursAfterLecture = mapAfterHostLecture.get(student); + Assert.assertEquals((initialStudyHours+ numberOfHoursStudied), studyHoursAfterLecture); + } + } +} From 11b6e34569029000da391c4953e915d47f787a73 Mon Sep 17 00:00:00 2001 From: monicadeshmukh Date: Tue, 28 Jul 2020 21:28:05 -0400 Subject: [PATCH 09/10] completed part 10.1,2,3,4 --- .../com/github/curriculeon/Classroom.java | 14 +++-- .../com/github/curriculeon/Instructors.java | 15 ++++- .../java/com/github/curriculeon/People.java | 56 ++++++++++++------- .../java/com/github/curriculeon/Students.java | 29 +++++++++- .../com/github/curriculeon/TestClassroom.java | 3 +- .../github/curriculeon/TestInstructors.java | 4 +- .../com/github/curriculeon/TestPeople.java | 33 +++++++---- .../com/github/curriculeon/TestStudent.java | 6 +- .../com/github/curriculeon/TestStudents.java | 14 +++-- 9 files changed, 122 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/github/curriculeon/Classroom.java b/src/main/java/com/github/curriculeon/Classroom.java index b7ffe40..98bedb2 100644 --- a/src/main/java/com/github/curriculeon/Classroom.java +++ b/src/main/java/com/github/curriculeon/Classroom.java @@ -14,7 +14,10 @@ * to host a lecture to the composite personList field in the cohort reference. * The class should define a method getStudyMap which returns a new instance of a mapping * from Student objects to Double objects, representative of each respective student's totalStudyTime. - */ +*/ + +/* Refactor the hostLecture method in the Classroom class by removing any intermediate casting trick(s).*/ + public class Classroom { private static final Students students = Students.getInstance(); private static final Instructors instructors = Instructors.getInstance(); @@ -32,15 +35,16 @@ public void hostLecture(long id, double numberOfHours){ } public Map getStudyMap(){ - Iterator iterator = students.iterator(); + Iterator iterator = students.iterator(); Map map = new HashMap<>(); Student s ; while(iterator.hasNext()){ - if (iterator.next() instanceof Student){ - s = (Student)iterator.next(); + //if (iterator.next() instanceof Student){ + //s = (Student)iterator.next(); + s = iterator.next(); map.put(s, s.getTotalStudyTime()); - } + //} } return map; } diff --git a/src/main/java/com/github/curriculeon/Instructors.java b/src/main/java/com/github/curriculeon/Instructors.java index 45710f3..50d3ee0 100644 --- a/src/main/java/com/github/curriculeon/Instructors.java +++ b/src/main/java/com/github/curriculeon/Instructors.java @@ -6,6 +6,8 @@ * Create a TestInstructors class. */ +import java.util.List; + /** * Implemented by Monica Deshmukh * 07/27/2020 @@ -14,11 +16,20 @@ public class Instructors extends People{ private static final Instructors INSTANCE = new Instructors(); private Instructors() { - this.add(new Person(11,"instructor1")); - this.add(new Person(12,"instructor2")); + this.add(new Instructor(11,"instructor1")); + this.add(new Instructor(12,"instructor2")); } public static Instructors getInstance() { return INSTANCE; } + + @Override + public Instructor[] toArray() { + //return INSTANCE.toArray(); + Instructor[] instructorArray = new Instructor[personList.size()];//should make a method in Person class to retrun personList.size() + List instructorList = personList; + instructorList.toArray(instructorArray); + return instructorArray; + } } diff --git a/src/main/java/com/github/curriculeon/People.java b/src/main/java/com/github/curriculeon/People.java index db1c67a..512cbcb 100644 --- a/src/main/java/com/github/curriculeon/People.java +++ b/src/main/java/com/github/curriculeon/People.java @@ -10,26 +10,48 @@ /** * Create a People class. */ -public class People implements Iterable{ + +/** + * Parameterize the People signature to enforce that it is a container for objects of type E + * such that E is a subclass of Person. + * Modify the class signature to declare this class abstract. + * An abstract class cannot be instantiated; Its concrete implementation is deferred to its subclass. + * Modify people field to enforce that is a container of objects of type E. + * Modify the add method to ensure that it handles object of type E. + * Modify the findById method to ensure that it returns an object of type E. + * Modify the getArray method signature by declaring it abstract of return type E. + * An abstract method is a subclass's contractual agreement to the deferment + * of an implementation of a respective method. + */ + +/** + * Implemented by Monica Deshmukh 07/28/2020 + */ +public abstract class People implements Iterable{ /* * The class should instantiate a List field of Person objects named personList. */ - List personList = new ArrayList(); + List personList; + + public People() { + personList = new ArrayList(); + } /* * The class should define a method named add which adds a Person to the personList. */ - public void add(Person person) { - personList.add(person); + public void add(E personType) { + personList.add(personType); } /* * The class should define a method named findById which makes use of * a long id parameter to return a Person object with the respective id field. */ - public Person findById(long id) { - for (Person person: personList) { - if (person.getId() == id) - return person; + public E findById(long id) { + for (E personType: personList) { + //if (personList instanceof Person) + if (personType.getId() == id) + return personType; } return null; //person with id not found } @@ -44,8 +66,8 @@ public Person findById(long id) { * The class should define a method named contains which makes use of * a Person person parameter to return true if the personList contains the respective Person object. */ - public Boolean contains(Person person) { - if (personList.contains(person)) + public Boolean contains(E personType) { + if (personList.contains(personType)) return true; return false; } @@ -53,9 +75,9 @@ public Boolean contains(Person person) { * The class should define a method named remove which makes use of * * a Person person parameter to remove a respective Person object. */ - public void remove(Person person) { - if (this.contains(person)) - personList.remove(person); + public void remove(E personType) { + if (this.contains(personType)) + personList.remove(personType); } /** @@ -84,18 +106,14 @@ public int count() { * which returns an array representation of the personList field. * */ - public Person[] toArray() { - Person[] personArray = new Person[personList.size()]; - personList.toArray(personArray); - return personArray; - } + public abstract E[] toArray(); /** * * The class should implement Iterable and define a method named iterator * * which makes use of the personList field to generate a new a Iterator. */ @Override - public Iterator iterator() { + public Iterator iterator() { return personList.iterator(); //return null; } diff --git a/src/main/java/com/github/curriculeon/Students.java b/src/main/java/com/github/curriculeon/Students.java index ae60d18..94616ba 100644 --- a/src/main/java/com/github/curriculeon/Students.java +++ b/src/main/java/com/github/curriculeon/Students.java @@ -14,18 +14,41 @@ /** * Implemented By Monica Deshmukh 07/24/2020 */ +/** + * * Modify the Students class signature to ensure that it is a subclass of People of parameterized type Student. + */ + +import java.util.List; + +/** + * Implemented by Monica Deshmukh + * 07/28/2020 + */ public class Students extends People{ private static final Students INSTANCE = new Students(); private Students() { - this.add(new Person(1, "student1")); - this.add(new Person(2, "student2")); - this.add(new Person(3, "student3")); + this.add(new Student(1, "student1")); + this.add(new Student(2, "student2")); + this.add(new Student(3, "student3")); } public static Students getInstance() { return INSTANCE; } + + @Override + public Student[] toArray() { + //return INSTANCE.toArray(); + //should return the list personList in people class as an array + //The personList (which is of type E) should be copied into StudentList of type Student. + //Then convert list toArray and return the array. + Student[] studentArray = new Student[personList.size()];//should make a method in Person class to retrun personList.size() + List studentList = personList; + studentList.toArray(studentArray); + return studentArray; + } + } diff --git a/src/test/java/com/github/curriculeon/TestClassroom.java b/src/test/java/com/github/curriculeon/TestClassroom.java index 274e142..0051f99 100644 --- a/src/test/java/com/github/curriculeon/TestClassroom.java +++ b/src/test/java/com/github/curriculeon/TestClassroom.java @@ -33,7 +33,8 @@ public void testHostLecture(){ // search for value initialStudyHours = initialMap.get(student); studyHoursAfterLecture = mapAfterHostLecture.get(student); - Assert.assertEquals((initialStudyHours+ numberOfHoursStudied), studyHoursAfterLecture); + double expectedStudeyHours = initialStudyHours+ numberOfHoursStudied; + Assert.assertEquals((long)expectedStudeyHours, (long)studyHoursAfterLecture); } } } diff --git a/src/test/java/com/github/curriculeon/TestInstructors.java b/src/test/java/com/github/curriculeon/TestInstructors.java index bceb565..c913340 100644 --- a/src/test/java/com/github/curriculeon/TestInstructors.java +++ b/src/test/java/com/github/curriculeon/TestInstructors.java @@ -8,8 +8,8 @@ public class TestInstructors { public void testInstructors(){ //given Instructors instructors = Instructors.getInstance(); - Person[] instructorsExpected = {new Person(11, "instructor1"), - new Person(12, "instructor2")}; + Instructor[] instructorsExpected = {new Instructor(11, "instructor1"), + new Instructor(12, "instructor2")}; //when Person[] instructorsActual = instructors.toArray(); diff --git a/src/test/java/com/github/curriculeon/TestPeople.java b/src/test/java/com/github/curriculeon/TestPeople.java index d5fc777..e0ef2ec 100644 --- a/src/test/java/com/github/curriculeon/TestPeople.java +++ b/src/test/java/com/github/curriculeon/TestPeople.java @@ -16,9 +16,10 @@ public class TestPeople { @Test public void testAdd() { //given - People people = new People(); + //People people = new People(); + People people = Students.getInstance(); - //when + //when Person person1 = new Person(1, "person1"); Person person2 = new Person(2, "person2"); Person person3 = new Person(3, "person3"); @@ -35,7 +36,8 @@ public void testAdd() { @Test public void testRemove() { //given - People people = new People(); + //People people = new People(); + People people = Instructors.getInstance(); //when //add two persons to the list. @@ -59,21 +61,30 @@ public void testRemove() { @Test public void testFindById() { //given - People people = new People(); - + //People people = new People(); + People people = Students.getInstance(); //when - Person person1 = new Person(1, "person1"); + long id = 1; + /*Person person1 = new Person(1, "person1"); Person person2 = new Person(2, "person2"); Person person3 = new Person(3, "person3"); people.add(person1); people.add(person2); - people.add(person3); + people.add(person3);*/ + //then + Assert.assertTrue(people.contains(people.findById(id))); + + //when + id = 4; + //then + Assert.assertFalse(people.contains(people.findById(id))); + + //when + people = Instructors.getInstance(); + id = 11; //then - Assert.assertEquals(person1,people.findById(1)); - Assert.assertEquals(person2,people.findById(2)); - Assert.assertEquals(person3,people.findById(3)); + Assert.assertTrue(people.contains(people.findById(id))); - Assert.assertNotEquals(person1,people.findById(4)); } } diff --git a/src/test/java/com/github/curriculeon/TestStudent.java b/src/test/java/com/github/curriculeon/TestStudent.java index 6e85109..d2c6f75 100644 --- a/src/test/java/com/github/curriculeon/TestStudent.java +++ b/src/test/java/com/github/curriculeon/TestStudent.java @@ -20,7 +20,7 @@ public class TestStudent { @Test public void testImplementation() { //given a studentObject - long id = 1; + long id = 5; String name = "Monica"; Student studentObject = new Student(id,name); @@ -34,7 +34,7 @@ public void testImplementation() { @Test public void testInheritance() { //given a student object - long id = 1; + long id = 5; String name = "Monica"; Student studentObject = new Student(id,name); @@ -48,7 +48,7 @@ public void testInheritance() { @Test public void testLearn(){ //given a student object - long id = 1; + long id = 5; String name = "Monica"; Student student = new Student(id, name); Double initialStudyHours = student.getTotalStudyTime(); diff --git a/src/test/java/com/github/curriculeon/TestStudents.java b/src/test/java/com/github/curriculeon/TestStudents.java index ec60427..ee06598 100644 --- a/src/test/java/com/github/curriculeon/TestStudents.java +++ b/src/test/java/com/github/curriculeon/TestStudents.java @@ -15,15 +15,17 @@ public class TestStudents { public void testStudents() { //given Students students = Students.getInstance(); - Person[] studentCohort = { new Person(1, "student1"), - new Person(2, "student2"), - new Person(3, "student3")}; + /* Student[] studentCohort = { new Student(1, "student1"), + new Student(2, "student2"), + new Student(3, "student3")};*/ //when - Person[] studentArray = students.toArray(); + //Person[] studentArray = students.toArray(); + Student[] studentArray = students.toArray(); //then - for (int i = 0; i< 3; i++){ - Assert.assertEquals(studentArray[0].getId(), studentCohort[0].getId()); + for (int i = 1; i<= 3; i++){ + Assert.assertEquals(studentArray[i-1].getId(), i); + //Assert.assertEquals(studentArray[0].getId(), studentCohort[0].getId()); //Assert.assertTrue(students.contains(studentCohort[i])); : physical Person objects are different, their properties are same } } From 6acb3cb929c8343f461b9f082b8cee33c0475e80 Mon Sep 17 00:00:00 2001 From: monicadeshmukh Date: Tue, 28 Jul 2020 21:30:19 -0400 Subject: [PATCH 10/10] completed part 10.1.2.3.4. Parts completed without help afap to build muscle memory --- src/test/java/com/github/curriculeon/TestStudents.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/github/curriculeon/TestStudents.java b/src/test/java/com/github/curriculeon/TestStudents.java index ee06598..eac231f 100644 --- a/src/test/java/com/github/curriculeon/TestStudents.java +++ b/src/test/java/com/github/curriculeon/TestStudents.java @@ -25,6 +25,7 @@ public void testStudents() { //then for (int i = 1; i<= 3; i++){ Assert.assertEquals(studentArray[i-1].getId(), i); + // //Assert.assertEquals(studentArray[0].getId(), studentCohort[0].getId()); //Assert.assertTrue(students.contains(studentCohort[i])); : physical Person objects are different, their properties are same }