我正在编写一个添加2个分数的程序。我有3个类别:UI,Rhnhnen和RechnenTest。给出了UI和RechnenTest类,我不得不编写Rechnen类,添加了分数。问题是,程序运行良好,但JUnit测试失败。这些是类:
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class UserInterface {
public static void main(String[] args) {
while (true) {
String[] userData = getData_Dialog();
if (userData == null)
break;
int z1 = 0;
int n1 = 0;
int z2 = 0;
int n2 = 0;
try {
z1 = Integer.parseInt(userData[0]);
n1 = Integer.parseInt(userData[1]);
z2 = Integer.parseInt(userData[2]);
n2 = Integer.parseInt(userData[3]);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,
"Bitte nur ganze Zahlen eingeben, Nenner != 0");
continue;
}
try {
JOptionPane.showMessageDialog(null,
"Summe: " + Rechnen.bruchAddition(z1, n1, z2, n2));
} catch (IllegalArgumentException e) {
JOptionPane
.showMessageDialog(null,
"Mindestens eine der Zahlen war nicht im vorgebenen Bereich");
}
}
JOptionPane.showMessageDialog(null, "Danke und auf Wiedersehen!");
System.exit(0);
}
private static String[] getData_Dialog() {
JTextField z1TF = new JTextField();
JTextField n1TF = new JTextField();
JTextField z2TF = new JTextField();
JTextField n2TF = new JTextField();
Object[] message = { "Zaehler 1", z1TF, "Nenner 1", n1TF, "Zaehler 2",
z2TF, "Nenner 2", n2TF };
Object[] options = { "Addition", "Abbruch" };
int n = JOptionPane.showOptionDialog(new JFrame(), message,
"Brueche Addieren", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (n == JOptionPane.OK_OPTION) { // Zustimmung
return new String[] { z1TF.getText(), n1TF.getText(),
z2TF.getText(), n2TF.getText() };
} else if (n == JOptionPane.NO_OPTION // Verneinung
|| n == JOptionPane.CLOSED_OPTION) { // Dialogfenster
// geschlossen
return null;
} else {
return null;
}
}
}
import javax.swing.JOptionPane;
public class Rechnen {
public static String bruchAddition(int z1, int n1, int z2, int n2)
throws IllegalArgumentException {
int m = (z1 * n2) + (z2 * n1);
int n = (n1 * n2);
if (n1 <= 0 || n2 <= 0 || z1 < 0 || z2 < 0) {
throw new IllegalArgumentException();
}
return m / ggt(m, n) + "/" + n / ggt(m, n);
}
public static int max(int x, int y) {
return x > y ? x : y;
}
public static int min(int x, int y) {
return x > y ? y : x;
}
public static int ggt(int x, int y) {
return (x == y) ? x : ggt(max(x, y) - min(x, y), min(x, y));
}
}
import static org.junit.Assert.*;
import org.junit.Test;
public class RechnenTest {
@Test
public void test() {
assertEquals("Zaehler = 1 Nenner = 1",
rechnen.Rechnen.bruchAddition(1, 3, 2, 3));
assertEquals("Zaehler = 1 Nenner = 1",
rechnen.Rechnen.bruchAddition(5, 8, 3, 8));
assertEquals("Zaehler = 1 Nenner = 1",
rechnen.Rechnen.bruchAddition(10, 16, 3, 8));
assertEquals("Zaehler = 1 Nenner = 3",
rechnen.Rechnen.bruchAddition(-1, 3, 2, 3));
assertEquals("Zaehler = -1 Nenner = 2",
rechnen.Rechnen.bruchAddition(0, 2, -1, 2));
assertEquals("Zaehler = -2 Nenner = 3",
rechnen.Rechnen.bruchAddition(-1, 3, 1, -3));
try {
rechnen.Rechnen.bruchAddition(1, 1, 1, 0);
fail();
} catch (IllegalArgumentException e) {
assertTrue(true);
}
try {
rechnen.Rechnen.bruchAddition(Integer.MAX_VALUE, 1, 1, 1);
fail();
} catch (IllegalArgumentException e) {
assertTrue(true);
}
assertEquals("Zaehler = 1 Nenner = " + Integer.MAX_VALUE,
rechnen.Rechnen.bruchAddition(0, Integer.MAX_VALUE, 1,
Integer.MAX_VALUE));
}
}
语言是德语。
谢谢
最佳答案
当我自己运行代码时,我得到:
org.junit.ComparisonFailure: expected:<[Zaehler = 1 Nenner = ]1> but was:<[1/]1>
该测试期望
bruchAddition
返回具有不同格式的字符串。您可以通过将bruchAddition
的最后一行更改为以下内容来解决此问题:return "Zaehler = " + m / ggt(m, n) + " Nenner = " + n / ggt(m, n);
这使我们通过了前三个断言,但是随后对
Rechnen.bruchAddition(-1, 3, 2, 3)
的调用因IllegalArgumentException
而失败。这是因为bruchAddition
拒绝z1或z2 如果将
bruchAddition
更改为接受负的z1和z2(如单元测试要求),则由于java.lang.StackOverflowError
递归调用自身而显然无法正确结束递归,因此您将得到ggt
。用
ggt
,max
和min
查找最大公约数的技术似乎已损坏。 min(1, -3)
返回-3。您确定那是您想要的吗?我有一种感觉,在这种情况下,您的算法需要它返回1,即最小的绝对值。