• Post Reply Bookmark Topic Watch Topic
  • New Topic

Numbers to Word coversion  RSS feed

 
sureshu kumary
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-----------------------

Take a number and give the equivalent number in words.
The following are specifications by example (for English output) and the code must satisfy these specifications:

Given 1 output one
Given 21 output twenty one
Given 105 output one hundred and five
Given 110105 output one hundred and ten thousand one hundred and five
Given 1000099 output one million and ninety nine
Given 1000100 output one million one hundred
Given 56945781 output fifty six million nine hundred and forty five thousand seven hundred and eighty one

etc..
up to 999,999,999 without using a tokenizer, or any external libraries (junit allowed)

By default the output language should be English, but the application should enable support for other language output as well.

The design should also take into account that a requirement for the next release is to also support outputting numbers as:
Given 1 output first
Given 2 output second
Given 21 output twenty first
Given 105 output one hundred and fifth
etc

The code to solve this problem should be implemented in Java and engineered to production specification using Object Oriented techniques (e.g. cohesion, loose coupling) and should include unit tests.

All answers should implement the following interface at the highest level:

public interface WordifiedNumber {
String toWords(int number);
}

// these are the minimum tests that must pass, the expected output must not be edited (note the use of the word "and" when outputting the numbers)
@Test
public void wordifiedWordReferenceTest() throws Exception {
WordifiedNumber wordifiedNumber = new WordifiedNumberImpl();

assertEquals("one", wordifiedNumber.toWords(1));
assertEquals("twenty one", wordifiedNumber.toWords(21));
assertEquals("one hundred and five", wordifiedNumber.toWords(105));
assertEquals("one thousand and five", wordifiedNumber.toWords(1005));
assertEquals("one hundred and ten thousand one hundred and five", wordifiedNumber.toWords(110105));
assertEquals("one million and ninety nine", wordifiedNumber.toWords(1000099));
assertEquals("one million one hundred", wordifiedNumber.toWords(1000100));
assertEquals("fifty six million nine hundred and forty five thousand seven hundred and eighty one", wordifiedNumber.toWords(56945781));


}








-------------------
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. Sounds like an assignment you should be doing?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sureshu kumary wrote:Take a number and give the equivalent number in words...

OK, so: have you done it? Is there a question there?

One thing I will tell you for free - the last part of the requirement:
"By default the output language should be English, but the application should enable support for other language output as well."
is going to be very tough because of grammatical differences between languages. French, in particular, is an absolute swine because it has lots of irregularities in its groups of 10.

My advice: get it working in English, and only worry about that last bit if you have time. It's possible (in fact, highly likely) that all your tutor wants is that you call a stub method to prove that you could implement it for a different language if you had to.

HIH

Winston
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
French, in particular, is an absolute swine because it has lots of irregularities in its groups of 10.


Well in French there are remnants of a 20-based number system.
40, 50, 60 have their proper names but 70 is "60+10" and 80 is "4x20" and 90 is "4x20+10".
96 is "4x20+16" and 97 is "4x20+10+7", 16 having a proper name, and 17 being composed.
Belgian and Canadian French might show some differences however.

Actually the numbers 11 and 12 have their proper names in English (and also in German), and from 13 on are the numbers' names composed.
In my native tongue Hungarian but also in Russian 11 and 12 are already composed from 10 and the rest.

In German (and I think also in Dutch) 34 is told as "4 and 30".

Wifey likes very much the novels of Jane Austen. Also the films and film series based on them. She likes them so much, that she once gave me as present the English (that is, orginal) version of "Pride and Prejudice" as a book. I must admit to like this stuff too and I found in the novel the inverted usage of the numerals in the Gemran style like "four-and-thirty".
Pemberley

Naming numbers is a hard job, and even more so in several languages!

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:Actually the numbers 11 and 12 have their proper names in English (and also in German), and from 13 on are the numbers' names composed.

And in many Romance languages (including French) that goes to 16. And in Polish (not sure about other Slavic languages) the whole darn lot are consistent; the tens aren't though (different suffixes for 20, 30/40, 50-90). Swings and roundabouts.

Winston
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:[
(including French) that goes to 16.


Oui, from 17 to 19 they are composed n the way 10+x, but the whole system still conveys a feeling of being 20-based. Actually funny.

Salut,

Iván



 
sureshu kumary
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done it thanks for the help
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!