Java operand Stack Need assistance completing the code below. Program is mostly
ID: 3757549 • Letter: J
Question
Java operand Stack
Need assistance completing the code below.
Program is mostly complete ust needs to complete the sections with "YOUR CODE HERE" that are left.
1 package OperandStack; import Value.* import java.io.* 4 import Utilities.Error sok A stack for doing arithmetic and logic operations. Stack elements are of type felink Value) 7 @author Matt B. Pedersen 8 @version 1.0 ok/ 10 public class OperandStack 11 o* 12 The size of the operand stack. We could have made this dynamic, but as long as we choose a number 13 large enough it should be fine with a fixed sized stack 4 15 private int stackSize; 16 /sok 17 * The internal stack holding the actual values. These values are of type felink Value). 18 9 1/ Hint, if you don't make it an array the toString) method wont work as it is-) 20 II YOUR CODE HERE 21 /ok 22 * The stack pointer. The stack pointer always points to the next free Location in the a href-#stack,stack array 23 24 I/ YOUR CODE HERE 25 sok 26 Just a name to give to the stack -helps with debugging later 27 28 29/sok 30 We keep track of the numbers of stacks created - just for statistics later. 31/ 32 private static int stackNo-0 private String name public int stackNumber; 34 /ok 35 Creates a new operand stack of size size and sets the stack pointer to e. 6 @param size The size of the newly created operand stack 37 *@see #stack 38 * @see #sp 39/ public OperandStack(int size, String name)Explanation / Answer
package OperandStack;
import Value.*;
import java.io.*;
import Utilities.Error;
public class OperandStack {
/**
* The size of the operand stack. We could have made this dynamic, but as long as we choose a number
* large enough it should be fine with a fixed sized stack
*/
private int stackSize;
/**
* The internal stack holding the actual values. These values are of type {@link Value}.
*/
// Hint, if you don't make it an array the toString() method wont work as it is ;-)
//<--
private Value[] stack;
//-->
/**
* The stack pointer. The stack pointer always points to the next free location in the <a href="#stack">stack<a> array.
*/
//<--
private int sp = 0;
//-->
/**
* Just a name to give to the stack - helps with debugging later.
*/
private String name;
/**
* We keep track of the numbers of stacks created - just for statistics later.
*/
private static int stackNo = 0;
public int stackNumber;
/**
* Creates a new operand stack of size <b>size</b> and sets the stack pointer to 0.
* @param size The size of the newly created operand stack.
* @see #stack
* @see #sp
*/
public OperandStack(int size, String name) {
this.name = name;
stackNumber = stackNo;
stackNo++;
//<--
stackSize = size;
stack = new Value[stackSize];
sp = 0;
//-->
}
/**
* Pushes one element of type {@link Value} on to the operand stack and increments the stack pointer (sp) by one.
* <p>
* stack before push: .... X<br>
* push(Y);<br>
* stack after push: .... X Y
* <p>
* An error is signaled if no more room is available on the stack.
* @param e An object of the {@link Value} type to be placed on the stack.
*/
public void push(Value e) {
//<--
if (sp < stackSize) {
stack[sp++] = e;
//dump();
} else
Error.error(stackNumber + " OperandStack.push: Stack overflow.");
//-->
}
/**
* Pops one element of type {@link Value} off the operand stack and decrements the stack pointer (sp) by one.
* <p>
* stack before pop: .... X Y<br>
* Z = pop();<br>
* stack after pop: .... X<br>
* and Z = Y
* <p>
* An error is signaled if the stack is empty.
* @return Returns an object of type {@link Value}.
*/
public Value pop() {
//<--
if (sp >0)
return stack[--sp];
else
Error.error(stackNumber +" OperandStack.pop: Stack underflow.");
return null;
//-->
}
/**
* Returns the n'th element on the stack (counted from the top)
* without removing it.
*
* @param n The index (counting from the top of the stack) of the
* element to be returned. The top element is at index 1.
*/
public Value peek(int n) {
//<--
if (sp-(n-1) > 0)
return stack[sp-n];
else
Error.error(stackNumber + " OperandStack.peek: Stack underflow.");
return null;
//-->
}
/**
* Prints out the operand stack with information about every elements type.
*/
public void dump(PrintWriter out) {
out.println(toString());
}
public String toString() {
String s = "";
s = "| Operand Stack " + stackNumber + " - "+ name + " (size = "+sp+") --------------- | ";
for (int i=0;i<sp;i++) {
if (stack[i] == null)
s = s + "null ";
else
s = s + stack[i] + "{" + stack[i].type2String()+ "} ";
}
s = s + " +----------------------------------------------------------------";
return s;
}
public void dump_(PrintWriter out) {
int max = 20;
// compute the max width of any element
for (int i=0;i<sp;i++) {
if (stack[i] == null)
max = 6 > max ? 6 : max; // ' null '
else {
int l;
l = (stack[i] + " {" + stack[i].type2String()+ "}").length();
max = l > max ? l : max;
}
}
int left, right, spcs;
for (int i=0;i<sp;i++) {
if (stack[sp-i-1] == null) {
spcs = max - 6;
left = spcs/2;
right = spcs - left;
out.println("|" + spaces(left) + " null " + spaces(right) + "|");
} else {
String st = "" + stack[sp-i-1] + " {" + stack[sp-i-1].type2String()+ "}";
spcs = max - st.length();
left = spcs / 2;
right = spcs - left;
out.println("|" + spaces(left) + st + spaces(right) + "|");
}
}
}
private String spaces(int n) {
String s = "";
for (int i =0; i<n; i++)
s += " ";
return s;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.