// Project: UnitTests // Module: amt2words // Source code file Amt2Words.java // The amt2Words method in the Amt2Words class inputs // a double amount < 100.00 and convert it to // words suitable for using as the amount on a check. // The Amt2WordsTest class provides the test1 method // with unit tests for the Amt2Words.amt2Words method. public class Amt2Words { // Traditional test file. // Unit tests are in the Amt2WordsTest.java file. public static void main(String[] args) { System.out.println(amtToWords(63.45)); System.out.println(amtToWords(18.06)); System.out.println(amtToWords(0.97)); } public static String amtToWords(double amt) { // Extract pieces of input. int dollars = (int) amt; int tens = dollars / 10; int ones = dollars % 10; int cents = (int) Math.round(100 * (amt - dollars)); // Definitions of words for digits. String[ ] onesWords = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; String[ ] teensWords = {"", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; String[ ] tensWords = {"", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; // Declare output variable. String output = ""; if (dollars <= 9) { output = onesWords[ones]; } else if (dollars <= 19) { output = teensWords[ones]; } else if (dollars <= 99) { output = tensWords[tens] + " " + onesWords[ones]; } else { output = "Out of range"; } return output + " and " + cents + "/100"; } }