patternjavaMinor
Print and calculate French written numbers from 1 to 1000 using imperative Java
Viewed 0 times
frenchwrittennumbersjavaprintimperativecalculate1000usingand
Problem
I've written a little (imperative) java program that basically prints out all french written numbers from 1 to 1000. (I'm learning how to write them)
Based on the first numbers (1-16) and the tens and hundreds, It chains the words of the next number together and writes them into an one dimensional array.
The French counting system is a bit unusual as e.g. the numbers from 60 to 99 are based on twenty, instead of the normal, decimal 10.
That means, the next number after 69 ("soixante-neuf" => 60+9) is written out 60+10 ("soixante-dix") instead of just 70.
In the program, I've used basically the modulo (e.g. 70 % 20 => 10) operator to detect and print the particular digits.
My Questions are
=> what could i do to improve readability, the "length" of the code and make it in general more "smart"
The Code:
```
public class Main {
public static void main(String args[]) {
// i want to print also 1000
String[] french_num = new String[1001];
french_num[0] = "zero";
french_num[1] = "un";
french_num[2] = "deux";
french_num[3] = "trois";
french_num[4] = "quatre";
french_num[5] = "cinq";
french_num[6] = "six";
french_num[7] = "sept";
french_num[8] = "huit";
french_num[9] = "neuf";
french_num[10] = "dix";
french_num[11] = "onze";
french_num[12] = "douze";
french_num[13] = "treize";
french_num[14] = "quatorze";
french_num[15] = "quinze";
french_num[16] = "seize";
french_num[20] = "vingt";
french_num[30] = "trente";
french_num[40] = "quarante";
french_num[50] = "cinquante";
french_num[60] = "soixante";
Based on the first numbers (1-16) and the tens and hundreds, It chains the words of the next number together and writes them into an one dimensional array.
The French counting system is a bit unusual as e.g. the numbers from 60 to 99 are based on twenty, instead of the normal, decimal 10.
That means, the next number after 69 ("soixante-neuf" => 60+9) is written out 60+10 ("soixante-dix") instead of just 70.
In the program, I've used basically the modulo (e.g. 70 % 20 => 10) operator to detect and print the particular digits.
My Questions are
- how I could make the program generally "better"
=> what could i do to improve readability, the "length" of the code and make it in general more "smart"
- In particular how could I profit from object orientated programming in this particular example, as the normal OOP advantages like security, separated frontend/backend don't help me here very well. (at least in my opinion)
The Code:
```
public class Main {
public static void main(String args[]) {
// i want to print also 1000
String[] french_num = new String[1001];
french_num[0] = "zero";
french_num[1] = "un";
french_num[2] = "deux";
french_num[3] = "trois";
french_num[4] = "quatre";
french_num[5] = "cinq";
french_num[6] = "six";
french_num[7] = "sept";
french_num[8] = "huit";
french_num[9] = "neuf";
french_num[10] = "dix";
french_num[11] = "onze";
french_num[12] = "douze";
french_num[13] = "treize";
french_num[14] = "quatorze";
french_num[15] = "quinze";
french_num[16] = "seize";
french_num[20] = "vingt";
french_num[30] = "trente";
french_num[40] = "quarante";
french_num[50] = "cinquante";
french_num[60] = "soixante";
Solution
how i could profit from object orientated programming in this particular example, as the normal oop advantages like security, separated frontend/backend don't help me here very well. (at least in my opinion)
Start by organizing it better.
Currently you do everything in a single
This is not ideal.
Ideally you should follow the single responsibility principle,
and have program elements such as methods and classes and are in charge of doing one thing alone.
For example:
-
What is the purpose of the class? "Main" is not a purpose. Something like
-
Instead of one
it will make sense to have a method called
which takes a number and returns a
That way the logic you implemented will become more usable from other classes.
-
The
-
Move printing to its own method
With the above applied,
the class should end up with a structure more like this:
This is more reusable, with a clear purpose, and the responsibilities better separated.
how I could make the program generally "better" => what could i do to improve readability, the "length" of the code and make it in general more "smart"
Filling an array of a 1000 strings, doesn't sound so good.
You don't need to store all the calculated French numbers to print them.
You could print them as you calculate, reducing your memory footprint.
It will be enough to store only the irregular numbers,
and the ones that can be the building blocks to compose the others.
There are many similar question on this site about printing numbers in English,
you could learn much from those and adopt the logic similarly to the French language.
In Java the naming convention for variables is
Start by organizing it better.
Currently you do everything in a single
main method.This is not ideal.
Ideally you should follow the single responsibility principle,
and have program elements such as methods and classes and are in charge of doing one thing alone.
For example:
-
What is the purpose of the class? "Main" is not a purpose. Something like
FrenchCounting would be better.-
Instead of one
main method,it will make sense to have a method called
toFrench(int num),which takes a number and returns a
String.That way the logic you implemented will become more usable from other classes.
-
The
french_num array can be a (private) static field of the class. The toFrench method can reuse it, instead of recreating it every time.-
Move printing to its own method
With the above applied,
the class should end up with a structure more like this:
public class FrenchCounting {
private static final Map french;
static {
french = new HashMap<>();
french.put(0, "zero");
// ...
}
public static String toFrench(int num) {
// ...
}
public static void main(String args[]) {
for (int i = 0; i < 1000; ++i) {
System.out.println(toFrench(i) + " , " + i);
}
}
}This is more reusable, with a clear purpose, and the responsibilities better separated.
how I could make the program generally "better" => what could i do to improve readability, the "length" of the code and make it in general more "smart"
Filling an array of a 1000 strings, doesn't sound so good.
You don't need to store all the calculated French numbers to print them.
You could print them as you calculate, reducing your memory footprint.
It will be enough to store only the irregular numbers,
and the ones that can be the building blocks to compose the others.
There are many similar question on this site about printing numbers in English,
you could learn much from those and adopt the logic similarly to the French language.
In Java the naming convention for variables is
camelCase, not snake_case.Code Snippets
public class FrenchCounting {
private static final Map<Integer, String> french;
static {
french = new HashMap<>();
french.put(0, "zero");
// ...
}
public static String toFrench(int num) {
// ...
}
public static void main(String args[]) {
for (int i = 0; i < 1000; ++i) {
System.out.println(toFrench(i) + " , " + i);
}
}
}Context
StackExchange Code Review Q#107929, answer score: 5
Revisions (0)
No revisions yet.