Question

Write method removeThree(Stack   s) that receives a stack, and removes the three elements at the bottom of...

Write method removeThree(Stack   s) that receives a stack, and removes the three elements at the bottom of the stack. You should ensure that stack has at least three elements, otherwise display an error message. please do tin java

Homework Answers

Answer #1



import java.util.Stack;

public class StackPractice{
        public static void main(String[] args) {
                
                Stack<Integer> stack = new Stack<Integer>();
                stack.push(10);
                stack.push(20);
                stack.push(30);
                stack.push(40);
                System.out.print("Stack : ");
                System.out.println(stack);
                StackPractice stkp = new StackPractice();
                stkp.removeThree(stack);
                System.out.print("Popping 3 elements : ");
                System.out.println(stack);
        }       
        public void removeThree(Stack<Integer> stack) {
                Stack<Integer> temp = new Stack<Integer>();
                // checking size less than 3 or not
                if(stack.size() < 3) {
                        System.out.println("Cannot remove");
                        return;
                }
                // pushing stack into temp
                while(!stack.isEmpty()) {
                        temp.push(stack.pop());
                }
                // popping 3 elements
                temp.pop();
                temp.pop();
                temp.pop();
                // popping temp and pushing it to stack
                while(!temp.isEmpty()) {
                        stack.push(temp.pop());
                }       
        }
}


Code

Output

Know the answer?
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
Similar Questions
Write a method named changeStack that takes in a Stack of Integer objects named stackIn as...
Write a method named changeStack that takes in a Stack of Integer objects named stackIn as a parameter and returns another Stack of Integer objects. The returned Stack contains contents that is the result of switching the top half and the bottom half of the stackIn. But the ordering of integers in each half is NOT changed. In the case of odd-size stackIn, the middle element remains in the same position before and after the switch. This method is OUTSIDE...
urgent !! ASAP PLEASE DATA STRUCTURES To class SinglyLinkedList, add method afterThird(E e) which receives an...
urgent !! ASAP PLEASE DATA STRUCTURES To class SinglyLinkedList, add method afterThird(E e) which receives an element e and insert that in a new node after the third node in the list (assume the first node in the list is number 1). You should ensure that the linked list has at least three nodes, otherwise throw an exception. You should consider all possible cases.
Java 1. Implement a method that meets the following requirements: (a) Try to write this method...
Java 1. Implement a method that meets the following requirements: (a) Try to write this method with as few lines of code as you can (b) Sorts a group of three integers, x,y and z, into increasing order (they do not have to be in a sequence). (c) Assume the value in x is less than the value in z. You can also assume there are no duplicates among x, y and z (none of them contain the same value)...
Write 3 test cases to test the operation of a stack. The test cases should be...
Write 3 test cases to test the operation of a stack. The test cases should be black box test cases. The input and output functionality that you can test for a stack are creating an empty stack with a certain capacity, "push" an item on the stack, "pop" an item off of the stack, and examine the item on the top of the stack ("top"). At least 1 of your test cases needs to test a boundrary condition and at...
Please answer quickly! Thank you. Java: Write a non-static method to be added to a class...
Please answer quickly! Thank you. Java: Write a non-static method to be added to a class that implements a singly linked list of integers. The method is called make_partition() and takes an integer x as input. The method should modify this list by moving all the elements less than x to the front of the list, and all the elements greater than x to the back of the list. It is not the important for the elements to be kept...
Queues, Lists, and Stacks Assignment: Write client methods to do the following Assume that there exists...
Queues, Lists, and Stacks Assignment: Write client methods to do the following Assume that there exists in main a list   declared as : List<Integer> aList   =   new   List<Integer>(50); Write a client method called bubbleSort which given a list as a parameter uses the bubble sort algorithm to sort the contents of the list. YOU MAY ASSUME that there exists a method called swap (with the following signature) which swaps the contents of two elements: // swap public static void swap(List<Integer>...
Please dont forget the bolded part. Read carefully Write a method in JAVA/C++ to solve the...
Please dont forget the bolded part. Read carefully Write a method in JAVA/C++ to solve the Syracuse Sequence using recursion. A Syracuse Sequence is a sequence that begins with a number n0 and each element ni of the sequence is ni-1/2 if is ni-1 even and 3*ni-1+1 otherwise. You can write the method in a .docx file (no source code required). Also write the base, and induction clause. Draw the Activation Record (AR) diagram for the first six elements of...
Write a method which takes as input an integer, returns true if the integer is prime,...
Write a method which takes as input an integer, returns true if the integer is prime, and returns false otherwise. Do not import anything. If the input is negative, 0 or 1, false should be returned. If the input x is greater than 1, you can test if it is prime (inefficiently) by checking if it is divisible by any integer from 2 up to half of x. If it is not divisible by any of these numbers, then it...
The Power Set of a set of (distinct) objects S is the set of all subsets...
The Power Set of a set of (distinct) objects S is the set of all subsets of S, including S itself and an empty set. For example, let S be a set of words: S={Hello, World, Students}. Then possible subsets are: {Hello} {World} {Students} {Hello, World} {Hello, Students} {World, Students} {Hello, World, Students} From a course in Discrete Mathematics, you should know that if S contains n elements, then its power set consists of 2^n elements. Please write a recursive...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT