Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the following method that, given two stacks and an integer, moves entr

ID: 3887980 • Letter: I

Question

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) {...}

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.

Develop a complete test plan for the Sequence constructor and kernel methods (add, remove, and length) and enter them in SequenceTest.

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) {...}

Explanation / Answer

Below is your method: -

private static <T> void setLengthOfLeftStack(Stack<T> leftStack, Stack<T> rightStack, int newLeftLength) {

int difference = leftStack.length() - newLeftLength;

if (difference > 0) {

for (int i = 0; i <= difference; i++) {

leftStack.flip();

T remove = leftStack.pop();

leftStack.flip();

rightStack.push(remove);

}

} else {

for (int i = 0; i <= 0 - difference; i++) {

leftStack.flip();

T remove = rightStack.pop();

leftStack.push(remove);

leftStack.flip();

}

}

}

Regarding test plan of Sequence Constructor, If you can give some more details in comments , I'll provide that one too.