Question

In: Computer Science

Write a method called "twoStacksAreEqual" that takes as parameters two stacks of integers and returns true...

Write a method called "twoStacksAreEqual" that takes as parameters two stacks of integers and returns true if the two stacks are equal and that returns false otherwise. To be considered equal, the two stacks would have to store the same sequence of integer values in the same order. Your method is to examine the two stacks but must return them to their original state before terminating. You may use one stack as auxiliary storage.

import java.util.Enumeration;

import java.util.LinkedList;

import java.util.Queue;

import java.util.Stack;

//Name,Class,Date..etc.. Header

public class Assignment6 {

public static void main(String[] args) {

//testSeeingThreeMethod();

//testTwoStacksAreEqualMethod();

//testIsMirrored();

}

public static void seeingThree(Stack<Integer> s) {

/*********Write Code Here************/

}

public static boolean twoStacksAreEqual(Stack<Integer> s1, Stack<Integer> s2)

{

/*********Write Code Here************/

}

public static boolean isMirrored(Queue<Integer> q) {

/*********Write Code Here************/

}

private static void testIsMirrored() {

Queue<Integer> myQueueP = new LinkedList<Integer>();;

for (int i = 0; i < 5; i++)

{

System.out.print(i);

myQueueP.add(i);

}

for (int i = 3; i >= 0 ; i--)

{

System.out.print(i);

myQueueP.add(i);

}

System.out.println();

System.out.println(isMirrored(myQueueP) + " isMirrord");

}

private static void testTwoStacksAreEqualMethod() {

Stack<Integer> myStack1 = new Stack<Integer>();

Stack<Integer> myStack2 = new Stack<Integer>();

Stack<Integer> myStack3 = new Stack<Integer>();

Stack<Integer> myStack4 = new Stack<Integer>();

for (int i = 0; i < 5; i++)

{

myStack1.push(i);

myStack2.push(i);

myStack4.push(i);

}

for (int i = 0; i < 6; i++)

{

myStack3.push(i);

}

System.out.println(twoStacksAreEqual(myStack1,myStack2) + " Same Stack

");

System.out.println(twoStacksAreEqual(myStack3, myStack4) + " Not Same

Stack");

}

private static void testSeeingThreeMethod() {

Stack<Integer> myStack = new Stack<Integer>();

for (int i = 0; i < 5; i++)

{

myStack.push(i);

}

System.out.println();

print(myStack);

seeingThree(myStack);

print(myStack);

}

private static void print(Stack<Integer> s) {

Enumeration<Integer> e = s.elements();

while ( e.hasMoreElements() )

System.out.print( e.nextElement() + " " );

System.out.println();

}

} //end of Assignment6

Solutions

Expert Solution

import java.util.Enumeration;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Assignment6 {
   public static void main(String[] args) {
       testTwoStacksAreEqualMethod();
   }

   public static boolean twoStacksAreEqual(Stack<Integer> s1, Stack<Integer> s2)
   {
       if (s1.empty() && s2.empty())
       {
           return true;  
       }

       else if (s1.empty() && s2.empty())
       {
           return false;  
       }

       else
       {
           while(!(s1.empty() || s2.empty()))
           {
               if (s1.pop() != s2.pop())
               {
                   return false;
               }
           }
           if (s1.empty() && s2.empty())
           {
               return true;  
           }
       }
       return false;
   }

   private static void testTwoStacksAreEqualMethod()
   {
       Stack<Integer> myStack1 = new Stack<Integer>();
       Stack<Integer> myStack2 = new Stack<Integer>();
       Stack<Integer> myStack3 = new Stack<Integer>();
       Stack<Integer> myStack4 = new Stack<Integer>();

       for (int i = 0; i < 5; i++)
       {
           myStack1.push(i);
           myStack2.push(i);
           myStack4.push(i);
       }

       for (int i = 0; i < 6; i++)
       {
           myStack3.push(i);
       }

       System.out.println(twoStacksAreEqual(myStack1,myStack2) + " Same Stack");
       System.out.println(twoStacksAreEqual(myStack3, myStack4) + " Not Same Stack");
   }
} //end of Assignment6


Related Solutions

Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at...
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
C++ write a function called divideBy that takes two integers as its input and returns the...
C++ write a function called divideBy that takes two integers as its input and returns the remainder. If the divisor is 0, the function should return -1, else it should return the remainder to the calling function.
.. Write a method called findNums that takes a two-dimension array of integers as a parameter...
.. Write a method called findNums that takes a two-dimension array of integers as a parameter and returns the number of times a two-digit number appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 The value returned would be 5 (there are 5 two-digit numbers in the array) public class Question2 {    public static void main(String args[]){      int arr[][] = {{10, 45,...
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an...
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class HomeworkA { public static...
in java language Write a method called findNums that takes a two-dimension array of integers and...
in java language Write a method called findNums that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class Question2 {   ...
write the “largerComponents” method that takes in two integer arrays and returns true or false if...
write the “largerComponents” method that takes in two integer arrays and returns true or false if the first array’s components are strictly greater than the second array’s components. The arrays must be the same size or else return false. Clarification Note: Components meaning each value at a specific index. For instance, if we had the arrays {5,2,7} and {1,3,1} then this method would return false as the value “2” is not greater than “3”. here is a provided code //Do...
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
In.java In this program write a method called upDown that takes three integers as arguments and...
In.java In this program write a method called upDown that takes three integers as arguments and returns one of these 3 strings: "increasing" if they are in strictly increasing order (note that 3,3,4 - are not strictly increasing), "decreasing" if they are in strictly decreasing order. "none" otherwise. I recommend you use a complex condition to check for this (that is, have a single if statement with one big question). In the main method do the following: read three integers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT