Question
Say My Number You will produce a short, elegant, recursive algorithm that spells out any number between 1 and 2.1 billion. Allow the user to
Say My Number
You will produce a short, elegant, recursive algorithm that spells out any number between 1 and 2.1 billion.
Allow the user to input a number in the range from 1 to 2.1 billion (a positive 32-bit int), then spell out the number (e.g. 123456 would output "one hundred twenty three thousand four hundred fifty six"). Chap12_updated2say has a simple flow diagram that could represent the logic you use for this.
Use a recursive method "say(n)" where say(n) returns the string corresponding to the input integer n.
The beauty of recursion is that this can be done with just a few if statements and switch/case values (about 30 or so total), thanks to the way we read numbers (the number 123,123,123 is spoken the same as a single 123, but with a few "place" words - million, thousand, etc.).
Deliverable:
Saymynumber.java
I have the code. I just can't figure out how to get it to let me input my own number.
import java.text.DecimalFormat;
public class Saymynumber {
private static final String[] tensNames = { "", " ten", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety" };
private static final String[] numNames = { "", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen" };
private Saymynumber() {}
private static String convertLessThanOneThousand(int number) { String soFar;
if (number % 100 < 20){ soFar = numNames[number % 100]; number /= 100; } else { soFar = numNames[number % 10]; number /= 10;
soFar = tensNames[number % 10] + soFar; number /= 10; } if (number == 0) return soFar; return numNames[number] + " hundred" + soFar; }
public static String convert(long number) { // 0 to 999 999 999 999 if (number == 0) { return "zero"; }
String snumber = Long.toString(number);
// pad with "0" String mask = "000000000000"; DecimalFormat df = new DecimalFormat(mask); snumber = df.format(number);
// XXXnnnnnnnnn int billions = Integer.parseInt(snumber.substring(0,3)); // nnnXXXnnnnnn int millions = Integer.parseInt(snumber.substring(3,6)); // nnnnnnXXXnnn int hundredThousands = Integer.parseInt(snumber.substring(6,9)); // nnnnnnnnnXXX int thousands = Integer.parseInt(snumber.substring(9,12));
String tradBillions; switch (billions) { case 0: tradBillions = ""; break; case 1 : tradBillions = convertLessThanOneThousand(billions) + " billion "; break; default : tradBillions = convertLessThanOneThousand(billions) + " billion "; } String result = tradBillions;
String tradMillions; switch (millions) { case 0: tradMillions = ""; break; case 1 : tradMillions = convertLessThanOneThousand(millions) + " million "; break; default : tradMillions = convertLessThanOneThousand(millions) + " million "; } result = result + tradMillions;
String tradHundredThousands; switch (hundredThousands) { case 0: tradHundredThousands = ""; break; case 1 : tradHundredThousands = "one thousand "; break; default : tradHundredThousands = convertLessThanOneThousand(hundredThousands) + " thousand "; } result = result + tradHundredThousands;
String tradThousand; tradThousand = convertLessThanOneThousand(thousands); result = result + tradThousand;
// remove extra spaces! return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " "); }
/** * testing * @param args */ public static void main(String[] args) { System.out.println("*** " + Saymynumber.convert(0)); System.out.println("*** " + Saymynumber.convert(1)); System.out.println("*** " + Saymynumber.convert(16)); System.out.println("*** " + Saymynumber.convert(100)); System.out.println("*** " + Saymynumber.convert(118)); System.out.println("*** " + Saymynumber.convert(200)); System.out.println("*** " + Saymynumber.convert(219)); System.out.println("*** " + Saymynumber.convert(800)); System.out.println("*** " + Saymynumber.convert(801)); System.out.println("*** " + Saymynumber.convert(1316)); System.out.println("*** " + Saymynumber.convert(1000000)); System.out.println("*** " + Saymynumber.convert(2000000)); System.out.println("*** " + Saymynumber.convert(3000200)); System.out.println("*** " + Saymynumber.convert(700000)); System.out.println("*** " + Saymynumber.convert(9000000)); System.out.println("*** " + Saymynumber.convert(9001000)); System.out.println("*** " + Saymynumber.convert(123456789)); System.out.println("*** " + Saymynumber.convert(2147483647)); System.out.println("*** " + Saymynumber.convert(3000000010L)); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started