// This object will implement the stack. class MyStack { // The actual structure used will be an array. double[] stack; // To keep track of the top: int stackTop; // A method to initialize. We'll assume no more than 100 elements. public void initialize () { stack = new double [100]; } // Push and pop as usual. public void push (double value) { stackTop ++; stack[stackTop] = value; } public double pop () { double value = stack[stackTop]; stackTop --; return value; } public boolean isEmpty () { if (stackTop < 0) { return true; } else { return false; } } } // This class has the same name as the file and has main(). public class StackExample2 { public static void main (String[] argv) { // Create an instance of the MyStack class. MyStack stack = new MyStack (); // Initialize before using. stack.initialize (); // Dump stuff on stack. stack.push (3.141); stack.push (2.718); stack.push (1.618); // Print while extracting in order. while (! stack.isEmpty ()) { System.out.println ("Removed " + stack.pop()); } } }