Lab 4 Comp 170-201 -- Dordal/Nabicht -- Wed, Sept 18, 2002 Final version due Fri Sept 27 Two parts Part A You are to write a program that takes a number entered into your program, and then prints out the number in words: int n = 1234; The number is one thousand two hundred thirty four You should be able to handle integers between 0 and 9,999, inclusive. If a number is too big, or negative, just say so and quit. It is permissible to treat the number as a string: String n = "1234", if this helps, although to get the four digits of an int you can use: ones = n % 10; n = n/10; tens = n % 10; n = n/10; hundreds=n%10; n = n/10; thousands=n; I am providing you with two *functions*, the first to convert a single digit to words, and the second to convert a (tens) digit to the appropriate twenty/thirty/forty/etc word. In each case, if n is "out of range", the empty string is returned; note that the digit function actually works for n<=19. The easiest way to get these functions into your program is to go to my web page, www.cs.luc.edu/~pld, follow the link to 170, click on lab4, and then use copy/paste to transfer from this file to yours. public static String digit (int n) { switch(n) { 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"; case 10: return "ten"; case 11: return "eleven"; case 12: return "twelve"; case 13: return "thirteen"; case 14: return "fourteen"; case 15: return "fifteen"; case 16: return "sixteen"; case 17: return "seventeen"; case 18: return "eighteen"; case 19: return "nineteen"; default: return ""; } } public static String digit10(int n) { switch(n) { 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: return ""; } } (Note that I don't need "break" in the above after each case, because return has the effect of exiting the function, and thus in particular it exits the switch() statement.) With these, the strategy is straightforward if detailed: 1. If the thousands digit is not zero, use digit() to print it, followed by "thousand" 2. If the hundreds digit is not zero, use digit() to print it, followed by "hundred" 3. If the tens digit is >=2, print it using digit10(), then the ones digit If the tens digit is 1, print the rest of the number with digit(). If the tens digit is 0, just print the ones digit, optonally preceded by "and" Instead of printing a piece at a time, it is a better (more useful generally) strategy to create a single string representing the number in words, and print it all at once at the end. Part B =================================================================== You saw in the Kakutani program in Lab 3 that the sequence generated there: if (N%2 == 0) N = N/2; else N = 3*N + 1; eventually reached 1, although the max value reached in the sequence might be large and the length of the sequence might be long. For example, starting with n=54, the sequence has 112 entries, and the largest is 9232. Starting with n=7, the sequence is: 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 The length here is 16 steps; the largest (max) value that appears is 52. Run the Kakutani loop for all N between 2 and 1000, and report: * which N gives the sequence with the longest length * which N has the largest max value in its sequence For each N, you are also to print out both the max value and the sequence length.