There is a copy of the following code block in an interactive mode that you can modify and compile. Your task is to improve the code’s style as an expert would view it, while ensuring its behavior remains unchanged. You may run your refactored code to verify that it still passes all test cases. Imagine str is not null.
public class SWord {
public static boolean startsWithS(String str) {
if (str.isEmpty() || str.charAt(0) != 'S') {
return false;
} else {
return true;
}
}
}
Activity1.36.2.
Which of the following code blocks is the most similar to your final refactored code for the previous question?
public class SWord {
public static boolean startsWithS(String str) {
if (!str.isEmpty() && str.charAt(0) == 'S') {
return true;
}
return false;
}
}
This code can further be improved!
public class SWord {
public static boolean startsWithS(String str) {
if (str.isEmpty()) {
return false;
}
return str.charAt(0) == 'S';
}
}
You refactored correctly!
public class SWord {
public static boolean startsWithS(String str) {
return !str.isEmpty() && str.charAt(0) == 'S';
}
}