Professional Documents
Culture Documents
implements
Iterables<E>
Collaboration:
CompletethisprojectonlywithhelpfromUofASectionLeadersandthecoursematerials(books,
presentations,codedemos).Donotgiveyourcodetoanyoneorcopyanycode.Donotevenlookatanother's
screenwithcodefromthisprojectbyanotherstudent.
Preview:
Thisprojectasksyoutodevelopclass
DoublyLinkedList
thatimplementsanInterfacewithtwo
interfaces:FowardIteratorandReverseIterator
ThreeInterfacesthistime:
DoublyLinkedmustimplementinterfaceIterables<E>.Thisissimilartowhatwedid
withLinkedBag<E>implementsIterable<E>.
public
interface
Iterables<E>{
//Thisinterfaceiscomplete
public
ForwardIterator<E>forwardIterator()
public
ReverseIterator<E>reverseIterator()
}
Iterablesispluralbecauseyouwillhavetwomethods
forwardIterator()
and
reverseIterator()
eachofwhichrequiresaprivateinnerclassthatimplementstheseinterfaces:
public
interface
ForwardIterator<E>{
//Thisinterfaceiscomplete
public
boolean
hasNext()
public
Enext()
}
public
interface
ReverseIterator<E>{
//Thisinterfaceiscomplete
public
boolean
hasPrev()
public
Eprev()
}
Createfilesforeachofthethreeinterfacesandcopyandpasteinthesethreefromabove
TheCollectionclassthatmustuseaDoublyLinkedStructure
UsethesameNodeclassfromlab.Createafile
DoublyLinkedList.java
inyourEclipseProjectandcopyandpaste
thisclasswithallrequiredmethodstubs.Optional:GetaunittestintoEclipseorwriteyourowntestsifyouenjoy
that.Thelinkisbelowthisclasswithmethodstubsandinnerclassdeclarations.
/**
*Acollectionclassforstoringalistofanytypeelementsusingadoublylinkeddata
*structure.InstancesofthistypemusthavethesemethodsdefinedininterfaceIterables:
*
*1.ForwardIterator
<E>
forwardIterator()
*
*2.ReverseIterator
<E>
reverseIterator()
*/
public
class
DoublyLinkedList<E
extends
Comparable<E>>
implements
Iterables<E>{
private
class
Node{
Node
prev
E
data
Node
next
public
Node(Node
prevRef
,E
element
,Node
nextRef
){
prev
=
prevRef
this
.
data
=
element
next
=
nextRef
}
}
private
int
private
Node
header
,
trailer
//Constructanemptylist
public
DoublyLinkedList(){
//
TODO
:Completethismethod
}
//Returnthenumberofelementsinthislist
public
int
size(){
//
TODO
:Completethismethod
return99
}
//InsertelementsothislistisalwaysinitsnaturalorderingaccordingtoComparable
public
void
insertInorder(E
element
){
//
TODO
:Completethismethod
}
//Returnareferencetotheelementatthegivenindex
//Precondition:index>0&&index<size
public
Eget(
int
index
){
//
TODO
:Completethismethod
returnnull
}
//Removeelementiffoundandreturntrue.Ifnotfound,returnfalse
public
boolean
remove(E
element
){
//
TODO
:Completethismethod
returnfalse
}
//Returnareferencetoanobjectthathasaccesstothislist'selements
@Override
public
ForwardIteratorforwardIterator(){
//Thismethodiscomplete
return
new
ForwardIter<E>()
}
private
class
ForwardIter<Type>
implements
ForwardIterator{
//
TODO
Implementthisclass.Youwillseeacompiletimeerroratfirst
}
//Returnareferencetoanobjectthathasaccesstothislist'selements
@Override
public
ReverseIteratorreverseIterator(){
//Thismethodiscomplete
return
new
ReverseItr<E>()
}
private
class
ReverseItr<Type>
implements
ReverseIterator{
//
TODO
Implementthisclass.Youwillseeacompiletimeerroratfirst
}
}
//endclassDoublyLinkedList
UnitTest
UnitTest?Writeyourownorgetthisone
DoublyLinkedListTest.java
.Hereisastarttoshowtheuseof
ReverseIteratorandtoshowthatthereisno
assertEquals(int,Integer)
or
assertEquals(Integer,
int)
soacastisused.
@Test
public
void
testReverseIterator(){
DoublyLinkedList<Integer>
list
=
new
DoublyLinkedList<Integer>()
assertEquals
(0,
list
.size())
list
.insertInorder(12)
list
.insertInorder(4)
list
.insertInorder(3)
list
.insertInorder(2)
ReverseIterator<Integer>
fItr
=
list
.reverseIterator()
assertTrue
(
fItr
.hasPrev())
assertEquals
(12,(
int
)
fItr
.prev())
//castthereturnvalue,or
assertEquals
(newInteger(4),
fItr
.prev())
//WraptheintinanewInteger
assertEquals
(2,(
int
)
fItr
.prev())
assertTrue
(
fItr
.hasPrev())
assertEquals
(3,(
int
)
fItr
.prev())
assertFalse
(
fItr
.hasPrev())
}
GradingCriteria(100points,subjecttochange)
Style/Readability
10pts
+2Youincludedyournameasacommentinallfiles
+2Youhavea1or2sentencedescriptionofthepurposeofeachclass
+2Thesourcecodeisformattednicely(inEclipseuseSource>Format)
+2Usedmeaningfulidentifiers
+2Allmethodsarecommentedwithanaccuratedescriptioninallfiles
ProblemandCodeCoverage
90pts
+90WebCatcorrectnessandcodecoverage:Toget100%forthese90points,youwillneed100%problem
coverageonly,whichmeansRick'stestspassandyouexercisedallmethods.Youcangetascoreof0eventhough
allofyourtestspassedinyourworkspacebecause
WebCatreportsacompiletimeerror(lookforUnknownsymbol).
OneofRick'stestcasesplacedyourloopintoaninfiniteloop(Timeouterror)
OneofyourassertionsfailedonWebCat(eventhoughitpassedforyoulocally)
PleasenoteotherwaystolosepointswhengradedbyyourSectionLeader:
Upto100pts,azero(0),
willbegivenif
y
oudidntusethedoublylinkedstructureofNodeobjects
1ptforeveryWebCatsubmissionmorethan10,upto30points:Example:3dayslatewith40
submissionswouldbeamaximumof40/100evenwith100%codeandproblemcoverage
Youcanalsogetascoreof0eventhoughallofyourtestspassedinyourworkspacebecause
WebCatreportsacompiletimeerror(lookfor
Unknownsymbol
)
OneofRick'stestcasesplacedoneofyourloopsintoaninfiniteloopforaTimeoutError
OneofyourownassertionsfailsonWebCat