program to understand concept of Interfaces.
/*
Aim : Write a program to understand concept of Interfaces.
*/
import java.lang.*;
interface stack
{
void push(int item);
int pop();
}
class fix implements stack
{
private int st[];
private int top;
private int max;
fix(int size)
{
st= new int[size];
top=-1;
max=size;
}
public void push(int item)
{
if(top==max-1)
System.out.print("\t!!!!!Overflow!!!!!!!!!!");
else
st[++top]=item;
}
public int pop()
{
if(top==-1)
System.out.println("\t\t!!!!!!!!!!!!Underflow!!!!!!!!!");
else
return(st[top--]);
return(-1);
}
}
class pr_4
{
public static void main(String args[])
{
int i,j;
System.out.println("\tFixing object ie stack with size 7 ");
fix obj =new fix(7);
System.out.println("\n\tInserting Elements as follow (ie. pushing 1 t0 9 sequence)....");
for(i=1;i<10;i++)
{
System.out.print("\n\t\tElement : "+ i );
obj.push(i);
}
System.out.println("\n\tPoping out elements as follow (ie. 9 times poping)...");
for(i=1;i<10;i++)
{
j=obj.pop();
if(j!=-1)
System.out.println("\t\tpoped element : "+j);
}
}
}
/* OUTPUT
prashant@prashant-OptiPlex-755:~$ javac pr_5.java
prashant@prashant-OptiPlex-755:~$ java pr_5
Fixing object ie stack with size 7
Inserting Elements as follow (ie. pushing 1 t0 9 sequence)....
Element : 1
Element : 2
Element : 3
Element : 4
Element : 5
Element : 6
Element : 7
Element : 8 !!!!!Overflow!!!!!!!!!!
Element : 9 !!!!!Overflow!!!!!!!!!!
Poping out elements as follow (ie. 9 times poping)...
poped element : 7
poped element : 6
poped element : 5
poped element : 4
poped element : 3
poped element : 2
poped element : 1
!!!!!!!!!!!!Underflow!!!!!!!!!
!!!!!!!!!!!!Underflow!!!!!!!!!
prashant@prashant-OptiPlex-755:~$
*/
0 comments:
Post a Comment