Auf Thema antworten

Ist mein Code und meine Test-Methode richtig? Wie kann ich das verbessern? Was ist falsch? Was ist der einfache Weg? Wie kann ich noch vereinfachen?


[CODE=java]class Produzieren

{

    int leerZeichen=0;

    String umgedreht = new String();

    /**

     * Liefert die Fakultaet von n, also das Produkt aller natuerlichen Zahlen bis n.

     * Die Fakultaet von 0 ist per mathematischer Definition 1.

     *

     * Beispielauswertung:

     *

     *   fak(4)

     * -> 4 * fak(3)

     * -> 4 * (3 * fak(2))

     * -> 4 * (3 * (2 * fak(1)))

     * -> 4 * (3 * (2 * (1 * fak(0))))

     * -> 4 * (3 * (2 * (1 * 1)))

     * -> 4 * (3 * (2 * 1))

     * -> 4 * (3 * 2)

     * -> 4 * 6

     * -> 24

     */

    public int fak(int n)

    {

        int res=1;

        if (n>0)

        {

            res = n*fak(n-1);

        }

        return res;

    }


    /**

     * Berechnet n modulo m, also den Rest der ganzzahligen Division n/m.

     *

     * modulo(14,3) -> modulo(11,3) -> modulo(8,3) -> modulo(5,3) -> modulo(2,3) -> 2

     *

     * @param n ein beliebiger Wert >= 0

     * @param m ein beliebiger Wert > 0

     */

    public int modulo(int n, int m)

    {

        int res=0;

        if (m>0)

        {

            if (n>=m)

            {

                res = modulo (n-m, m);

            }

            else

            {

                res = n;

            }

        }

        else

        {

            throw new IllegalArgumentException("m darf nicht negativ sein!");

        }

        return res;

    }


    /**

     * Gibt an, ob die Zeichenkette einen Vokal enthaelt. Auswertungen:

     *

     * enthaeltVokal("brei") -> enthaeltVokal("rei") -> enthaeltVokal("ei") -> true

     * enthaeltVokal("xyz") -> enthaeltVokal("yz") -> enthaeltVokal("z") -> enthaeltVokal("") -> false

     * @param s ein beliebiger String ungleich null

     */

    public boolean enthaeltVokal(String s)

    {

        boolean res = false;

        if (s.length()==0 && s==null)

        {

            throw new IllegalArgumentException("Sting darf nicht null sein!");

        }

        else

        {

            s = s.toLowerCase();

            if (s.length()!=0)

            {

                if (s.charAt(0) == 'a' || s.charAt(0) == 'e' || s.charAt(0) == 'i' || s.charAt(0) == 'o' || s.charAt(0) == 'u')

                {

                    res = true;

                }

                else

                {

                    return enthaeltVokal(s.substring(1));

                }

            }

        }

        return res;

    }


    /**

     * Gibt an, ob die Zeichenkette ein Palindrom ist. Auswertungen:

     *

     * istPalindrom("anna") -> istPalindrom("nn") -> istPalindrom("") -> true

     * istPalindrom("asta") -> istPalindrom("st") -> false

     * istPalindrom("axa") -> istPalindrom("x") -> true

     * istPalindrom("xyz") -> false

     * @param s ein beliebiger String ungleich null

     */

    public boolean istPalindrom(String s)

    {

        if (s.length()==0 && s==null)

        {

            throw new IllegalArgumentException("Sting darf nicht null sein!");

        }

        else

        {

            s = s.toLowerCase();

            if (s.length() <= 1)

            {

                return true;

            }

            else if (s.charAt(0) != s.charAt(s.length() - 1))

            {

                return false;

            }

        }

        return istPalindrom(s.substring(1, s.length() - 1));

    }


    /**

     * Berechnet die Anzahl Leerzeichen in der Zeichenketten. Auswertung:

     *

     * anzahlLeerzeichen("a bc d")

     * -> 0 + anzahlLeerzeichen(" bc d")

     * -> 0 + (1 + anzahlLeerzeichen("bc d"))

     * -> 0 + (1 + (0 + anzahlLeerzeichen("c d")))

     * -> 0 + (1 + (0 + (0 + anzahlLeerzeichen(" d"))))

     * -> 0 + (1 + (0 + (0 + (1 + anzahlLeerzeichen("d")))))

     * -> 0 + (1 + (0 + (0 + (1 + (0 + anzahlLeerzeichen(""))))))

     * -> 0 + (1 + (0 + (0 + (1 + (0 + 0)))))

     * -> 0 + (1 + (0 + (0 + (1 + 0))))

     * -> 0 + (1 + (0 + (0 + 1)))

     * -> 0 + (1 + (0 + 1))

     * -> 0 + (1 + 1)

     * -> 0 + 2

     * -> 2

     * @param s ein beliebiger String ungleich null

     */

    public int anzahlLeerzeichen(String s)

    {

        if (s.length()==0 && s==null)

        {

            throw new IllegalArgumentException("Sting darf nicht null sein!");

        }

        else

        {

            if (s.length()!=0)

            {

                if (s.charAt(0)==' ')

                {

                    leerZeichen++;

                }

                anzahlLeerzeichen(s.substring(1));

            }

        }

        return leerZeichen;

   


    /**

     * Liefert die umgedrehte Zeichenkette. Auswertung:

     *

     *   umgedreht("regal")

     * -> umgedreht("egal") + 'r'

     * -> (umgedreht("gal") + 'e') + 'r'

     * -> ((umgedreht("al") + 'g') + 'e') + 'r'

     * -> (((umgedreht("l") + 'a') + 'g') + 'e') + 'r'

     * -> (((          "l"  + 'a') + 'g') + 'e') + 'r'

     * -> ((           "la"        + 'g') + 'e') + 'r'

     * -> (            "lag"              + 'e') + 'r'

     * ->              "lage"                    + 'r'

     * ->              "lager"

     * @param s ein beliebiger String ungleich null

     */

    public String umgedreht(String s)

    {

        char charat;

        if (s.length()==0 && s==null)

        {

            throw new IllegalArgumentException("Sting darf nicht null sein!");

        }

        else

        {

            if (s.length()!=0)

            {

                charat = s.charAt(s.length()-1);

                umgedreht = umgedreht + charat;

                umgedreht(s.substring(0, s.length()-1));

            }

        }

        return umgedreht;

    }

}



[B]Das ist meine Test-Methoden:[/B]


[CODE]public class ProduzierenTest

{

    @Test

    public void testfak3()

    {

        Produzieren testee = new Produzieren();

        assertEquals(6, testee.fak(3));

    }


    @Test

    public void testfak4()

    {

        Produzieren testee = new Produzieren();

        assertEquals(24, testee.fak(4));

    }


    @Test

    public void testfak6()

    {

        Produzieren testee = new Produzieren();

        assertEquals(720, testee.fak(6));

    }


    @Test

    public void testmodulo14_3()

    {

        Produzieren testee = new Produzieren();

        assertEquals(2, testee.modulo(14, 3));

    }


    @Test

    public void testmodulo20_10()

    {

        Produzieren testee = new Produzieren();

        assertEquals(0, testee.modulo(20, 10));

    }


    @Test

    public void testmodulo21_4()

    {

        Produzieren testee = new Produzieren();

        assertEquals(1, testee.modulo(21, 4));

    }


    @Test

    public void testmodulom0()

    {

        Produzieren testee = new Produzieren();

        try

        {

            testee.modulo(21, 0);

        }

        catch (Exception ex)

        {

            return;

        }

        fail ("Es wurde keine Exception geforfen!");

    }


    @Test

    public void testEnthaeltVokalBrei()

    {

        Produzieren testee = new Produzieren();

        assertEquals(true, testee.enthaeltVokal("brei"));

    }


    @Test

    public void testEnthaeltVokalXyz()

    {

        Produzieren testee = new Produzieren();

        assertEquals(false, testee.enthaeltVokal("xyz"));

    }


    @Test

    public void testEnthaeltVokalVokal()

    {

        Produzieren testee = new Produzieren();

        assertEquals(true, testee.enthaeltVokal("Vokal"));

    }


    @Test

    public void testEnthaeltVokalNull()

    {

        Produzieren testee = new Produzieren();

        try

        {

            testee.istPalindrom(null);

        }

        catch (Exception ex)

        {

            return;

        }

        fail ("Es wurde keine Exception geforfen!");

    }


    @Test

    public void testIstPalindromAnna()

    {

        Produzieren testee = new Produzieren();

        assertEquals(true, testee.istPalindrom("anna"));

    }


    @Test

    public void testIstPalindromAsta()

    {

        Produzieren testee = new Produzieren();

        assertEquals(false, testee.istPalindrom("asta"));

    }


    @Test

    public void testIstPalindromAxa()

    {

        Produzieren testee = new Produzieren();

        assertEquals(true, testee.istPalindrom("axa"));

    }


    @Test

    public void testIstPalindromXyz()

    {

        Produzieren testee = new Produzieren();

        assertEquals(false, testee.istPalindrom("xyz"));

    }


    @Test

    public void testIstPalindromNull()

    {

        Produzieren testee = new Produzieren();

        try

        {

            testee.istPalindrom(null);

        }

        catch (Exception ex)

        {

            return;

        }

        fail ("Es wurde keine Exception geforfen!");

    }


    @Test

    public void testanzahlLeerzeichenA_BC_D()

    {

        Produzieren testee = new Produzieren();

        assertEquals(2, testee.anzahlLeerzeichen("a bc d"));

    }


    @Test

    public void testanzahlLeerzeichenABCD()

    {

        Produzieren testee = new Produzieren();

        assertEquals(0, testee.anzahlLeerzeichen("abcd"));

    }


    @Test

    public void testanzahlLeerzeichenA_B_C_D()

    {

        Produzieren testee = new Produzieren();

        assertEquals(3, testee.anzahlLeerzeichen("a b c d"));

    }


    @Test

    public void testanzahlLeerzeichenNull()

    {

        Produzieren testee = new Produzieren();

        try

        {

            testee.anzahlLeerzeichen(null);

        }

        catch (Exception ex)

        {

            return;

        }

        fail ("Es wurde keine Exception geforfen!");

    }


    @Test

    public void testUmgedrehtRegal()

    {

        Produzieren testee = new Produzieren();

        assertEquals("lager", testee.umgedreht("regal"));

    }


    @Test

    public void testUmgedrehtLager()

    {

        Produzieren testee = new Produzieren();

        assertEquals("regal", testee.umgedreht("lager"));

    }


    @Test

    public void testUmgedrehtAbcd()

    {

        Produzieren testee = new Produzieren();

        assertEquals("dcba", testee.umgedreht("abcd"));

    }


    @Test

    public void testUmgedrehtNull()

    {

        Produzieren testee = new Produzieren();

        try

        {

            testee.umgedreht(null);

        }

        catch (Exception ex)

        {

            return;

        }

        fail ("Es wurde keine Exception geforfen!");

    }

}[/CODE]



Oben