Question

java questions write a static method that determines it two stacks are composed of the same...


java questions
write a static method that determines it two stacks are composed of the same element

public static boolean allElementsShared (StackInterface <T>,s1,StackInterface <T>,s2)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.*;
import java.util.*;

class main{
   public static boolean allElementsShared (StackInterface<T> s1,StackInterface<T> s2){
       if (s1.size() == s2.size()){
           while (s1.isEmpty() == false){
               Object s_1 = s1.pop();
               Object s_2 = s2.pop();
               if (s_1.equals(s_2) == false)
                   return false;
           }
           return true;
       }
       return false;
   }
   public static void main(String[] args){

   }
}

Add a comment
Know the answer?
Add Answer to:
java questions write a static method that determines it two stacks are composed of the same...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Recursively sorting an unbounded stack (java) in ascending order? (This assignment is only limited to stacks...

    Recursively sorting an unbounded stack (java) in ascending order? (This assignment is only limited to stacks only, No other data structures are allowed) My professor gave us a hint on how to implement this, however she wants the comparable interface to be used. im not sure on how to do this. Hint: May want to use a public method that accepts the original stack and then creates the two additional stacks needed. This method then calls a private method that...

  • Consider two sorted stacks S1 and S2 (min on top) including comparable elements. Write a method...

    Consider two sorted stacks S1 and S2 (min on top) including comparable elements. Write a method to merge S1 and S2 and create one stack that is sorted (min on top). You are allowed to use stacks as extra space. Note: stack operations include push, pop, top and empty.

  • For merge sort the time complexity is Θ(nlogn), but what if we had two unsorted stacks...

    For merge sort the time complexity is Θ(nlogn), but what if we had two unsorted stacks and wanted to but merge them into one final sorted stack! what is the time complexity then? code / Java program to merge to unsorted stacks // into a third stack in sorted way. import java.io.*; import java.util.*;    public class GFG {            // This is the temporary stack     static Stack<Integer> res = new Stack<Integer>();     static Stack<Integer> tmpStack = new Stack<Integer>();            //...

  • Write a method to multiply two matrices. The header of the method is: public static double[][]...

    Write a method to multiply two matrices. The header of the method is: public static double[][] multiplyMatrix(double[][] a, double[][] b) To multiply matrix a by matrix b, the number of columns in a must be the same as the number of rows in b, and the two matrices must have elements of the same or compatible types. Let c be the result of the multiplication. Assume the column size of matrix a is n. Each element is For example, for...

  • How to write a method in Java with these set of instructions: Method name will be:...

    How to write a method in Java with these set of instructions: Method name will be: public static boolean containsNumber(String[] array, String str) { // instruction: returns true if str is an element that is equal to an element of array // return false if str does not appear in array. // using compare String equality (str1.equals(str2). // JUST assume that array is not null and not empty // and NONE of strings in array is null. Assume str is...

  • Write a public static method called ave in java to return the average of two doubles.

    Write a public static method called ave in java to return the average of two doubles.

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • Write a Java method . public static boolean upChecker(char [][] wordSearch, String word, int row, int...

    Write a Java method . public static boolean upChecker(char [][] wordSearch, String word, int row, int col) This method does the following: compare the first character from word to the character in puzzle[row][col] if they match subtract one from row (to check the previous character in the same column) and continue comparing characters, by advancing to the next character in word else if puzzle[row][col] is NOT part of puzzle array or the character in the cell does not match the...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private...

  • A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers...

    A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers as a a parameter and returns a new ArrayList that has every element in the second half of the original ArrayList swapped with every element in the first half of the original ArrayList. Note: • You can assume that the ArrayList passed to this method as a parameter always contains an even number of elements and will always contain at least two elements. For...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT