4
\$\begingroup\$

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.)

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<=space;++a)
        {System.out.print(" ");}
    for (int x=0;x<=number;x++)
        {System.out.print(x);}
    for (int y=number-1;y>=0;y--)
        {System.out.print(y);}
    for (int b=1;b<=space;++b)
        {System.out.print(" ");}
}
\$\endgroup\$
  • \$\begingroup\$ Can number ever be greater than 9? \$\endgroup\$ – 200_success Apr 29 '16 at 10:38
  • \$\begingroup\$ "You can assume the method parameter length is a positive odd integer and is always greater than or equal the length of the digit sequence to be printed. You can also assume that the method parameter number can only take values from 0 to 9." \$\endgroup\$ – Nooblhu Apr 29 '16 at 10:41
4
\$\begingroup\$

You can simplify the logic that prints the numbers going from 0 to number anc back to 0.

They are: number - Math.abs(number - x). Let's explain why. With the number 5, the wanted output is:

01234543210

As a pre-requisite, let's consider the (simpler) output

54321012345

We 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  <-- output

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.
\$\endgroup\$
3
\$\begingroup\$

Typos

lenght should be length. :)

Braces and whitespacing

Please be consistent in your braces style, even for one-liners. As such,

for (int a=1;a<=space;++a)
    {System.out.print(" ");}

Should look like this:

// note the extra whitespaces
for (int a=1; a<=space; ++a) {
    System.out.print(" ");
}

for-iteration conditions

Somewhat along the same lines as the previous point, you can consider counting up in your whitespace for-loops using the same style as you do for the numbers:

for (int a=0; a<space; a++) {
    System.out.print(" ");
}

Calculating padding

Storing your results as an int means that you are also doing integer divisions for (length - numberFull) / 2. You will need to add 1 more whitespace (start? end?) to fulfill your requirements, or can it be assumed that length will always be an odd number?

Validation

You also do not validate whether \$number < 0\$, it'll be nice if you have done so. :)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.