- Purpose - to demonstrate the use of Java interfaces
- Objective - to implement a
ZipCodeWilmington
singleton which mediates a compositeStudents
andInstructors
reference.
- Create a
Person
class.- The class should declare a
final
field namedid
of typelong
. Person
constructor should have a parameter of typelong
which sets theid
field to the respective value.- The class should define a
getId()
method which returns thePerson
object'sid
field. - The class should define a
getName()
method which returns thePerson
object'sname
field. - The class should define a
setName()
method which sets thePerson
object'sname
field.
- The class should declare a
- Create a
TestPerson
class.- Create a
testConstructor
method which ensures that aPerson
object'sid
field is being set upon construction. - Create a
testSetName
method which ensures that aPerson
object'sname
variable is being set by invoking the.setName
method. - Create a
testConstructor
method which ensures that aPerson
object'sname
variable is being set by invoking thePerson
constructor.
- Create a
- Create a
Learner
interface.Learner
should declare one method signature:- Method name:
learn
- Method parameters:
double numberOfHours
- Method return-type:
void
- Method name:
- Create a
Student
class such that:Student
is a subclass ofPerson
Student
implements theLearner
interfaceStudent
should have an instance variabletotalStudyTime
of typedouble
Student
should have a concrete implementation of thelearn
method which increments thetotalStudyTime
variable by the specifiednumberOfHours
argument.Student
should have agetTotalStudyTime()
method which returns thetotalStudyTime
instance variable.
- Create a
TestStudent
class.- Create a
testImplementation
method that asserts that aStudent
is aninstanceof
aLearner
. - Create a
testInheritance
method that asserts that aStudent
is aninstanceof
aPerson
. - Create a
testLearn
method that ensures aStudent
'stotalStudyTime
instance variable is incremented by the specifiednumberOfHours
by invoking the.learn
method.
- Create a
- Create a
Teacher
interface.-
Teacher
should declare ateach
method signature:- Method name:
teach
- Method parameters:
Student student
double numberOfHours
- Method return-type:
void
- Method name:
-
Teacher
should declare alecture
method signature:- Method name:
lecture
- Method parameters:
Student[] student
double numberOfHours
- Method return-type:
void
- Method name:
-
- Create an
Instructor
class such that:Instructor
is a subclass ofPerson
Instructor
implements theTeacher
interfaceInstructor
should have a concrete implementation of theteach
method which invokes thelearn
method on the specifiedStudent
object.Instructor
should have a concrete implementation of thelecture
method which invokes thelearn
method on each of the elements in the specified array ofStudent
objects.numberOfHours
should be evenly split amongst the students.double numberOfHoursPerStudent = numberOfHours / students.length;
- Create a
TestInstructor
class.- Create a
testImplementation
method that asserts that anInstructor
is aninstanceof
aTeacher
. - Create a
testInheritance
method that asserts that aInstructor
is aninstanceof
aPerson
. - Create a
testTeach
method that ensures when anInstructor
invokes the.teach
method, a respective student'stotalStudyTime
instance variable is incremented. - Create a
testLecture
method that ensures when anInstructor
invokes the.teach
method, a respective student'stotalStudyTime
instance variable is incremented by the specifiednumberOfHours
.
- Create a
- Create a
People
class.- The class should instantiate an
ArrayList
field ofPerson
objects namedpersonList
. - The class should define a method named
add
which adds aPerson
to thepersonList
. - The class should define a method named
findById
which makes use of along id
parameter to return aPerson
object with the respectiveid
field. - The class should define a method named
remove
which makes use of aPerson person
parameter to remove a respectivePerson
object. - The class should define a method named
remove
which makes use of along id
parameter to remove aPerson
object with the respectiveid
field. - The class should define a method named
getCount
which returns the size ofpersonList
. - The class should define a method named
getArray
which returns an array representation of thepersonList
field. - The class should define a named
removeAll
which clears ourpersonList
field.
- The class should instantiate an
- Create a
TestPeople
class.- Create a
testAdd
method which ensures that ourpersonList
in ourPeople
class populated with respectiveStudent
objects following invokation of theaddStudent
method. - Create a
testRemove
method which ensures that thepersonList
in aPeople
object is depopulated with a respectivePerson
object following the invokation of theremove
method. - Create a
testFindById
method which ensures that a respectivePerson
object with a respectiveid
field is returned upon invokation of thefindById
method on a respectivePeople
object.
- Create a
- 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 namedINSTANCE
of typeStudents
. - The class should define a private nullary constructor which populates the
INSTANCE
field with respectiveStudent
representations of your colleagues.- Each student should have a relatively unique
id
field.
- Each student should have a relatively unique
- The class should define a
getInstance
method which returns theINSTANCE
field.
- The class should be an unextendable subclass of the
- Create a
TestStudents
class.- Create a
test
method which ensures that each of the students in your current cohort are in yourStudents
singleton.
- Create a
- Use
Part 7
as a reference. - Create a
Instructors
singleton which represents the set of instructors at ZipCodeWilmington. - Create a
TestInstructors
class.
- Use
Part 7
as a reference. - Create a
ZipCodeWilmington
singleton.- The class should declare a field that references the instance of
Students
calledstudents
. - The class should declare a field that references the instance of
Instructors
calledinstructors
. - The class should define a method
hostLecture
which makes use of aTeacher teacher, double numberOfHours
parameter to host alecture
to the compositepeople
field in thestudents
reference. - The class should define a method
hostLecture
which makes use of along id, double numberOfHours
parameter to identify a respectiveInstructor
to host alecture
to the compositepeople
field in thecohort
reference.
- The class should declare a field that references the instance of
- Create a
TestZipCodeWilmington
class.- Create a
testHostLecture
method which ensures that each of theStudent
'stotalStudyTime
instance variable is incremented by the specifiednumberOfHours
upon invoking thehostLecture
method.
- Create a
- You may have noticed that the
findById
, andhostLecture
methods require an intermediate casting trick. - To remedy this issue, we can generify the
People
class.
- Parameterize the
People
signature to enforce that it is a container for objects of typeE
such thatE
is a subclass ofPerson
. - Modify
people
field to enforce that is a container of objects of typeE
. - Modify the
add
method to ensure that it handles object of typeE
. - Modify the
getArray
method to ensure that it returns an object of typeE[]
. - Modify the
findById
method to ensure that it returns an object of typeE
.
- Modify the
Students
class signature to ensure that it is a subclass ofPeople
of parameterized typeStudent
. - Modify the
Instructors
class signature to ensure that it is a subclass ofPeople
of parameterized typeInstructor
.
- Refactor the
hostLecture
method in theZipCodeWilmington
class by removing any intermediate casting trick(s).
- Ensure that the
TestStudents
,TestInstructors
,TestPeople
,TestZipCodeWilmington
classes were no affected by the refactor.
- You may have notice that
findById
makes it difficult to intuitively identify whichPerson
object is being returned.
Additionally, it's challenging to ensure everyPerson
instance has a unique ID amongst its respectivePeople
subclass.
To remedy this issue, we redesign and refactor.
- Create an enum named
Educator
.- The enum should implement
Teacher
. - The enum should have an enumeration for each of the instructors represented in the
Instructors
class. - The enum should have an empty nullary constructor.
- The enum should implement
- Use
Part 5
as a reference.
- Annotate the constructor with
@Deprecated
. - This constructor should be commented with
//TODO - Remove dependencies
- Create a constructor in the
Instructor
class which usesEducator educator
parameter to set a finaleducator
field. - The class should differ calls to the
teach
andlecture
method to the compositeeducator
field. - Remove any calls being made to the deprecated construcor.
- Remove the deprecated constructor from the class.
- Refactor
TestInstructor
to support the newly createdInstructor
constructor. - Ensure the tests were not affected by the refactoring.