cs046 ยป

Shorter Code for Calculating Plural Forms

This is a shorter version of the getPluralForm() method in the Word class from this video: https://www.udacity.com/course/viewer#!/c-cs046/l-219235598/e-218845815/m-218845818

How does it compare with the shortest version you came up with? Which do you think is easiest to read?

    public String getPluralForm()
    {
        int last = letters.length() - 1; //index of last letter
        if(isConsonant(last-1) && is(last,"y"))
           {
               return letters.substring(0,last)+"ies";
           }
        // If the word ends in y preceded by a vowel, you just add an s.
        // You add an es when a word ends in o, or s, or sh, or ch.
        else if (is(last,"o") 
                 || is(last,"s")
                 || (is(last,"h") && is(last-1,"s"))
                 || (is(last,"h") && is(last-1,"c"))               
                )
        {return letters+"es";}
        else {return letters+"s";}
    }

The longer answer given in the video was

    public String getPluralForm()
    {
        int last = letters.length() -1; //index of last letter
        if(is(last, "o") || is(last, "s"))
        {
            return letters + "es";
        }
        else if (is(last, "y"))
        {
            if (isConsonant(letters.length() -2))
            {
                return letters.substring(0, last) + "ies";
            }
            else
            {
                return letters + "s";
            }
        }
        else if (is(last, "h"))
        {
            if (is(letters.length() - 2, "s") || (is(letters.length() - 2, "c")))
            {
                return letters + "es";
            }
            else
            {
                return letters + "s";
            }
        }
        else
        {
            return letters + "s";
        }

    }