我希望程序做什么:创建一个堆栈并使用HP12C类中的calculate方法对其进行操作。
我有点迷失在这里,因为我已经有一段时间了,因为我使用Java而且我在创建和使用对象时遇到麻烦,如果你可以帮助我...
自从我上次在这里发帖以来,人们一直在责备我发布我的作业,我不会那样做。如果你觉得我的问题太多了,那就给我一个关于我做错的提示,也许我可以从那里开始。感谢。
public interface Stack {
public void push(String element);
public String pop();
public int size();
public String peek();
}
public class LinkedStack implements Stack {
String elements[];
int top;
@Override
public void push(String element) {
top++;
elements[top] = element;
}
@Override
public String pop() {
String element;
element = elements[top];
top--;
return element;
}
@Override
public int size() {
return 0;
}
@Override
public String peek() {
return null;
}
}
public class HP12C {
public Stack stack;
public HP12C() {
stack = new LinkedStack();
}
public double calculate(String line) {
String n, n1, n2;
double d = 0, d1 = 0, d2 = 0;
// Quebrar a linha nos parametros
String elements[] = line.split(" ");
// Empilhar e calcular
for (String e : elements) {
// Distinguir o que eh operador e o que nao eh
switch (e) {
case "+":
n1 = stack.pop();
d1 = Double.parseDouble(n1);
n2 = stack.pop();
d2 = Double.parseDouble(n2);
d = d1 + d2;
n = String.valueOf(d);
stack.push(n);
break;
case "-":
n1 = stack.pop();
d1 = Double.parseDouble(n1);
n2 = stack.pop();
d2 = Double.parseDouble(n2);
d = d1 - d2;
n = String.valueOf(d);
stack.push(n);
break;
case "*":
n1 = stack.pop();
d1 = Double.parseDouble(n1);
n2 = stack.pop();
d2 = Double.parseDouble(n2);
d = d1 * d2;
n = String.valueOf(d);
stack.push(n);
break;
case "/":
n1 = stack.pop();
d1 = Double.parseDouble(n1);
n2 = stack.pop();
d2 = Double.parseDouble(n2);
d = d1 / d2;
n = String.valueOf(d);
stack.push(n);
break;
default:
stack.push(e);
break;
}
stack.push(e);
}
return -1;
}
}
import static org.junit.Assert.*;
import org.junit.Test;
public class HP12CTest {
@Test
public void testSimple() {
HP12C hp = new HP12C();
double result = hp.calculate("10 15 +");
assertEquals(25, result, 0);
}
}
错误的痕迹:
java.lang.NullPointerException
at LinkedStack.push(LinkedStack.java:9)
at HP12C.calculate(HP12C.java:55)
at HP12CTest.testSimple(HP12CTest.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
答案 0 :(得分:2)
在LinkedStack中,您有一个永不初始化的字段:
String elements[];
这相当于
String elements[] = null;
当您尝试访问elements[top]
时,您使用空引用进行访问,从而导致异常。
请参阅:How to initialize an array in Java?
请注意,如果您将堆栈初始化为例如20个元素,那么您将需要处理该类用户尝试将超过20个元素推送到堆栈的情况。
您可以通过复制基础数组并将大小加倍来实现此目的,或者您可以使用自动调整大小的数据结构like one of Java's List
implementations而不是基本数组。
答案 1 :(得分:0)
你必须初始化你的数组:
String[] elements = new String[lengthOfYourArray];