CS 535: Object-Oriented Programming & Design |
---|
class Stack[1] { private float[] elements; private int topOfStack = -1; public Stack( int stackSize ) { elements = new float[ stackSize ]; } public void push( float item ) { elements[ ++topOfStack ] = item; } public float pop() { return elements[ topOfStack-- ]; } public boolean isEmpty() { if ( topOfStack < 0 ) return true; else return false; } public boolean isFull() { if ( topOfStack >= elements.length ) return true; else return false; } }
Stack me = new Stack( 20 ); me.push( 5 ); me.push( 12 ); System.out.println( me.pop() ); System.out.println( me.pop() );
struct Stack { float stack[100]; int topOfStack; }; void push(Stack& it, int item) { it.stack[(it.topOfStack)++] = item; } float pop(Stack& it) { return it.stack[--(it.topOfStack)]; } main() { Stack tryThisOut; Stack yours, mine; tryThisOut.topOfStack = 0; yours.topOfStack = 0; push( tryThisOut, 5.0 ); push( yours, 3.3 ); push( tryThisOut, 9.9 ); cout << pop( tryThisOut ) << endl; }
Stack troubleAhead; troubleAhead.topOfStack = 13; troubleAhead.stack[ 8 ] = 29;
class StackData { public float[] elements = new float[100]; public int topOfStack = 0; } class Test { static void push(Stack it, int item) { it.stack[ ( it.topOfStack )++ ] = item; } static float pop(Stack it) { return it.stack[--(it.topOfStack)]; } public void static main( String[] args) { Stack yours, mine; push( yours, 3.3 ); push( mine, 9.9); }
class StackData { private float[] elements = new float[100]; private int topOfStack = -1; public int getTopOfStack() { return topOfStack; } public void setTopOfStack( int newTop ) { topOfStack = newTop; } public float getElement( int elementIndex ) { return elements[ elementIndex ]; } public void getElement( int elementIndex, float element ) { elements[ elementIndex ] = element; } }
class Stack { private float[] elements = new float[ 100 ]; private int topOfStack = -1; public void push( ) { float item = Console.readFloat( "Type a float to push"); elements[ ++topOfStack ] = item; } public void pop() { Console.println( "Top stack item: " + elements[topOfStack--]; } }
class Test { public void static main( String[] args) { Stack yours ohNo = new Stack(); ohNo.push( ); ohNo.push( ); ohNo.pop( ); }
class CalculatorEngine { private Stack operands = new Stack( 100 ); public float evaluate( String expression ) { StringTokenizer tokenList = new StringTokenizer( expression ); while ( tokenList.hasMoreTokens() ) processToken( tokenList.nextToken() ); return operands.pop(); } private void processToken( String token ) { if ( token.equals( "+" ) ) operands.push( operands.pop() + operands.pop() ); else if ( token.equals( "-" ) ) operands.push( operands.pop() - operands.pop() ); else if ( token.equals( "/" ) ) operands.push( operands.pop() / operands.pop() ); else if ( token.equals( "*" ) ) operands.push( operands.pop() * operands.pop() ); else operands.push( stringToFloat( token ) ); } private float stringToFloat( String number ) { return Float.valueOf( number ).floatValue(); } }
class CalculatorGUI { CalculatorEngine rpnEvaluator; public CalculatorGUI( CalculatorEngine myComputeEngine ) { rpnEvaluator = myComputeEngine; // code to build the gui interface removed } // code to make gui run removed }
public static void main( String[] arguments ) { CalculatorGUI fourBanger; fourBanger = new CalculatorGUI( new CalculatorEngine() ); }