Skip to content
Snippets Groups Projects
Commit 4cf4bcfb authored by njal3001's avatar njal3001
Browse files

Closes (#1)

parent 84bd1ee3
Branches error-fix-1
No related tags found
No related merge requests found
......@@ -38,10 +38,11 @@ public class Calc {
* @throws IllegalArgumentException if n is larger than the operand count
*/
public double peekOperand(int n) {
if (n > getOperandCount()) {
int index = getOperandCount() - n - 1;
if (index >= getOperandCount() || index < 0) {
throw new IllegalArgumentException("Cannot peek at position " + n + " when the operand count is " + getOperandCount());
}
return operandStack.get(getOperandCount() - n - 1);
return operandStack.get(index);
}
/**
......
......@@ -87,9 +87,9 @@ public class CalcTest {
public void testSwap() {
Calc calc = new Calc(1.0, 3.14);
calc.swap();
checkCalc(calc, 3.14, 1.0);
calc.swap();
checkCalc(calc, 1.0, 3.14);
calc.swap();
checkCalc(calc, 3.14, 1.0);
}
@Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment