Pluralization
A Programming Pattern |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
There is 1 poodle available for adoption.
There are 3 poodles available for adoption.
/** * A method used to choose between two forms. * * @param n The number of items * @param singular The singular form * @param plural The plural form * @return The singular or plural form */ public static String form(int n, String singular, String plural) { if (n > 1) return plural; else return singular; }
/** * A method used to get the singular or plural * form of a regular noun. * * @param n The number of items * @param noun The regular noun * @return "s" or "" */ public static String regular(int n, String noun) { return noun + form(n, "", "s"); }
result = "There " + Pluralize.is(n) + " " + n + " " + Pluralize.regular(n, "poodle") + " available for adoption.";
result = "There " + Pluralize.is(n) + " " + n + " " + Pluralize.form(n, "mouse", "mice") + " available for adoption.";