Question

In: Computer Science

how could I implement an intStack class that has only a push and pop method? in...

how could I implement an intStack class that has only a push and pop method?

in java of course.

Solutions

Expert Solution

// Java code for stack implementation

  

import java.io.*;

import java.util.*;

  

class Test

{   

    // Pushing element on the top of the stack

    static void stack_push(Stack<Integer> stack)

    {

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

        {

            stack.push(i);

        }

    }

      

    // Popping element from the top of the stack

    static void stack_pop(Stack<Integer> stack)

    {

        System.out.println("Pop :");

  

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

        {

            Integer y = (Integer) stack.pop();

            System.out.println(y);

        }

    }

  

  

    public static void main (String[] args)

    {

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

  

        stack_push(stack);

        stack_pop(stack);

        stack_push(stack);

    }

}


Related Solutions

All code should be in Python 3. Implement the Stack Class, using the push, pop, str,...
All code should be in Python 3. Implement the Stack Class, using the push, pop, str, init methods, and the insurance variable 'list'.
Write a function that returns the largest value in a stack (only use push and pop)
Write a function that returns the largest value in a stack (only use push and pop)
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the...
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the java and give the explanation
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only...
Modify Account Class(Java programming) used in the exercises such that ‘add’ and ‘deduct’ method could only run if the account status is active. You can do this adding a Boolean data member ‘active’ which describes account status (active or inactive). A private method isActive should also be added to check ‘active’ data member. This data member is set to be true in the constructor, i.e. the account is active the first time class Account is created. Add another private method...
why and how could we clearly establish POP and POD?
why and how could we clearly establish POP and POD?
how do I write a class PostFix that has one method covert that converts an infix...
how do I write a class PostFix that has one method covert that converts an infix expression (as a string input) to postfix in java?
How am I supposed to implement a c_str method that returns a c string representation of...
How am I supposed to implement a c_str method that returns a c string representation of a String object? I need to return a csting representation of a string object. I am confused on what cstrings are and where to go. method signature is as follows: char* c_str();
In C++, implement a class called setOper that provides several simple set operations. The class only...
In C++, implement a class called setOper that provides several simple set operations. The class only needs to deal with sets that are closed intervals specified by two real numbers; for example, the pair (2.5, 4.5) represent the interval [2.5, 4.5]. The following operations should be supported: - Check if the value x belongs to the given interval. - Check if the value x belongs to the intersection of two intervals. - Check if the value x belongs to the...
We have created an ArrayList of Person class. write a method called push that pushes all...
We have created an ArrayList of Person class. write a method called push that pushes all the people with the even length last name to the end of the ArrayList Content of the ArrayList before push [alex Bus, Mary Phillips, Nik Lambard, Rose Rodd, Esa khan, Jose Martinex, Nik Patte] content of the ArrayList after the push method [alex Bus, Nik Lambard, Nik Patte, Mary Phillips, Rose Rodd, Esa khan, Jose Martinex] import java.util.*; class Person { private String name;...
JAVA Implement a public class method named comparison on a public class Compare that accepts two...
JAVA Implement a public class method named comparison on a public class Compare that accepts two Object arguments. It should return 0 if both references are equal. 1 if both objects are equal. and -1 otherwise. (SUPER IMPORTANT) Either reference can be null, so you'll need to handle those cases carefully! Here is what I have so far: public class Compare { public static int comparison(Object a, Object b) {   if (a == null || b == null) {    return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT