Question

In: Computer Science

Two stacks are the same if they have the same size and their elements at the...

Two stacks are the same if they have the same size and their elements at the corresponding positions are the same. Add the method equalStack to class ArrayStack that takes as a parameter ArrayStack object otherStack, and returns true if this stack is the same as otherStack.

Use the following signature for your method: public boolean equalStack(ArrayStack otherStack) { ... }

Solutions

Expert Solution

Please find the below implementation for equalStack method, assuming tthe ArrayStack is extending Stack Class

Also the below code has all the validation checks for null and not used any method variables

    public boolean equalStack(ArrayStack otherStack) {
                if (this == otherStack)
                        return true;
                if (otherStack == null)
                        return false;
                if (getClass() != otherStack.getClass())
                        return false;
                if (this.size() != otherStack.size())
                        return false;
                while (this.empty() == false) {
                        if (this.peek() == otherStack.peek()) {
                                this.pop();
                                otherStack.pop();
                        } else
                                return false;
                }
                return true;
        }

Related Solutions

You are comparing the common-size financial statements for two firms in the same industry that have...
You are comparing the common-size financial statements for two firms in the same industry that have very similar operations. You note that their sales revenues are similar in dollar value but yet the common-size EBIT for one firm is 30 percent compared to only 26 percent for the other firm. What are some possible explanations for this difference given the strong similarities of the two firms?
There are at most two squares (not necessarily with the same size) such that the sum...
There are at most two squares (not necessarily with the same size) such that the sum of the area(s) is 8 in^2 . Maximize and minimize the sum of the perimeter(s)?
If the size and shape of two objects A & B are the same, but object...
If the size and shape of two objects A & B are the same, but object B has a smaller mass: (a) for which object would the beta factor be larger? Justify your answer. (b) Which object would have a larger terminal velocity? Justify your answer. (c) Using a-b: if you dropped styrofoam ball and a steel ball of the same size/shape off a tall building, which would land land first, discounting air resistance? How about including air resistance? beta=...
Two major automobile manufacturers have produced compact cars with the same size engines. We are interested...
Two major automobile manufacturers have produced compact cars with the same size engines. We are interested in determining whether or not there is a significant difference in the MPG (miles per gallon) of the two brands of automobiles. A random sample of eight cars from each manufacturer is selected, and eight drivers are selected to drive each automobile for a specified distance. The following data show the results of the test and if the variance of MPG is the same,...
Assume the two objects in question 3 are of the same physical size and are connected...
Assume the two objects in question 3 are of the same physical size and are connected together with a conductor. Will charge move from object 1 to object 2 or from object 2 to object 1? Why?
Two large containers A and B of the same size are filled with different fluids. The...
Two large containers A and B of the same size are filled with different fluids. The fluids in containers A and B are maintained at 0° C and 100° C, respectively. A small metal bar, whose initial temperature is 100° C, is lowered into container A. After 1 minute the temperature of the bar is 90° C. After 2 minutes the bar is removed and instantly transferred to the other container. After 1 minute in container B, the temperature of...
Two large containers A and B of the same size are filled with different fluids. The...
Two large containers A and B of the same size are filled with different fluids. The fluids in containers A and B are maintained at 0° C and 100° C, respectively. A small metal bar, whose initial temperature is 100° C, is lowered into container A. After 1 minute the temperature of the bar is 90° C. After 2 minutes (since being lowered into container A) the bar is removed and instantly transferred into the other container. After 1 minute...
Two large containers A and B of the same size are filled with different fluids. The...
Two large containers A and B of the same size are filled with different fluids. The fluids in containers A and B are maintained at 0° C and 100° C, respectively. A small metal bar, whose initial temperature is 100° C, is lowered into container A. After 1 minute the temperature of the bar is 90° C. After 2 minutes (since being lowered into container A) the bar is removed and instantly transferred into the other container. After 1 minute...
Suppose there are two countries that have the same nominal GDP. Do they have the same...
Suppose there are two countries that have the same nominal GDP. Do they have the same standard of living? Why or why not? Scenario 2: Suppose there are two countries and nominal GDP in one country is twice as large as the other country. Do they have the same standard of living? Why or why not?
What happens to the size of atoms in p-block elements when we move from left to right in the same period? a) Size does not change b) Size increases then decreases c) Size increases d) Size decreases
What happens to the size of atoms in p-block elements when we move from left to right in the same period?a) Size does not changeb) Size increases then decreasesc) Size increasesd) Size decreases
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT