//An instance of this class provides a method to spell an integer in words //Public Interface // Mutator : void setNumber(int number) // Accessor: int getNumber() // Method : String spelling() public class IntSpeller { //Instance variable: any int value private int number; //Accessor public int getNumber() { return this.number; } //Mutator public void setNumber(int number) { this.number = number; } //Spelling Method // @return: the spelling (in words) of the instance variable public String spelling() { if (this.number == 0) return "Zero"; //"Negative" prefix if the number < 0, work with absolute value int number = this.number; String spelling = ""; if (number < 0) { number = -number; spelling = "Negative "; } //Extend with the spelling of the BILLIONS part (if not 0) int billions = number / 1000000000; if (billions != 0) spelling += threeDigitSpelling(billions) + " Billion "; //Extend with the spelling of the MILLIONS part (if not 0) int millions = number % 1000000000 / 1000000; if (millions != 0) spelling += threeDigitSpelling(millions) + " Million "; //Extend with the spelling of the THOUSANDS part (if not 0) int thousands = number % 1000000 / 1000; if (thousands != 0) spelling += threeDigitSpelling(thousands) + " Thousand "; //Extend with the spelling of the UNITS part (if not 0) int units = number % 1000; if (units != 0) spelling += threeDigitSpelling(units); return spelling.trim(); } //Helper Method // @param number: any positive 3-digit number // @return: the spelling (in words) of the parameter private static String threeDigitSpelling(int number) { if (number <= 0 || number > 999) //Validate the parameter throw new RuntimeException("Positive 3-digit number expected"); //Split the number into its hundreds digit and 2-digit remainder int hundreds = number / 100; //Hundreds digit int tensOnes = number % 100; //2-digit remainder (tens & ones) String spelling = ""; //Extend with the spelling of the HUNDREDS part (if not 0) if (hundreds > 0) spelling = onesWord(hundreds) + " Hundred "; //Extend with the spelling of the TENS & ONES part (if not 0) if (tensOnes != 0) spelling += twoDigitSpelling(tensOnes); return spelling.trim(); } //Helper Method // @param number: any positive 2-digit number // @return: the spelling (in words) of the parameter private static String twoDigitSpelling(int number) { if (number <= 0 || number > 99) //Validate the parameter throw new RuntimeException("Positive 2-digit number expected"); //Split the number into TENS and ONES digits int tens = number / 10; int ones = number % 10; String spelling = ""; if (tens == 1) //Spelling consists of a TEEN-word only spelling = teenWord(ones); else { if (tens > 1) //Spelling begins with a TENS-word spelling = tensWord(tens) + " "; if (ones != 0) //Spelling ends with a UNITS-word spelling = spelling + onesWord(ones); } return spelling.trim(); } //Helper Method // @param number: any single-digit integer // @return: the spelling of the parameter as a ONES/UNITS word private 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); } } //Helper Method // @param number: any single-digit integer // @return: the spelling of the parameter as a TEEN word private 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); } } //Helper Method // @param number: any single-digit integer // @return: the spelling of the parameter as a TENS word private 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); } } }