Unformatted text preview:

Homework Sequence Implementation on Stack This homework is necessary preparation for the lab Make sure you type your answers in files you bring to the lab so that you will not have to waste time entering your code during the lab 1 Implement the following method that given two stacks and an integer moves entries between the two stacks so that the length of the first stack is equal to the given integer Note that as the ensures clause states rev leftStack rightStack must not be changed by the method 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Shifts entries between code leftStack and code rightStack keeping reverse of the former concatenated with the latter fixed and resulting in length of the former equal to code newLeftLength param T type of code Stack entries param leftStack the left code Stack param rightStack the right code Stack param newLeftLength desired new length of code leftStack updates leftStack rightStack requires pre 0 newLeftLength and newLeftLength leftStack rightStack pre ensures pre rev leftStack rightStack rev leftStack rightStack and leftStack newLeftLength pre private static T void setLengthOfLeftStack Stack T leftStack Stack T rightStack int newLeftLength if leftStack length newLeftLength while leftStack length newLeftLength T z rightStack pop leftStack push z if leftStack length newLeftLength while leftStack length newLeftLength T s leftStack pop rightStack push s Note that setLengthOfLeftStack is a static generic method it is parameterized by the type T of the entries in the stacks You can use the type T wherever you need to declare a variable that refers to an object of type T 2 Develop a complete test plan for the Sequence constructor and kernel methods add remove and length and enter them in SequenceTest For the homework turn in printouts of your implementation of setLengthOfLeftStack and of the SequenceTest java file components sequence Sequence JUnit test fixture for code Sequence String s constructor and kernel methods author Put your name here public abstract class SequenceTest Invokes the appropriate code Sequence constructor for the implementation under test and returns the result return the new sequence ensures constructorTest protected abstract Sequence String constructorTest Invokes the appropriate code Sequence constructor for the reference implementation and returns the result return the new sequence ensures constructorRef protected abstract Sequence String constructorRef Creates and returns a code Sequence String of the implementation under test type with the given entries param args the entries for the sequence return the constructed sequence ensures createFromArgsTest entries in args private Sequence String createFromArgsTest String args Sequence String sequence this constructorTest for String s args sequence add sequence length s return sequence Creates and returns a code Sequence String of the reference implementation type with the given entries param args the entries for the sequence return the constructed sequence ensures createFromArgsRef entries in args private Sequence String createFromArgsRef String args Sequence String sequence this constructorRef for String s args sequence add sequence length s return sequence TODO add test cases for constructor add remove and length Test public void testAdd Sequence String sequence constructorTest sequence add 0 First assertEquals 1 sequence length assertEquals First sequence remove 0 Test public void testRemove Sequence String sequence createFromArgsTest f s t String removed sequence remove 1 assertEquals Second removed assertEquals 2 sequence length Test public void testLength Sequence String sequence createFromArgsTest a c g assertEquals 3 sequence length


View Full Document

OSU CSE 1223 - Homework 7

Documents in this Course
Load more
Download Homework 7
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Homework 7 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Homework 7 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?