Hey, I am working on a project that requires to write a class that implements a
ID: 3592379 • Letter: H
Question
Hey, I am working on a project that requires to write a class that implements a stack interface. Interface I wrote is given below even though I am not really sure if I need isFull or isEmpty.
However The most important aspect of the project is I CAN NOT USE ANY EXCEPTION for underflow and overflow.
I have to handle errors inside push, pop and top method. Here push and pop method will return true if it can be pushed and popped and if it can't it will return false. if there is top element it will return it if it desnt have it will return null.
How do i go about writing this class without exceptions as well as test method for this class? I also dont understand if it will really make any difference with exceptions or handling errors without it.
public interface MyIntface<T> {
void push ( T members) ;
void pop() ;
T top();
boolean isFull();
boolean isEmpty();
}
Explanation / Answer
//MyIntface.java
public interface MyIntface<T> {
boolean push ( T members) ;
boolean pop() ;
T top();
boolean isFull();
boolean isEmpty();
}
--------------------------------------------------------------------------------
//MyStack.java
public class MyStack<T> implements MyIntface<T>{
private static final int CAPACITY = 15;
private int top;
private T[] A;
/**
* Constructor to set capacity
*/
public MyStack(int capcaity)
{
if (capcaity <= 0)
A = (T[]) new Object[CAPACITY];
else
A = (T[]) new Object[capcaity];
//stack is empty
top = -1;
}
/**
* Creates a Stack with
* default capacity
*/
public MyStack()
{
this(CAPACITY);
}
/**
* Tests if the stack is empty.
*/
public boolean isEmpty()
{
return top==-1;
}
/**
* Returns the top item
* Return null if empty
*/
public T top()
{
if (isEmpty())
return null;
return A[top];
}
/**
* Removes and returns the item at the top of this stack.
*/
public boolean pop()
{
T x;
if(!isEmpty())
{
x = top();
top--;
return true;
}
return false;
}
/**
* Inserts an item onto the top of the stack.
*/
public boolean push(T e)
{
if (top != A.length)
{
top++;
A[top] = e;
return true;
}
return false;
}
/**
* Removes all items from the Stack.
*/
public void clear()
{
for(int i = 0; i <= top; i++)
A[i] = null;
top = -1;
}
/**
* Returns a string
* representation of the Stack.
*/
public String toString()
{
if(isEmpty()) return "[ ]";
StringBuffer out = new StringBuffer("[");
for(int i = 0; i < top; i++)
out.append(A[i] + ", ");
out.append(A[top] + "]");
return out.toString();
}
//Return true if stack is full
public boolean isFull() {
return top==CAPACITY;
}
}
--------------------------------------------------------------------------------
//MyStack.java
public class MyStack<T> implements MyIntface<T>{
private static final int CAPACITY = 15;
private int top;
private T[] A;
/**
* Constructor to set capacity
*/
public MyStack(int capcaity)
{
if (capcaity <= 0)
A = (T[]) new Object[CAPACITY];
else
A = (T[]) new Object[capcaity];
//stack is empty
top = -1;
}
/**
* Creates a Stack with
* default capacity
*/
public MyStack()
{
this(CAPACITY);
}
/**
* Tests if the stack is empty.
*/
public boolean isEmpty()
{
return top==-1;
}
/**
* Returns the top item
* Return null if empty
*/
public T top()
{
if (isEmpty())
return null;
return A[top];
}
/**
* Removes and returns the item at the top of this stack.
*/
public boolean pop()
{
T x;
if(!isEmpty())
{
x = top();
top--;
return true;
}
return false;
}
/**
* Inserts an item onto the top of the stack.
*/
public boolean push(T e)
{
if (top != A.length)
{
top++;
A[top] = e;
return true;
}
return false;
}
/**
* Removes all items from the Stack.
*/
public void clear()
{
for(int i = 0; i <= top; i++)
A[i] = null;
top = -1;
}
/**
* Returns a string
* representation of the Stack.
*/
public String toString()
{
if(isEmpty()) return "[ ]";
StringBuffer out = new StringBuffer("[");
for(int i = 0; i < top; i++)
out.append(A[i] + ", ");
out.append(A[top] + "]");
return out.toString();
}
//Return true if stack is full
public boolean isFull() {
return top==CAPACITY;
}
}
--------------------------------------------------------------------------------
Sample Output:
Stack elements
[1, 2, 3, 4]
Pop element 4
Stack elements
[1, 2, 3]
Stack elements
[USA, INDIA, UK]
Pop element UK
Stack elements
[USA, INDIA]