/** * Has a couple of static methods that might be useful * * 1. a method that returns the name of an int month E.g. if month = 3, then * GoodyBag.getMonthName(month) will return "March" * * 2. a method that returns the ordinal of an int E.g. if number = 37, then * GoodyBag.getOrdinal(number) will return "37th" */ public class GoodyBag { /** * Returns the name of an int month * * @param monthNumber the number of the month. E.g. March is 3 * @return the name of the month */ public static String getMonthName(int monthNumber) { final String[] MONTHS = { "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; // MONTH[0] has null string return MONTHS[monthNumber]; } /** * Returns the ordinal of a number (i.e., 1st, 2nd, 3rd, 4th, etc) * * @param number the number to get the ordinal of * @return the ordinal of number */ public static String getOrdinal(int number) { String ordinal; // the ordinal of number (e.g., 37th) // First, check for special cases - numbers where the last // 2 digits are 11, 12, or 13 // Those are the only numbers ending in 1, 2, or 3 where the suffix // is "th" E.g. 11th, 12th, 13th, vs 21st, 22nd, 43rd // get last two digits int lastTwo = number % 100; if (lastTwo == 11 || lastTwo == 12 || lastTwo == 13) { ordinal = number + "th"; // e.g. 11th, 212th, 1013th } else // last 2 digits not 11, 12, or 13 { // get last digit int lastDigit = number % 10; if (lastDigit == 1) { ordinal = number + "st"; // e.g. 1st, 21st } else if (lastDigit == 2) { ordinal = number + "nd"; // e.g. 2nd, 32nd } else if (lastDigit == 3) { ordinal = number + "rd"; // e.g. 3rd, 43rd } else // number ends in 0 or 4 thru 9 { ordinal = number + "th"; // e.g. 0th, 14th, 37th, 99th } } return ordinal; } }