Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

parityMatches

Language/Type: Java Collections Stacks and Queues
Author: Stuart Reges (on 2020/11/05)

Write a method called parityMatches that takes two stacks of integers as parameters and that returns a count of the number of elements in corresponding positions that have the same parity. Two numbers are considered to have the same parity if they are either both even or both odd. Suppose, for example, that two stacks store these values:

        s1: bottom [3, 4, 5] top        s2: bottom [37, 14, 24] top

The method compares values in corresponding positions in the two stacks (3 and 37, 4 and 14, 5 and 24). Of these pairs, two have the same parity. The third pair (5 and 24) do not have the same parity. Therefore the method call parityMatches(s1, s2) would return 2.

Your method should throw an IllegalArgumentException if they have different numbers of elements. You may also assume that all of the numbers stored in the list are greater than or equal to 0. Your method is to examine the two stacks but must return them to their original state before terminating.

You are to use one queue as auxiliary storage to solve this problem. You may not use any other auxiliary data structures to solve this problem, although you can have as many simple variables as you like. You also may not solve the problem recursively. Your solution must run in O(n) time where n is the size of the stacks. Use the Stack and Queue structures described in the cheat sheet and obey the restrictions described there (recall that you can't use the peek method or a foreach loop or iterator).

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.