Created
May 23, 2017 18:06
-
-
Save armaandhir/1d7f2c6bf5fa63cba5439629b8e3b8f1 to your computer and use it in GitHub Desktop.
Prints the textual representation of integer in English.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The program takes 2 parameters for the main method: | |
* - first parameter is the number to be converted | |
* - second parameter is either 1 or 2. 1 represents question 1 and 2 represents question 2 | |
* | |
* @author armaan | |
* | |
*/ | |
public class NumberWords { | |
/** | |
* The output should be this file which, when run, will output your answers to the following problems. | |
* | |
* 1. When passed a single integer between 0 and 10 the program should print out the textual representation | |
* of that integer in English. E.g. given the integer 4 your program should print 'four'. | |
* | |
* 2. Extend your solution in Q1 to support all integers between 0 and 99999. Consider all cases necessary; | |
* for example | |
* - '10001' should print 'Ten Thousand and One'. | |
* - '6415' should print 'Six Thousand Four Hundred and Fifteen'. | |
* - '93467' should print 'Ninety Three Thousand Four Hundred and Sixty Seven'. | |
* | |
* Program should compile and will be passed each of the integers below as the first entry in the args array passed to the main method. | |
* It should print out each one in the form '1234 : One Thousand Two Hundred and Thirty Four'. | |
* | |
* Question 1 Test Numbers: 0, 2, 4, 6, 8, 10 | |
* Question 2 Test Numbers: 0, 2, 11, 17, 23, 100, 114, 756, 1010, 5500, 10000, 10001, 14389, 99999 | |
* | |
*/ | |
private static final String[] numberNames = { | |
"", | |
"one", | |
"two", | |
"three", | |
"four", | |
"five", | |
"six", | |
"seven", | |
"eight", | |
"nine", | |
"ten", | |
"eleven", | |
"twelve", | |
"thirteen", | |
"fourteen", | |
"fifteen", | |
"sixteen", | |
"seventeen", | |
"eighteen", | |
"nineteen" | |
}; | |
private static final String[] tenNames = { | |
"", | |
"ten", | |
"twenty", | |
"thirty", | |
"forty", | |
"fifty", | |
"sixty", | |
"seventy", | |
"eighty", | |
"ninety" | |
}; | |
/** | |
* This method handles the solution to Question 1. | |
* @param number number to be converted to it's word representation. | |
* @return String converted word string from a number | |
*/ | |
public String convert1To10(int number) { | |
if(number < 0 || number > 10) { | |
return "Number entered is out of bounds"; | |
} | |
else if(number == 0){ | |
return "Zero"; | |
} | |
else { | |
String input = numberNames[number]; | |
return input.substring(0, 1).toUpperCase() + input.substring(1); | |
} | |
} | |
/** | |
* Private utility function that handles numbers upto 99 | |
* @param number | |
* @return String string representation of the number | |
*/ | |
private String convertUpto99(int number){ | |
if(number <= 0 || number > 99) { | |
return "Number entered is out of bounds"; | |
} | |
else if(number < 20) { | |
return numberNames[number]; | |
} | |
else { | |
return tenNames[number/10] + " " + numberNames[number%10]; | |
} | |
} | |
/** | |
* Private utility function that handles numbers upto 999 | |
* @param number | |
* @return String string representation of the number | |
*/ | |
private String convertUpto999(int number) { | |
StringBuilder word = new StringBuilder(""); | |
if(number <= 0 || number > 999) { | |
return "Number entered is out of bounds"; | |
} | |
else if(number < 100) { | |
return convertUpto99(number); | |
} | |
else{ | |
word.append(numberNames[number/100]); | |
number %= 100; | |
if(number == 0){ | |
word.append(" hundred"); | |
return word.toString(); | |
} | |
word.append(" hundred "); | |
return word.append("and " + convertUpto99(number).toString()).toString(); | |
} | |
} | |
/** | |
* Private utility function that handles numbers upto 99999 | |
* @param number | |
* @return String string representation of the number | |
*/ | |
private String convertUpto99999(int number){ | |
StringBuilder word = new StringBuilder(""); | |
if(number <= 0 || number > 99999) { | |
return "Number entered is out of bounds"; | |
} | |
else if(number < 1000) { | |
return convertUpto999(number); | |
} | |
else{ | |
word.append(convertUpto99(number/1000)); | |
number %= 1000; | |
if(number == 0){ | |
word.append(" thousand"); | |
return word.toString(); | |
} | |
else if(number < 100 || number/100 < 9) { | |
word.append(" thousand and "); | |
} | |
else{ | |
word.append(" thousand "); | |
} | |
return word.append(convertUpto999(number).toString()).toString(); | |
} | |
} | |
/** | |
* Represents the solution to question 2. | |
* @param number number to be converted to it's word representation. | |
* @return String converted word string from a number | |
*/ | |
public String convert(int number) { | |
if(number < 0 || number > 99999) { | |
return "Number entered is out of bounds"; | |
} | |
else if (number == 0){ | |
return "Zero"; | |
} | |
else{ | |
String input = convertUpto99999(number); | |
return input.substring(0, 1).toUpperCase() + input.substring(1); | |
} | |
} | |
public static void main(String[] args) { | |
NumberWords obj = new NumberWords(); | |
int input = 0; | |
String param = null; | |
try{ | |
input = Integer.parseInt(args[0]); | |
param = args[1]; | |
} | |
catch(Exception ex){ | |
System.err.println(ex); | |
} | |
if(param.equals("1")){ | |
System.out.println(Integer.toString(input) + ": " + obj.convert1To10(input)); | |
} | |
else if(param.equals("2")){ | |
System.out.println(Integer.toString(input) + ": " + obj.convert(input)); | |
} | |
else{ | |
System.out.println("Check arguments. 2nd argument needs to be 1 or 2."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment