Write a program to create a stack, in which implement ArrayStack

Program Plan:

·                     Define stack interface Interface4Stack, in which define the stack operations
            method: the pop method, push method, peek method and remove method.
·                     Define class Imp_Stack, which implements Interface4Stack.
·                     The remove method is used to remove all items from the stack from top to
            bottom.
·                     The remove method will operate if the top will be greater than zero.
·                     It means that one push item is required otherwise an exception in main will
display on the monitor.
·                     Define class ArrayStack, in which the main method of program is defined.
·                     The input and output exception in main method will also define.
·                     Use the method DataInputStream to input from user keyboard.



Program Coding:

// include header files
import java.lang.*;
import java.io.*;
import java.util.*;
// define stack interface
interface Interface4Stack
{
     int n = 20;

Define the stack operation in the interface, in which the pop method, push method, peek method and remove method.

     public void pop();
     public void push();
     public void peek();
     public void remove(int n);
}
// define class for stack implementation
class Imp_Stack implements Interface4Stack
{
     // define stack array new variable
     int stack_arr[] = new int[n];
     int top = -1;

     // define push method
     public void push()
     {
          // define try method for illegal command
          try
          {
              // use this method for input from keyboard
              DataInputStream dis = new
                               DataInputStream(System.in);
              System.out.println("Enter Element");
              int element =
                       Integer.parseInt(dis.readLine());
              stack_arr[++top] = element;
          }
          // catch exception
          catch (Exception ex)
          {
              System.out.println("ex");
        }
     }
     // define pop method
     public void pop()
     {
          // define popping from stack top
          int popping = stack_arr[top];
          top--;
          System.out.println("popped element " + popping);
    }
     // define peek method
     public void peek()
     {
          // popping from stack top similar to pop
          int popping = stack_arr[top];
          System.out.println("popped element " + popping);
    }

The remove method is used to remove all items from the stack from top to bottom. The remove method is operated if the top is greater than zero. It means that one push item is required otherwise an exception in main will display on the monitor.

     // define remove method instead of display method
     @Override
     public void remove(int n)
     {
          // if top is greater than 0 then it pop from top
          // to bottom item
          while(n>0)
          {
              pop();
              n--;
          }        
     }
}
// define array stack class
class ArrayStack
{

The main method of program is defined, from where the execution of program begins start. The input and output exception in main method is also defined.

     // main method of the class will give exception for
     // any Input or output error
     public static void main(String arg[]) throws
                                                IOException
     {
          // input from keyboard
          DataInputStream dis = new
                               DataInputStream(System.in);
          Imp_Stack IS = new Imp_Stack();
          int menu = 0;
          do
          {
            System.out.println("1.push \n2.pop \n3.peek
                                     \n4.remove \n5.Exit");
            // enter a choice
            System.out.print("Enter your choice: ");

            // uses readLine() method of DataInputStream
            // that read the user input from keyboard
            menu = Integer.parseInt(dis.readLine());
            switch (menu)
            {
            case 1:
              IS.push();
              break;
            case 2:
              IS.pop();
              break;
            case 3:
              IS.peek();
              break;
            case 4:
              IS.remove(menu);
                break;
            case 5:
              System.exit(0);
            }
        }
        while (menu <= 5);
                System.out.println();
        }
}


Sample Output:

1. Push
2. Pop
3. Peek
4. Remove
5. Exit
Enter your choice: 1
Enter Element
21
1. Push
2. Pop
3. Peek
4. Remove
5. Exit
Enter your choice: 1
Enter Element
22
1. Push
2. Pop
3. Peek
4. Remove
5. Exit
Enter your choice: 1
Enter Element
23
1. Push
2. Pop
3. Peek
4. Remove
5. Exit
Enter your choice: 4
Popped element 23
Popped element 22
Popped element 21


c.                    

Program Plan:

·                     Define interface Interface4Stack, in which define the stack operations in
            the interface, the pop method, push method, peek method and remove
     method.
·                     Define class VectorStack that implements the push operation methods.
·                     Define main method of program, from where the execution of program begins
            start. The input and output exception in main method is also defined.
·                     Define DataInputStream method for user inputs from keyboard.
·                     The remove method is used to remove all items from the stack from top to
bottom. The remove method is operated if the top is greater than zero.
·                     Define exception in main to display on the monitor: if at least one push item
            does not occur.
·                     Define constructor VectorStack(int initialCapacity).
·                     The input/ output exception in main method is also defined.


Program Coding:

// include header
import java.util.Vector;
import java.lang.*;
import java.io.*;
import java.util.*;

// define stack-interface
interface Interface4Stack
{
     int n = 20;

Define the stack operation in the Interface4Stack, in which the pop method, push method, peek method and remove method.

     public void pop();
     public void push();
     public void peek();
     public void remove(int n);
 }

Define the class VectorStack that implements the push operation methods.

public class VectorStack<T> implements Interface4Stack
{
     // Last element is the top entry in stack
     private Vector<T> stack;
     private boolean initialized = false;
     private static final int DEFAULT_CAPACITY = 50;
     private static final int MAX_CAPACITY = 10000;
     int stack_arr[] = new int[n];
     int top = -1;
     // create constructor
     public VectorStack()
     {
          this(DEFAULT_CAPACITY);
     } // end default constructor
    
     // define constructor
     public VectorStack(int initialCapacity)
     {
       checkCapacity(initialCapacity);
       stack = new Vector<>(initialCapacity);
       initialized = true;
     } // end constructor

The main method of program is defined, from where the execution of program begins start. The input and output exception in main method is also defined.

     // main method
     public static void main(String arg[]) throws
                                            IOException
     {
          // define this method for user input from
          // keyboard
          DataInputStream dis = new
                               DataInputStream(System.in);
          VectorStack vs = new VectorStack();
          // define menu
          int menu = 0;
          do
          {
              // list the menu here
              System.out.println("1.push \n2.pop \n3.peek
                                     \n4.remove \n5.Exit");
             
              System.out.print("Enter your choice: ");

              // uses readLine() method of DataInputStream
               // that read the user input from keyboard
              menu = Integer.parseInt(dis.readLine());
             
              switch (menu)
              {
              case 1:
                   vs.push();
                   break;
              case 2:
                   vs.pop();
                   break;
              case 3:
                   vs.peek();
                   break;
              case 4:
                   vs.remove(menu);
                   break;
              case 5:
                   System.exit(0);
        }
    }
    while (menu <= 5);
            System.out.println();
    }
     private void checkCapacity(int initialCapacity)
     {
     }
    
     // define pop operation
     @Override
     public void pop()
     {
          int popping = stack_arr[top];
          top--;
          System.out.println("popped element " + popping);
     }
     // define push method
     @Override
     public void push()
     {
          // try for exception
          try
          {
              // define method to input from user keyboard
              DataInputStream dis = new
                                DataInputStream(System.in);
              System.out.println("Enter Element");
              // define readLine() method of
               DataInputStream to read input from keyboard
              int element =
                        Integer.parseInt(dis.readLine());
              stack_arr[++top] = element;
          }
          catch (Exception e)
          {
              System.out.println("e");
          }
     }
     // define peek method of stack
     public void peek()
     {
          int popper = stack_arr[top];
          System.out.println("popped element " + popper);
     }

The remove method is used to remove all items from the stack from top to bottom. The remove method is operated if the top is greater than zero. It means that one push item is required otherwise an exception in main will display on the monitor.

     // define display method
     public void remove(int n)
     {
          // if top is greater than 0 then it pop from top
              to bottom item
          while(n>0)
          {
              pop();
              n--;
          }        
     }
 }


Sample Output:

1. Push
2. Pop
3. Peek
4. Remove
5. Exit
Enter your choice: 1
Enter Element
101
1. Push
2. Pop
3. Peek
4. Remove
5. Exit
Enter your choice: 1
Enter Element
102
1. Push
2. Pop
3. Peek
4. Remove
5. Exit
Enter your choice: 1
Enter Element
103
1. Push
2. Pop
3. Peek
4. Remove
5. Exit
Enter your choice: 4
Popped element 103
Popped element 102
Popped element 101


d.          

Program Plan:
·                     Define interface Interface4Stack, in which define the stack operations in
the interface, the pop method, push method, peek method and remove
method.
·                     Define the class ClientStack that implements Interface4Stack.
·                     The main method of program is defined, from where the execution of program
            begins start.
·                     Define DataInputStream method for user inputs from keyboard.
·                     Define method readLine() of DataInputStream to read the data from
User keyboard.
·                     The remove method is used to remove all items from the stack from top to
            bottom. The remove method is operated if the top is greater than zero.
·                     Define exception in main to display on the monitor: if at least one push item
does not occur.

Program Coding:

// headers and packages
import java.lang.*;
import java.io.*;
import java.util.*;

// define stack-interface
interface Interface4Stack
{
     int n = 20;

Define the stack operation in the Interface4Stack, in which the pop method, push method, peek method and remove method.

     public void pop();
     public void push();
     public void peek();
     public void remove(int n);
}

// define client class of stack that implements interface
class ClientStack implements Interface4Stack
{
     int stack_array[] = new int[n];
     int top = -1;
     public void push()
     {
          try
          {
              DataInputStream dis = new
                                DataInputStream(System.in);
              System.out.println("Enter Element");
              int element =
                         Integer.parseInt(dis.readLine());
              stack_array[++top] = element;
          }
         
          catch (Exception e)
          {
              System.out.println("e");
        }
     }
     // define pop method
     public void pop()
     {
          int popping = stack_array[top];
          top--;
          System.out.println("popped element " + popping);
     }
     // define peek method
     public void peek()
     {
          int popping = stack_array[top];
          System.out.println("popped element " + popping);
     }

The remove method is used to remove all items from the stack from top to bottom. The remove method is operated if the top is greater than zero. It means that one push item is required otherwise an exception in main will display on the monitor.

     // remove method
     public void remove(int n)
     {
          while (n > 0)
          {
              pop();
              n--;
              } // end while
     } // end remove

The main method of program is defined, from where the execution of program begins start. The input and output exception in main method is also defined.

     // define main method of client class
     public static void main(String arg[]) throws
                                           IOException
     {
          // define DataInputStream method so user can
                                    input from keyboard
          DataInputStream dis = new
                                DataInputStream(System.in);
          // define client constructor
          ClientStack sv = new  ClientStack();
          // define menu and initialize it to zero
          int menu = 0;
        do
        {
          // define list of operations in stack
          System.out.println("1.push \n2.pop \n3.peek
                                  \n4.remove \n5.Exit");
            System.out.println();
            System.out.print("Enter your choice: ");
            // uses readLine() method of DataInputStream to
                                   read input from keyboard
            menu = Integer.parseInt(dis.readLine());
            switch (menu)
            {
            case 1:
              sv.push();
              break;
            case 2:
              sv.pop();
              break;
            case 3:
              sv.peek();
              break;
            case 4:
              sv.remove(menu);
                break;
            case 5:
              System.exit(0);
            }
        }
        while (menu <= 5);
                System.out.println();
        }
}


Sample Output:

1. Push
2. Pop
3. Peek
4. Remove
5. Exit

Enter your choice: 1
Enter Element
121
1. Push
2. Pop
3. Peek
4. Remove
5. Exit

Enter your choice: 1
Enter Element
122
1. Push
2. Pop
3. Peek
4. Remove
5. Exit

Enter your choice: 1
Enter Element
123
1. Push
2. Pop
3. Peek
4. Remove
5. Exit

Enter your choice: 4
Popped element 123
Popped element 122

Popped element 121

Post a Comment

Previous Post Next Post