patternjavaMinor
Counting from 0 to a number and back to zero, and space-padding the result to be a given length
Viewed 0 times
paddingresultnumbercountingspacethelengthbackandfrom
Problem
I completed a task: The objective was to create a method with 2 arguments: a number to count to, and how many char/numbers in the resulting string.
Result: Let's say that "number" is 5, so it will count: 01234543210 BUT it will also add spaces to the sides to complete the 2nd requirement "lenght", so if long is 15, the result would be " 01234543210 " with 2 spaces to each side (always same quantity of space each side) So i wrote this piece of code, but other coder told me that he used half the lines I did.
There were many assumptions in the code, like the fact that lenght is > than number or that if number is an odd number, lenght is odd too.
So my question would be, can this code be reduced (refactored?) (I just have one month of experience in Java, so be hard but understanding.)
Result: Let's say that "number" is 5, so it will count: 01234543210 BUT it will also add spaces to the sides to complete the 2nd requirement "lenght", so if long is 15, the result would be " 01234543210 " with 2 spaces to each side (always same quantity of space each side) So i wrote this piece of code, but other coder told me that he used half the lines I did.
There were many assumptions in the code, like the fact that lenght is > than number or that if number is an odd number, lenght is odd too.
So my question would be, can this code be reduced (refactored?) (I just have one month of experience in Java, so be hard but understanding.)
public static void counter(int number, int lenght) {
int numberFull=(number*2)+1;
int spaceFull=lenght-numberFull;
int space=spaceFull/2;
for (int a=1;a=0;y--)
{System.out.print(y);}
for (int b=1;b<=space;++b)
{System.out.print(" ");}
}Solution
You can simplify the logic that prints the numbers going from 0 to
They are:
As a pre-requisite, let's consider the (simpler) output
We want to model that with a single variable
number anc back to 0.They are:
number - Math.abs(number - x). Let's explain why. With the number 5, the wanted output is:01234543210As a pre-requisite, let's consider the (simpler) output
54321012345We want to model that with a single variable
x going from 0 to 2 * number. Clearly, there are 2 paths: one decreasing and one increasing. 5 4 3 2 1 0 1 2 3 4 5
value of x 0 1 2 3 4 5 6 7 8 9 10
value of 5 - x 5 4 3 2 1 0 -1 -2 -3 -4 -5
value of abs(5 - x) 5 4 3 2 1 0 1 2 3 4 5
Now that we have that, it is clear that our real wanted output is simply 5 minus that one, i.e. number - Math.abs(number - x).
Putting this into code:
public static void counter(int number, int length) {
int space = (length - number * 2 + 1) / 2;
for (int a = 0; a < space; a++) {
System.out.print(" ");
}
for (int x = 0; x <= number * 2; x++) {
System.out.print(number - Math.abs(number - x));
}
for (int b = 0; b < space; b++) {
System.out.print(" ");
}
}
Other comments:
- Add spaces before each operator, it adds to readability.
- Don't use a mixed form of curly braces and single line statement. Either add curly braces everywhere and then have a proper line break or make it a single line without them.
- There is no need to store into local variables when you only use it once. In this case having
numberFull and spaceFull don't really add to clarity and it is much simpler to have int space = (length - number * 2 + 1) / 2;.
- There is no validation on the number and the count of spaces. You might want to add some.
- Instead of letting the
counter method print the String directly, it would be better to let it build the String and return it. The caller can then decide to print it or not. For that, take a look at the StringBuilder` class.Code Snippets
0123454321054321012345public static void counter(int number, int length) {
int space = (length - number * 2 + 1) / 2;
for (int a = 0; a < space; a++) {
System.out.print(" ");
}
for (int x = 0; x <= number * 2; x++) {
System.out.print(number - Math.abs(number - x));
}
for (int b = 0; b < space; b++) {
System.out.print(" ");
}
}Context
StackExchange Code Review Q#127038, answer score: 4
Revisions (0)
No revisions yet.