|
|
|
|
|
by vrotaru
1396 days ago
|
|
Even for something which is well defined up-front this can of dubious value.
Converting an positive integer less than 3000 is well-defined task.
Now if you try to write such a program using TDD what do you think will end up with? Try it. Write a test for 1, and an implementation which passes that test then for 2, and so on. Bellow is something written without any TDD (in Java) private static String convert(int digit, String one, String half, String ten) {
switch(digit) {
case 0: return "";
case 1: return one;
case 2: return one + one;
case 3: return one + one + one;
case 4: return one + half;
case 5: return half;
case 6: return half + one;
case 7: return half + one + one;
case 8: return half + one + one + one;
case 9: return one + ten;
default:
throw new IllegalArgumentException("Digit out of range 0-9: " + digit);
}
}
public static String convert(int n) {
if (n > 3000) {
throw new IllegalArgumentException("Number out of range 0-3000: " + n);
}
return convert(n / 1000, "M", "", "") +
convert((n / 100) % 10, "C", "D", "M") +
convert((n / 10) % 10, "X", "L", "C") +
convert(n % 10, "I", "V", "X");
}
|
|