每次我尝试修复代码时都会发生StackOverflowError

时间:2013-11-19 20:01:04

标签: java junit fractions stack-overflow greatest-common-divisor

我正在编写一个添加2个分数的程序。我有3个类:UI,Rechnen和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));
    }
}

语言是德语。

由于

1 个答案:

答案 0 :(得分:1)

当我自己运行代码时,我得到:

org.junit.ComparisonFailure: expected:<[Zaehler = 1 Nenner = ]1> but was:<[1/]1>

测试期望bruchAddition返回具有不同格式的String。您可以通过将bruchAddition的最后一行更改为:

来解决问题
return "Zaehler = " + m / ggt(m, n) + " Nenner = " + n / ggt(m, n);

这让我们完成了前三个断言,但随后对Rechnen.bruchAddition(-1, 3, 2, 3)的调用失败了IllegalArgumentException。这是因为bruchAddition拒绝了z1或z2&lt; 0

如果您将bruchAddition更改为接受负z1和z2(因为单元测试需要),那么由于java.lang.StackOverflowError递归调用自身并且显然没有结束递归,您会得到ggt正常。

使用ggtmaxmin查找最大公约数的技巧似乎已被打破。 min(1, -3)返回-3。你确定那是你想要的吗?我觉得你的算法需要它在这种情况下返回1,即最小的绝对值。