Log events

Report a typo

Suppose, we have a utility method to capitalize strings:

class Util {
    public static String capitalize(String str) {
        if (str == null || str.isBlank()) {
            return str;
        }

        if (str.length() == 1) {
            return str.toUpperCase();
        }

        return Character.toUpperCase(str.charAt(0)) + str.substring(1);
    }
}

Your task is to add some logging to this method so it prints the input string and the string to be returned by the method in the following format:

Before: string
After: String

where string is an example of the input string. As you can see, the method has three execution paths and they all must be logged.

Sample Input 1:

string

Sample Output 1:

Before: string
After: String

Sample Input 2:

Capitalized

Sample Output 2:

Before: Capitalized
After: Capitalized
Write a program in Java 17
class Util {
public static String capitalize(String str) {
if (str == null || str.isBlank()) {
return str;
}

if (str.length() == 1) {
return str.toUpperCase();
}

return Character.toUpperCase(str.charAt(0)) + str.substring(1);
}
}
___

Create a free account to access the full topic