61 lines
1.3 KiB
Java
61 lines
1.3 KiB
Java
|
|
import java.util.Vector;
|
|
|
|
/**
|
|
* RationalStack provides a stack collection of Rational objects
|
|
*/
|
|
public class RationalStack
|
|
{
|
|
private Vector<Rational> _data = new Vector<Rational>();
|
|
|
|
/**
|
|
* pop will remove and return the Rational object from the top of the stack
|
|
* @return the Rational at the top of the stack
|
|
*/
|
|
public Rational pop()
|
|
{
|
|
int sz = _data.size();
|
|
if (sz > 0)
|
|
{
|
|
Rational r = _data.remove(sz-1);
|
|
return r;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* push will add a new Rational object to the top of the stack
|
|
* @param value the Rational object to add to the stack
|
|
*/
|
|
public void push(Rational value)
|
|
{
|
|
_data.add(value);
|
|
}
|
|
|
|
/**
|
|
* isEmpty will return true if there are no Rational elements on the stack
|
|
* @return true if there are no Rational elements on the stack
|
|
*/
|
|
public boolean isEmpty()
|
|
{
|
|
return _data.isEmpty();
|
|
}
|
|
|
|
/**
|
|
* clear will remove all Rational objects from the stack
|
|
*/
|
|
public void clear()
|
|
{
|
|
_data.clear();
|
|
}
|
|
|
|
/**
|
|
* size returns the number of Rational objects in the stack
|
|
* @return the number of Rational objects in the stack
|
|
*/
|
|
public int size()
|
|
{
|
|
return _data.size();
|
|
}
|
|
}
|