Lab: Stack Implementation with Linked List
Objective
In this lab you will practice working with linked structures by
implementing and testing a kernel component, Stack2 implemented with
a singly-linked list.
Setup
To get started, import the project for this lab, StackWithLinkedList,
from the StackWithLinkedList.zip file available at this
link. If you don't remember how to do this,
see the Setup
instructions
in an earlier lab.
Method
- Take a look at the
srcandtestfolders in your new project. Open theStack2.javafile in thesrcfolder and explore it. In particular, observe the private method conventionHolds that checks whether the convention holds at the point in which it is invoked. As you can see, calls to this method have been inserted at the end of each public method in the class (or just before each return statement in functions). These calls are meant to help both in exposing internal bugs while testing and also in identifying some of the bugs while debugging. - Complete the body of the private method
createNewRepand the body of the kernel methods (push,pop, andlength) inStack2in thesrcfolder. You should be able to just paste the code you wrote for the homework. - Paste the test cases you designed for the homework at the end of the
StackTestclass in thetestfolder. - Run
Stack2Testin thetestfolder to test your implementation ofStack2. Fix any bugs that you discover and/or add extra test cases, if you realize that you are missing some important cases.
Additional Activities
-
Copy and paste the following piece of code at the end of
Stack2(after theStack2Iteratornested class and before the classStack2closing '}')./* * Other methods (overridden for performance reasons) --------------------- */ @Override public final T top() { assert this.length() > 0 : "Violation of: this /= <>"; // TODO - fill in body assert this.conventionHolds(); // Fix this line to return the result after checking the convention. return null; } @Override public final T replaceTop(T x) { assert this.length() > 0 : "Violation of: this /= <>"; // TODO - fill in body assert this.conventionHolds(); // Fix this line to return the result after checking the convention. return null; }Provide a non-layered implementation of the secondary methods
topandreplaceTopdefined inStack. -
Add appropriate test cases to your
StackTestJUnit fixture to test the new methodstopandreplaceTop. Run the new fixture and fix any problems you encounter. -
Take a look at the
Standardmethods (newInstance,clear, andtransferFrom) inStack2. We will discuss the details of these implementations later in the semester, but for now, design test cases to test these methods and add the test cases to your test fixture,StackTest.