DEV Community

drew
drew

Posted on

A Coding Exercise: What I learned

I recently finished a Java coding exercise that took me the better part of two weeks to complete, coding every single day. I don't know if it should have taken that long, but for me it was difficult. I rewrote it multiple times trying to make it meet all the criteria.

For those of you who don't know, Udemy has a system where instructors can add actual coding exercises to their courses. I've talked about this a little bit before, mostly when I've been somewhat critical of courses for not having exercises. According to Udemy:

"Instructors can create coding exercises in various languages, including C++, C#, HTML & Javascript, Java, Python 3, Ruby, Swift 3, and PHP."

If you're not sure if a course has them or not you can use the filters to search for courses that do:

Alt Text

You can also see it on the course page close to where the buy button is:

Alt Text

See where it says 250 coding exercises?

I look for courses now that have them, because consistent writing of code is the absolute best way for a beginner to really internalize what you're learning.

Back to this particular exercise

Most exercises didn't take me more than one to two hours, with a few taking maybe 4 total. This one took me close to two weeks. It involved 3 methods, two doing their own thing, and a third main one using them to do something.

The exercise was called Number to Words.Now, keep in mind that the exercise only covers what I've learned up til now. We haven't covered arrays, or a lot of other things that probably would have made it easier. essentially I had to write a method that would take a given number (294,136, 1234 etc) and print out a number for each digit (two, nine, four for example). Sounds easy right?

It would have been, except there were some cases it wouldn't cover for. for example, The initial method actually prints them backwards. Instead of two nine four, it would print four nine two. A second method was needed to reverse the number.

Which was fine, except it caused a bug in certain cases, like the number 100 would only print "one". One more method was needed. That method would essentially get a count of a given number and return the number of digits. Then I had to call both those two methods within the main method and make it print all the use cases without a bug.

I'll post the code here for anyone interested in seeing it. Again, it's probably not pretty, and there's definitely easier ways to solve it. This required me to use only what I've learned in the course so far, so that's why it's written the way it is.

The Code:


    //Takes a given number and reverses it
    public static int reverse(int num) { 

        int reversed = 0;
        while(num != 0) {
            int digit = num % 10;
            reversed = (reversed * 10) + digit;
            num /= 10;
        }
        return reversed;
    }

    // gets the amount of digits in a number
    public static int getDigitCount (int number) {

        if (number < 0) {
            return -1;
        }
        return String.valueOf(number).length();
    }


    // prints a number as words
    public static void numberToWords (int number) {
        if (number < 0) {
            System.out.println("Invalid value;");
        } else if(number == 0) {
            System.out.println("Zero");
        }

        // sets up the two counters to be compared
        int reverseNumber = reverse(number);
        int reversedCount = getDigitCount(reverseNumber);
        int originalCount = getDigitCount(number);


        while(reverseNumber > 0) {

            int separateDigit;
            separateDigit = reverseNumber % 10;
            reverseNumber  /= 10;

            switch (separateDigit) {
                case 0:
                    System.out.println("Zero");
                    break;
                case 1:
                    System.out.println("One");
                    break;
                case 2:
                    System.out.println("Two");
                    break;
                case 3:
                    System.out.println("Three");
                    break;
                case 4:
                    System.out.println("Four");
                    break;
                case 5:
                    System.out.println("Five");
                    break;
                case 6:
                    System.out.println("Six");
                    break;
                case 7:
                    System.out.println("Seven");
                    break;
                case 8:
                    System.out.println("Eight");
                    break;
                case 9:
                    System.out.println("Nine");
                    break;

            }
        }
        // fixes a bug where only a "one" is printed in numbers like 100
        // by comparing the count of the original number by the count
        // of the reversed one, and printing zero if they don't match

        while (originalCount != reversedCount) {
            System.out.println("Zero");
            reversedCount++;
        }
    }

Enter fullscreen mode Exit fullscreen mode

What I learned

This was by far the hardest exercise I've ever written. But what did I actually learn in the process?

here's some bullet points in no particular order:

  • Scope is really important. Where you put your code matters. The final bug I had while trying to get this to work was placement of my final while loop. You see where it is after the two brackets? originally I had it sitting inside the first while loop, instead of outside.
  • Save all your code. If you've written snippets of code for other exercises you might find something in them that will help you later.
  • If you have to write big code, don't try to write it all at once. Get the very smallest part working, then move to the next. Same with writing different methods. Use printing (or in the case of Java, println() ) to console stuff to see what it's doing.
  • Back up your code to Github or somewhere. I started doing this after the first couple rewrites. I actually had most of it working then I broke it and had written so much I couldn't just undo it all, lol. Make occasional backups especially if its working so you can revert to it if you break something important and can't fix it.
  • Comment your code where needed. It will help especially if you wrote something complicated and you need to remember what it did. </>

    That's pretty much it. I learned a lot on this exercise. I never expected it to take that long, but I'm ultimately glad I stuck it out and completed it.

    ....does this make me a programmer? :D

  • Top comments (0)