Heart containing Coding Chica Java 101

Adding Shorts, Too! – Whoa, that’s Overloading!

TIP: References Quick List

Additional Info:

There are several different types of integer values, what if we need to add those, too? All we have to do is change the input parameter types and Java will pick the correct one, even though they’re both defined as methods in the same class. This is called overloading. Let’s create an overloaded add method using test-driven-development steps.

Unit Test Updates

Let’s start by updating the codingchica.java101.AppTest with our new method’s expectations.

/**
 * Unit tests for the add method.
 *
 * @see App#add(short, short)
 */
@Nested
class AddShortsTest {

    @ParameterizedTest
    @CsvSource({
            // Happy Path - Positive
            "1,2,3",
            // Happy Path - Neutral
            "0,0,0",
            // Happy Path - Negative
            "-1,-1,-2",
            // Edge Case - Upper Bound short
            "1,32766,32767",
            "32766,1,32767",
            // Edge Case - Lower Bound short
            "-1,-32767,-32768",
            "-32767,-1,-32768",
            // Edge Case - Beyond Upper Bound - short
            "1,32767,32768",
            "32767,2,32769",
            // Edge Case - Beyond Lower Bound - short
            "-1,-32768,-32769",
            "-32768,-2,-32770",
            // Edge Case - Minimum short values
            "-32768,-32768,-65536",
            // Edge Case - Maximum short values
            "32767,32767,65534",
    })
    void add_whenInvoked_thenReturnsExpectedResult(short value1, short value2, int expectedResult) {
        // Setup

        // Execution
        int actualResult = App.add(value1, value2);

        // Validation
        assertEquals(expectedResult, actualResult, () -> String.format("%s+%s=%s", value1, value2, expectedResult));
    }
}

The basic happy-path tests are the same, but the edges of the lower and upper bounds have changed, so our edge case tests (the spots where behavior may change) also change.

We also return an int in this case, so we can still add 2 maximum or minimum short values successfully.

Application Updates – Method Signature

Now, let’s add a second copy of the add method to codingchica.java101.App that overloads the existing one for bytes. However, we’re only going to add enough of the code to make the unit tests compile (and fail).

/**
 * Add two short values.
 *
 * @param shortValue1 The first value to add.
 * @param shortValue2 The second value to add.
 * @return An int value representing the two values added together.
 */
public static int add(short shortValue1, short shortValue2) {
    return 0;
}

This time, our input parameters come in as short values, which is how Java will know which version of the add method to invoke. Just like before, we’re returning the larger int type value.

Run Test For Failure

If we run the Maven build, we can confirm that the new unit test is picked up by the build and preview any failure messages.

[ERROR] Failures:
[ERROR]   AppTest$AddShortsTest.add_whenInvoked_thenReturnsExpectedResult:121 -1+-32768=-32769 ==> expected: <-32769> but was: <0>
[ERROR]   AppTest$AddShortsTest.add_whenInvoked_thenReturnsExpectedResult:121 -32768+-2=-32770 ==> expected: <-32770> but was: <0>
[ERROR]   AppTest$AddShortsTest.add_whenInvoked_thenReturnsExpectedResult:121 -32768+-32768=-65536 ==> expected: <-65536> but was: <0>
[ERROR]   AppTest$AddShortsTest.add_whenInvoked_thenReturnsExpectedResult:121 32767+32767=65534 ==> expected: <65534> but was: <0>
[ERROR]   AppTest$AddShortsTest.add_whenInvoked_thenReturnsExpectedResult:121 1+2=3 ==> expected: <3> but was: <0>
[ERROR]   AppTest$AddShortsTest.add_whenInvoked_thenReturnsExpectedResult:121 -1+-1=-2 ==> expected: <-2> but was: <0>
[ERROR]   AppTest$AddShortsTest.add_whenInvoked_thenReturnsExpectedResult:121 1+32766=32767 ==> expected: <32767> but was: <0>
[ERROR]   AppTest$AddShortsTest.add_whenInvoked_thenReturnsExpectedResult:121 32766+1=32767 ==> expected: <32767> but was: <0>
[ERROR]   AppTest$AddShortsTest.add_whenInvoked_thenReturnsExpectedResult:121 -1+-32767=-32768 ==> expected: <-32768> but was: <0>
[ERROR]   AppTest$AddShortsTest.add_whenInvoked_thenReturnsExpectedResult:121 -32767+-1=-32768 ==> expected: <-32768> but was: <0>
[ERROR]   AppTest$AddShortsTest.add_whenInvoked_thenReturnsExpectedResult:121 1+32767=32768 ==> expected: <32768> but was: <0>
[ERROR]   AppTest$AddShortsTest.add_whenInvoked_thenReturnsExpectedResult:121 32767+2=32769 ==> expected: <32769> but was: <0>

Application Updates – Method Logic

Now, we can add the logic inside the new add method:

public static int add(short shortValue1, short shortValue2) {
    return shortValue1 + shortValue2;
}

Build Success

Now, rerunning the maven build should produce a BUILD SUCCESS.

Commit

Remember, commit changes in small batches. This is a good time to do so.

Adding Shorts, Too! – Whoa, that’s Overloading!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.