import java.io.*;
public class LongStack {
private static boolean debug; // debug option
private static int stackCounter = 0; // number of stacks allocated so far
private class LongStackEngine {
// catastrophic error messages
static final String
POP_EMPTY = "Popping from an empty stack!!!\n",
PUSH_FULL = "Pushing to a full stack!!!\n",
TOP_EMPTY = "Topping from an empty stack!!!\n",
WRITE_NONEXIST_FILE
= "Attempt to write using non-existent"
+ " file pointer!!!\n";
// Debug messags
// HEX messages are used for negative values, used in hw4
static final String
ALLOCATE = "[Stack %d has been allocated]\n",
EMPTY = "[Stack %d - Emptied]\n",
JETTISON = "[Stack %d has been jettisoned]\n",
HEXPOP = "[Stack %d - Popping 0x%x]\n",
HEXPUSH = "[Stack %d - Pushing 0x%x]\n",
HEXTOP = "[Stack %d - Topping 0x%x]\n",
POP = "[Stack %d - Popping %d]\n",
PUSH = "[Stack %d - Pushing %d]\n",
TOP = "[Stack %d - Topping %d]\n";
long[] stack; // array to hold the data in stack order
int stackPointer; // index of the last occupied space
int stackSize; // size of the stack
int stackID; // which stack we are using
Tracker tracker; // to keep track of memory usage
LongStackEngine (int stackSize, String caller) {
// allocate a new array to represent the stack
stack = new long[stackSize];
// hold the memory size of the LongStackEngine object
long size = Size.of (stackPointer) + Size.of (stackSize)
+ Size.of (stackID) + Size.of (stack);
tracker = new Tracker ("LongStackEngine", size, caller);
// YOUR CODE GOES HERE
}
void jettisonStackEngine () {
if (debug)
System.err.print (String.format (JETTISON,
stackID));
tracker.jettison ();
stackCounter--;
}
void emptyStack () {
// YOUR CODE GOES HERE
}
Integer getCapacity () {
// YOUR CODE GOES HERE
}
Integer getOccupancy () {
// YOUR CODE GOES HERE
}
boolean isEmptyStack () {
// YOUR CODE GOES HERE
}
boolean isFullStack () {
// YOUR CODE GOES HERE
}
Long pop () {
// YOUR CODE GOES HERE
}
boolean push (long item) {
// YOUR CODE GOES HERE
}
Long top () {
// YOUR CODE GOES HERE
}
void writeStack (PrintStream stream) {
int index = 0; // index into the stack
if (stream == null) {
System.err.print (WRITE_NONEXIST_FILE);
return;
}
int stackOccupancy = getOccupancy ();
if (stream.equals (System.err)) {
stream.print (
"\nStack " + stackID + ":\n"
+ "Stack's capacity is " + stackSize + ".\n"
+ "Stack has "
+ stackOccupancy + " item(s) in it.\n"
+ "Stack can store "
+ (stackSize - stackOccupancy)
+ " more item(s).\n");
Tracker.checkMemoryLeaks ();
}
for (index = 0; index < stackOccupancy; index++) {
if (stream.equals (System.err))
stream.print (String.format (
"Value on stack is |0x%x|\n",
stack[index]));
else {
if (stack[index] < 0)
stream.print (String.format (
"%c ",
(char) stack[index]));
else
stream.print (String.format (
"%d ", stack[index]));
}
}
}
}
// PROVIDED INFRASTRUCTURE BELOW, YOUR CODE SHOULD GO ABOVE
// CHANGING THE CODE BELOW WILL RESULT IN POINT DEDUCTIONS
// catastrophic error messages
private static final String
CAPACITY_NONEXIST = "Capacity check from a non-existent stack!!!\n",
EMPTY_NONEXIST = "Emptying a non-existent stack!!!\n",
ISEMPTY_NONEXIST = "Isempty check from a non-existent stack!!!\n",
ISFULL_NONEXIST = "Isfull check from a non-existent stack!!!\n",
JETTISON_NONEXIST = "Jettisoning a non-existent stack!!!\n",
NUM_NONEXIST = "get_occupancy check from a non-existent stack!!!\n",
POP_NONEXIST = "Popping from a non-existent stack!!!\n",
PUSH_NONEXIST = "Pushing to a non-existent stack!!!\n",
TOP_NONEXIST = "Topping from a non-existent stack!!!\n",
WRITE_NONEXIST_STACK = "Attempt to write to a non-existent stack!!!\n";
private LongStackEngine stackEngine; // the object that holds the data
public LongStack (int stackSize, String caller) {
stackEngine = new LongStackEngine (stackSize, caller);
}
// Debug state methods
public static void debugOn () {
debug = true;
}
public static void debugOff () {
debug = false;
}
public boolean jettisonStack () {
// ensure stack exists
if (!exists ()) {
System.err.print (JETTISON_NONEXIST);
return false;
}
stackEngine.jettisonStackEngine ();
stackEngine = null;
return true;
}
public void emptyStack () {
// ensure stack exists
if (!exists ()) {
System.err.print (EMPTY_NONEXIST);
return;
}
stackEngine.emptyStack ();
}
private boolean exists () {
return !(stackEngine == null);
}
public Integer getCapacity () {
// ensure stack exists
if (!exists ()) {
System.err.print (CAPACITY_NONEXIST);
return null;
}
return stackEngine.getCapacity ();
}
public Integer getOccupancy () {
// ensure stack exists
if (!exists ()) {
System.err.print (NUM_NONEXIST);
return null;
}
return stackEngine.getOccupancy ();
}
public boolean isEmptyStack () {
// ensure stack exists
if (!exists ()) {
System.err.print (ISEMPTY_NONEXIST);
return false;
}
return stackEngine.isEmptyStack ();
}
public boolean isFullStack () {
// ensure stack exists
if (!exists ()) {
System.err.print (ISFULL_NONEXIST);
return false;
}
return stackEngine.isFullStack ();
}
public Long pop () {
// ensure stack exists
if (!exists ()) {
System.err.print (POP_NONEXIST);
return null;
}
return stackEngine.pop ();
}
public boolean push (long item) {
// ensure stack exists
if (!exists ()) {
System.err.print (PUSH_NONEXIST);
return false;
}
return stackEngine.push (item);
}
public Long top () {
// ensure stack exists
if (!exists ()) {
System.err.print (TOP_NONEXIST);
return null;
}
return stackEngine.top ();
}
public void writeStack (PrintStream stream) {
// ensure stack exists
if (!exists ()) {
System.err.print (WRITE_NONEXIST_STACK);
return;
}
stackEngine.writeStack (stream);
}
}
import java.io.*; // System.in and System.out
public class Driver {
private static final int FIRST_STACK_SIZE = 10;
public static void main (String[] args) {
LongStack mainStack = new LongStack (FIRST_STACK_SIZE,
"first mainStack in Driver.main"); // the test stack
int amount; // max numbers of items on stack
char command; // stack command entered by user
Long item; // item to go on stack
char option; // the command line option
boolean status; // return status of stack methods
LongStack.debugOff (); // initialize debug states
// check command line options
for (int index = 0; index < args.length; ++index) {
if (args[index].equals ("-x"))
LongStack.debugOn ();
}
while (true) {
command = 0; // initialize command, need for loops
MyLib.writeline (
"\nPlease enter a command:"
+ "\n\t(a)llocate, "
+ "(c)apacity, "
+ "(C)heck memory, "
+ "is(e)mpty, "
+ "(E)mpty, "
+ "is(f)ull, "
+ "(j)ettison, "
+ "\n\t"
+ "(o)ccupancy, "
+ "(p)op, "
+ "(t)op, "
+ "p(u)sh, "
+ "(w)rite to System.out, "
+ "(W)rite to System.err.\n"
+ "Please enter choice: ", System.out);
try {
command = MyLib.getchar ();
// get rid of extra characters
MyLib.clrbuf (command);
// process commands
switch (command) {
case 'a': // allocate
MyLib.writeline ("Please enter the number of"
+ " objects to be able to store: ",
System.out);
amount = (int) MyLib.decin ();
// get rid of extra characters
MyLib.clrbuf ((char) 0);
mainStack.jettisonStack ();
mainStack = new LongStack (amount,
"mainStack in Driver.main");
break;
case 'C': // check memory leaks
Tracker.checkMemoryLeaks ();
break;
case 'c': // check capacity
MyLib.writeline (
"The capacity of the stack is: "
+ mainStack.getCapacity () + ".\n",
System.out);
break;
case 'e': // isempty
if (mainStack.isEmptyStack ()) {
MyLib.writeline ("Stack is empty.\n",
System.out);
}
else {
MyLib.writeline ("Stack is "
+ "not empty.\n",
System.out);
}
break;
case 'E': // empty
mainStack.emptyStack ();
MyLib.writeline ("Stack is empty.\n",
System.out);
break;
case 'f': // isfull
if (mainStack.isFullStack ())
MyLib.writeline ("Stack is full.\n",
System.out);
else
MyLib.writeline ("Stack is not full.\n",
System.out);
break;
case 'j': // jettison
if (mainStack.jettisonStack ()) {
MyLib.writeline (
"Stack has been jettisoned.\n",
System.out);
}
break;
case 'o': // get_occupancy
MyLib.writeline ("Number of elements"
+ " on the stack is: ",
System.out);
if (mainStack.getOccupancy () != null)
MyLib.decout (
mainStack.getOccupancy ());
MyLib.newline (System.out);
break;
case 'p': // pop
item = mainStack.pop ();
if (item == null)
System.err.print (
"\nWARNING: pop FAILED\n");
else {
MyLib.writeline (
"Number popped from the stack is: ",
System.out);
MyLib.decout (item);
MyLib.newline (System.out);
}
break;
case 't': // top
item = mainStack.top ();
if (item == null)
System.err.print (
"\nWARNING: top FAILED\n");
else {
MyLib.writeline (
"Number at top of the stack is: ",
System.out);
MyLib.decout (item);
MyLib.newline (System.out);
}
break;
case 'u': // push
MyLib.writeline (
"\nPlease enter a number to push to stack: ",
System.out);
item = MyLib.decin ();
// get rid of extra characters
MyLib.clrbuf ((char) 0);
status = mainStack.push (item);
if (!status)
System.err.print (
"\nWARNING: push FAILED\n");
break;
case 'w': // write
MyLib.writeline ("\nThe Stack contains:\n",
System.out);
mainStack.writeStack (System.out);
break;
case 'W': // write in stderr
MyLib.writeline ("\nThe Stack contains:\n",
System.err);
mainStack.writeStack (System.err);
break;
}
}
catch (EOFException eof) {
System.err.println ();
break;
}
}
if (mainStack != null)
mainStack.jettisonStack (); // deallocate stack
Tracker.checkMemoryLeaks ();
MyLib.newline (System.out);
}
}