SO YOU THINK YOU CAN JAVA: IV SET THIS SIMPLE TEST, SHOULD TAKE 5 MINS, SO WHAT ARE YOU WAITING FOR? WHAT IS THE CORRECT AND COMPLETE CODE?
AKA787
// Stacker.java: // // A partial implementation of a simple stack mechanism -- // are you good enough to complete it? Whats wrong with the rest of the code? // // Aka787 //
import java.io.*; import java.util.*;
class Stack { // Stack data structure private int size; private int[] stack; private int index; public boolean empty(); public Object peek(); public Object pop(); public Object push(Object item); public int search(Object o);
// Returns the number of elements in the stack int count() { return index; }
// Returns true if stack empty, otherwise false boolean isEmpty() { return index == 0; }
// Returns true if stack full, otherwise false boolean isFull() { return index == size; }
// Class invariant [Incomplete] boolean StackClassInvariant() { // Stack index always within valid range }
// Pushes the value n on top of the stack [Incomplete] void push(int n) { // stack not full
// *** code goes here ***
// number of elements within stack is increased by 1 }
// Removes and returns the top element from the stack [Incomplete] int pop() { // stack not empty
// *** code goes here ***
// number of elements within stack is decreased by 1 }
// Resets the stack index [Incomplete] void reset() { // always applicable
// *** code goes here ***
// number of elements within stack is 0 }
// Returns the value of the top element on the stack [Incomplete] int top() { // stack not empty while ( index>0 ) { screen.println("top element on the stack "+Stack.peek().toString()); }
}
}
class Stacker { static BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in)); static PrintWriter screen = new PrintWriter (System.out, true);
public static void main(String[] args ) throws IOException { final int SIZE = 5; int cmd; boolean quit = false; Stack MyStack = new Stack(SIZE);