Professional Documents
Culture Documents
java
* Author: Seth Cram
* Description:
* A stack that holds ten integers and prioritizes odd number,
* without using a Stack Library. Its output is shown in
* outputOddStack.txt, and W11Main.java is its test file.
*
*Instructions:
* Create a file called OddStack.Java. In it create a class OddStack.
*
* OddStack must have 3 methods:
* public void push(int e)
* public int pop()
* @Override (not required but helps prevent errors)
* public String toString()
*
* OddStack is like most stacks. It takes in integers. The first integer
* pushed in is the last one popped out, but OddStack has one odd feature:
* all the odd integers that are pushed (like 5, 37 or 1) get popped before
* any even integer get popped even if an even integer is pushed after the
* odd ones. Other than that it maintains the order.
* The toString method prints out the elements in the form
* [e1, e2, ... en], but it prints them in the order they would be popped
* (not the order that they were pushed).
* You are free to implement your OddStack any way you want, as long as
* you do not use a Stack library from Java.
* The stack itself holds 10 elements. If you try to push to a full stack
* it must give the error:
* Error: OddStack is full
* If you try to pop form a stack that is empty it must give the error:
* Error: OddStack is empty
* Beyond that they only other thing that it should print is a confirmation
* when you push an element.
* For example push(35) should report:
* Pushing: 35
* Remember, you have limited space for compiling and running your program,
* so do not make your class too complex.
*
* Compile:
* javac -J-Xmx10m -J-Xms5m OddStack.java
* Run:
* java -Xmx10m -Xms5m W11Main
*
* Stack is a FIFO, first in first out, data structure
*
*/
//constructor
public OddStack(){
//odd and even arrays start with depths of 0
ocurr_depth = 0;
ecurr_depth = 0;
max_depth = 10; //can't hold more than 10 ints
}
//adds number passed in to the top of either the even or odd array
public void push(int num){
System.out.println("Pushing: " + num);
//placeholders for odd and even depth so method caller's odd and deven depths
aren't altered
int oddPH = ocurr_depth;
int evenPH = ecurr_depth;
}
completeList = completeList + "]";