public static void main(String[ ] args) { test(stringLength(null), 0, "length of null"); test(stringLength(""), 0, "length of empty string"); test(stringLength("AAA"), 3, "length of AAA"); } code example

Example: public static void main(String[ ] args) { test(stringLength(null), 0, "length of null"); test(stringLength(""), 0, "length of empty string"); test(stringLength("AAA"), 3, "length of AAA"); }

class Playground {
    /**
     * Implement method that takes string as an input and 
     * returns its last character.
     * 
     * Example:
     *  getLastCharacter("a") => 'a'
     *  getLastCharacter("abcde") => 'e'
     *  getLastCharacter("12345") => '5'
     */ 
    private static char getLastCharacter(String input) {
        // WRITE YOUR CODE BELOW THIS LINE
        return 'a';
        // WRITE YOUR CODE ABOVE THIS LINE
    }
    
    public static void main(String[ ] args) {
        test(getLastCharacter("a"), 'a', "\"a\" last character");
        test(getLastCharacter("abcde"), 'e', "abcde last character");
        test(getLastCharacter("12345"), '5', "12345 last character");
    }
    
    private static void test(char actual, char expected, String testName) {
        if (actual != expected) {
            String errorMessage = String.format(
                "Test %s failed: %s is not equal to expected %s",
                testName, 
                actual,
                expected);
            System.out.println("ERROR: " + errorMessage);  
        } else {
            System.out.println("SUCCESS: " + testName + " passed");
        }
    }
}

Tags:

Java Example