Monday, 3 November 2014

.Define an interface “StackOperations” which declares methods for a static stack. Define a class “MyStack” which contains an array and top as data members and implements the above interface. Initialize the stack using a constructor. Write a menu driven program to perform operations on a stack object.




Name : Shreyal Mandot

Title : Assignment 3 setA 3.Define an interface “StackOperations” which declares methods for a static stack.
Define a class “MyStack” which contains an array and top as data members and
implements the above interface. Initialize the stack using a constructor. Write a menu
driven program to perform operations on a stack object.



import java.io.* ;

interface StackOperations
{

public void initStack() ;
public boolean isEmpty() ;
public boolean isFull() ;
public void pushElement(int num) ;
public int popElement() ;

}

class ass3a3
{

int[] stack ;
int top , last ;

ass3a3()
{

stack = new int[10] ;
last = 10 ;

}

ass3a3(int num)
{

stack = new int[num] ;
last = num ;

}


public void initStack()
{

this.top = -1 ;

}

public boolean isEmpty()
{

if(this.top==-1)
return true ;

else
return false ;

}

public boolean isFull()
{

if(this.top==last-1)
return true ;

else
return false ;

}

public void pushElement(int num)
{

if(!this.isFull())
this.stack[++(this.top)]=num ;

else
System.out.println("\nOverFlow of stack...") ;

}

public int popElement()
{

if(!this.isEmpty())
return this.stack[this.top--] ;

else
{

System.out.println("\nUnderFlow of stack...") ;
return (-1) ;

}

}

public static void main(String[] arg) throws Exception
{

int number , choice , pop ;
BufferedReader b = new BufferedReader(new InputStreamReader(System.in)) ;

System.out.println("\nEnter the Number of elements in stack :") ;
number = Integer.parseInt(b.readLine()) ;
ass3a3 s = new ass3a3(number) ;

while(true)
{

System.out.println("\n==========Menu========") ;
System.out.println("\n1.Initialize") ;
System.out.println("\n2.Empty") ;
System.out.println("\n3.Full") ;
System.out.println("\n4.Push") ;
System.out.println("\n5.Pop") ;
System.out.println("\n6.Exit") ;
System.out.println("\n======================") ;

System.out.println("\nEnter your choice here : ") ;
choice = Integer.parseInt(b.readLine()) ;

switch(choice)
{

case 1:
s.initStack() ;
break ;

case 2:
if(s.isEmpty())
System.out.println("\nStack is Empty...") ;

else
System.out.println("\nStack is not Empty...") ;
break ;

case 3:
if(s.isFull())
System.out.println("\nStack is Full...") ;

else
System.out.println("\nStack is not Full...") ;
break ;

case 4:
System.out.println("\nEnter the element to bo pushed in stack : ") ;
number = Integer.parseInt(b.readLine()) ;
s.pushElement(number) ;
break ;

case 5:
number = s.popElement() ;
if(number==-1)
System.out.println("\nCan't pop Stack empty...") ;

else
System.out.println("\nPoped element from stack : "+number) ;
break ;

case 6:
System.exit(0) ;
break ;

default:
System.out.println("Invalid choice.\nEnter again :") ;
break ;

}

}

}

}

/*

Output-

[prady@localhost setA]# javac ass3a3.java
[prady@localhost setA]# java ass3a3
Enter the Number of elements in stack :
3
==========Menu========
1.Initialize
2.Empty
3.Full
4.Push
5.Pop
6.Exit
======================
Enter your choice here :
1
==========Menu========
1.Initialize
2.Empty
3.Full
4.Push
5.Pop
6.Exit
======================
Enter your choice here :
4
Enter the element to bo pushed in stack :
1
==========Menu========
1.Initialize
2.Empty
3.Full
4.Push
5.Pop
6.Exit
======================
Enter your choice here :
2
Stack is not Empty.
==========Menu========
1.Initialize
2.Empty
3.Full
4.Push
5.Pop
6.Exit
======================
Enter your choice here :
4
Enter the element to bo pushed in stack :
2
==========Menu========
1.Initialize
2.Empty
3.Full
4.Push
5.Pop
6.Exit
======================
Enter your choice here :
4
Enter the element to bo pushed in stack :
3
==========Menu========
1.Initialize
2.Empty
3.Full
4.Push
5.Pop
6.Exit
======================
Enter your choice here :
4
Enter the element to bo pushed in stack :
2
OverFlow of stack.
==========Menu========
1.Initialize
2.Empty
3.Full
4.Push
5.Pop
6.Exit
======================
Enter your choice here :
5
Poped element from stack : 3
==========Menu========
1.Initialize
2.Empty
3.Full
4.Push
5.Pop
6.Exit
======================
Enter your choice here :
5
Poped element from stack : 2
==========Menu========
1.Initialize
2.Empty
3.Full
4.Push
5.Pop
6.Exit
======================
Enter your choice here :
5
Poped element from stack : 1
==========Menu========
1.Initialize
2.Empty
3.Full
4.Push
5.Pop
6.Exit
======================
Enter your choice here :
5
UnderFlow of stack.
Can't pop Stack empty.
==========Menu========
1.Initialize
2.Empty
3.Full
4.Push
5.Pop
6.Exit
======================
Enter your choice here :
7
Invalid choice.
Enter again :
==========Menu========
1.Initialize
2.Empty
3.Full
4.Push
5.Pop
6.Exit
======================
Enter your choice here :
6
[prady@localhost setA]#

*/

No comments:

Post a Comment