//The NumberSpelling class provides static methods to spell integers // // SINGLE DIGIT NATURAL INTEGERS // @param digit: any single-digit integer 0 .. 9 // String onesWord(int digit) : Zero, One, Two. .. Nine // String teenWord(int digit) : Ten, Eleven, Twelve, .. Nineteen // String tensWord(int digit) : Zero, Ten, Twenty, Ninety // // 2-DIGIT or 3-DIGIT NATURAL INTEGERS // @param number: any natural number of no more than 2/3 digits // String twoDigitSpelling(int number) : Zero .. Ninety Nine // String threeDigitSpelling(int number): Zero .. Nine Hundred Ninety Nine // // Any valid Java int // @param number: any positive or negative Java int // String spelling(int number) // public class NumberSpelling { //Returns the spelling of a ones word: Zero, One, Two, .. Ten // @param digit : a single digit integer 0 .. 9 public static String onesWord(int digit) { switch ( digit ) { case 0 : return "Zero"; case 1 : return "One"; case 2 : return "Two"; case 3 : return "Three"; case 4 : return "Four"; case 5 : return "Five"; case 6 : return "Six"; case 7 : return "Seven"; case 8 : return "Eight"; case 9 : return "Nine"; default: throw new RuntimeException("Invalid digit " + digit); } } //Returns the spelling of a teen word: Ten, Eleven, .. Nineteen // @param digit : a single digit integer 0 .. 9 public static String teenWord(int digit) { switch ( digit ) { case 0 : return "Ten"; case 1 : return "Eleven"; case 2 : return "Twelve"; case 3 : return "Thirteen"; case 4 : return "Fourteen"; case 5 : return "Fifteen"; case 6 : return "Sixteen"; case 7 : return "Seventeen"; case 8 : return "Eighteen"; case 9 : return "Nineteen"; default: throw new RuntimeException("Invalid digit " + digit); } } //Returns the spelling of a tens word: Zero, Ten, Twenty, .. Ninety // @param digit : a single digit integer 0 .. 9 public static String tensWord(int digit) { switch ( digit ) { case 0 : return "Zero"; case 1 : return "Ten"; case 2 : return "Twenty"; case 3 : return "Thirty"; case 4 : return "Forty"; case 5 : return "Fifty"; case 6 : return "Sixty"; case 7 : return "Seventy"; case 8 : return "Eighty"; case 9 : return "Ninety"; default: throw new RuntimeException("Invalid digit " + digit); } } //Return the spelling of any 2-digit natural number // @param number: an integer in the range 0 .. 99 public static String twoDigitSpelling(int number) { if (number < 0 || number > 99) throw new RuntimeException(number + " is NOT a 2-Digit Natural Number"); if (number == 0) return "Zero"; int tens = number / 10; int ones = number % 10; if (tens == 1) return teenWord(ones); String spelling = ""; if (tens > 1) spelling = spelling + tensWord(tens) + " "; if (ones > 0) spelling = spelling + onesWord(ones); return spelling.trim(); } //Returns the spelling of any 3-digit natural number // @param number: an integer in the range 0 .. 999 public static String threeDigitSpelling(int number) { if (number < 0 || number > 999) throw new RuntimeException(number + " is NOT a 3-Digit Natural Number"); if (number == 0) return "Zero"; String spelling = ""; //Begin with the words of the 100's digit, if any int hundreds = number / 100; if (hundreds != 0) spelling = onesWord(hundreds) + " Hundred "; //Append the spelling of the 2-digit remainder, if any int remainder = number % 100; if (remainder != 0) spelling = spelling + twoDigitSpelling(remainder); return spelling.trim(); } //Returns the spelling, in words, of any Java int public static String spelling(int number) { if (number == 0) return "Zero"; if (number < 0) return "Negative " + spelling(-number); String words = spelling(number / 1000000000, "Billion ") + spelling(number % 1000000000 / 1000000, "Million ") + spelling(number % 1000000 / 1000, "Thousand ") + spelling(number % 1000, ""); return words.trim(); } //Helper Method: Spell a 3-digit part of a number with it's part suffix //@param triple: a 3-digit integer //@param suffix: "Billion", "Million", "Thousand" or "" private static String spelling(int triple, String suffix) { if (triple == 0) return ""; return threeDigitSpelling(triple) + " " + suffix; } }