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;...
Assume that Animal is a class that has method void info() that prints "I am an...
Assume that Animal is a class that has method void info() that prints "I am an animal". Classes Bird and Reptile both extend class Animal, and both override method info(). Class Bird implements method info to print "I am a bird", and class Reptile implements method info to print "I am a reptile ". Determine the outcome printed by the following lines of code. Animal animal = new Animal(); animal.info(); //OUTCOME: animal = new Reptile() animal.info(); //OUTCOME: animal = new...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT