Monday, 1 September 2014

Packages In Java

How to save a package ? 
1. Create folder StackStack.
2. Create subfloder stack.
3. Save Sstack.java in subfolder stack.
4. Then, save StackDemo.java in folder StackStack.
//save as Sstack.java
package stack;

interface Instack
{
    void push(int element);
    void pop();
    void display();
}

public class Sstack implements Instack
{
     int n,arr[],top;
     public Sstack(int size)
     {
          n = size;
          arr = new int[n];
          top = -1;
     }

     public void push(int element)
     {
          int temp = 0;
          if(top >= (n-1))
          {
                System.out.println("\nStack is overflow");
          }
          else
          {
                top++;
               arr[top] = element;
               System.out.println("\nElement inserted: "+element);
          }
    }

    public void pop()
    {
        int temp;
        if(top == -1)
        {
            System.out.println("\nStack is underflow");
        }
        else
        {
            temp = arr[top];
            top--;
            System.out.println("\nElement Deleted: "+temp);
        }
    }

    public void display()
    {
        int i;
        if(top == -1)
        {
            System.out.println("\nStack is underflow");
        }

        for(i=0;i<=top;i++)
        {
            System.out.println("\n"+arr[i]);
        }
    }
}
//save as  StackDemo.java
import java.io.*;
import stack.*;             //importing package stack

class StackDemo
{
    public static void main(String s[]) throws Exception
    {
        int n,c,e,i;
        BufferedReader br  = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("\nEnter number of elements in the array: ");
        n = Integer.parseInt(br.readLine());

        Sstack ss = new Sstack(n);
        for(i=0;i<100 data-blogger-escaped-1="" data-blogger-escaped-2="" data-blogger-escaped-3="" data-blogger-escaped-:="" data-blogger-escaped-break="" data-blogger-escaped-c="" data-blogger-escaped-case="" data-blogger-escaped-choice:="" data-blogger-escaped-default="" data-blogger-escaped-e="" data-blogger-escaped-element="" data-blogger-escaped-i="" data-blogger-escaped-input="" data-blogger-escaped-insert:="" data-blogger-escaped-nenter="" data-blogger-escaped-nwrong="" data-blogger-escaped-pre="" data-blogger-escaped-ss.display="" data-blogger-escaped-ss.pop="" data-blogger-escaped-ss.push="" data-blogger-escaped-switch="" data-blogger-escaped-system.out.print="" data-blogger-escaped-system.out.println="" data-blogger-escaped-to="" data-blogger-escaped-your="">

1 comment: