Skip to content
51 changes: 51 additions & 0 deletions src/main/java/com/github/curriculeon/Classroom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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.
*/

/* 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();

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<Student,Double> getStudyMap(){
Iterator<Student> iterator = students.iterator();
Map<Student, Double> map = new HashMap<>();
Student s ;

while(iterator.hasNext()){
//if (iterator.next() instanceof Student){
//s = (Student)iterator.next();
s = iterator.next();
map.put(s, s.getTotalStudyTime());
//}
}
return map;
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/github/curriculeon/Instructor.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/github/curriculeon/Instructors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.curriculeon;

/**
* Use Part 7 as a reference.
* Create a Instructors singleton which represents the set of instructors.
* Create a TestInstructors class.
*/

import java.util.List;

/**
* Implemented by Monica Deshmukh
* 07/27/2020
*/
public class Instructors extends People{
private static final Instructors INSTANCE = new Instructors();

private Instructors() {
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<Instructor> instructorList = personList;
instructorList.toArray(instructorArray);
return instructorArray;
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/github/curriculeon/Learner.java
Original file line number Diff line number Diff line change
@@ -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();
}
121 changes: 121 additions & 0 deletions src/main/java/com/github/curriculeon/People.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.github.curriculeon;

import java.util.*;

/**
* Implemented By Monica Deshmukh
* 07/22/2020
*/

/**
* Create a People class.
*/

/**
* 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 <E extends Person> implements Iterable<E>{
/*
* The class should instantiate a List field of Person objects named personList.
*/
List<E> personList;

public People() {
personList = new ArrayList<E>();
}

/*
* The class should define a method named add which adds a Person to the personList.
*/
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 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
}


/* 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.
*/
public Boolean contains(E personType) {
if (personList.contains(personType))
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(E personType) {
if (this.contains(personType))
personList.remove(personType);
}

/**
* * 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 abstract E[] toArray();

/**
* * The class should implement Iterable<E> and define a method named iterator
* * which makes use of the personList field to generate a new a Iterator<E>.
*/
@Override
public Iterator<E> iterator() {
return personList.iterator();
//return null;
}

}
33 changes: 32 additions & 1 deletion src/main/java/com/github/curriculeon/Person.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/github/curriculeon/Student.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
54 changes: 54 additions & 0 deletions src/main/java/com/github/curriculeon/Students.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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
*/
/**
* * 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 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<Student> studentList = personList;
studentList.toArray(studentArray);
return studentArray;
}

}


Loading