-
-
Notifications
You must be signed in to change notification settings - Fork 685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding approach for Bob #2861
base: main
Are you sure you want to change the base?
Adding approach for Bob #2861
Changes from 20 commits
a58600a
fa97c07
f5f7680
f6ae1f1
4be572f
da71900
51a8cd3
3ddea06
64261e0
b644b1c
1dcbe19
f06c782
d0346c1
8f2ac6b
0bf84b5
e509381
f0543c3
e67fc43
ad27bb2
6def244
498ae46
ff35ee4
b017e6c
e8da28d
c3dcc96
173ce32
521160b
eaa5cdf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Method-Based Approach | ||
|
||
```java | ||
class Bob { | ||
String hey(String input) { | ||
var inputTrimmed = input.trim(); | ||
|
||
if (isSilent(inputTrimmed)) | ||
return "Fine. Be that way!"; | ||
if (isYelling(inputTrimmed) && isAsking(inputTrimmed)) | ||
return "Calm down, I know what I'm doing!"; | ||
if (isYelling(inputTrimmed)) | ||
return "Whoa, chill out!"; | ||
if (isAsking(inputTrimmed)) | ||
return "Sure."; | ||
|
||
return "Whatever."; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest, I think this approach is the same as the existing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @kahgoh for your feedback! I understand your point, and while both the method-based approach and the
In summary, while both approaches work, the method-based approach tends to be cleaner, more modular, and easier to extend. It may seem similar on the surface, but the structure I’ve chosen is designed with maintainability and scalability in mind, which are good practices even for small exercises like this one. If you have any suggestions or further insights on this approach, I'd be happy to hear them! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, it has taken me quite a while to get back to this one. I think I can see where you are coming from. Looking around, I noticed Python's approach has two similar Btw, looking at the points mentioned under General guidance, there is a point about determining the yelling and questioning once: Use variables for I think the way you've currently is fine as it is, but may be worth pointing out this short coming in the introduction's Which approach to use? . |
||
} | ||
|
||
private boolean isYelling(String input) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the intro and other approaches for this exercise, we have used the term "shouting" instead of "yelling". I'd suggest using the same terminology to keep it consistent within the approaches. |
||
return input.chars() | ||
.anyMatch(Character::isLetter) && | ||
input.chars() | ||
.filter(Character::isLetter) | ||
.allMatch(Character::isUpperCase); | ||
} | ||
|
||
private boolean isAsking(String input) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to above, we've used the term "questioning" in the intro and other approaches. |
||
return input.endsWith("?"); | ||
} | ||
|
||
private boolean isSilent(String input) { | ||
return input.length() == 0; | ||
} | ||
} | ||
``` | ||
|
||
In this approach, the different conditions for Bob’s responses are separated into dedicated private methods within the `Bob` class. This method-based approach improves readability and modularity by organizing each condition check into its own method, making the main response method easier to understand and maintain. | ||
|
||
## Explanation | ||
|
||
This approach simplifies the main method `hey` by breaking down each response condition into helper methods: | ||
|
||
1. **Trimming the Input**: | ||
jagdish-15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
The `input` is trimmed using the `String` [`trim()`][trim] method to remove any leading or trailing whitespace. This helps to accurately detect if the input is empty and should prompt a `"Fine. Be that way!"` response. | ||
|
||
2. **Delegating to Helper Methods**: | ||
Each condition is evaluated using the following helper methods: | ||
|
||
- **`isSilent`**: Checks if the trimmed input has no characters. | ||
- **`isYelling`**: Checks if the input is all uppercase and contains at least one alphabetic character, indicating shouting. | ||
- **`isAsking`**: Verifies if the trimmed input ends with a question mark. | ||
|
||
This modular approach keeps each condition encapsulated, enhancing code clarity. | ||
|
||
3. **Order of Checks**: | ||
The order of checks within `hey` is important: | ||
- Silence is evaluated first, as it requires an immediate response. | ||
- Shouted questions take precedence over individual checks for yelling and asking. | ||
- Yelling comes next, requiring its response if not combined with a question. | ||
- Asking (a non-shouted question) is checked afterward. | ||
|
||
This ordering ensures that Bob’s response matches the expected behavior without redundancy. | ||
|
||
## Shortening | ||
|
||
When the body of an `if` statement is a single line, both the test expression and the body _could_ be put on the same line, like so: | ||
|
||
```java | ||
if (isSilent(inputTrimmed)) return "Fine. Be that way!"; | ||
``` | ||
|
||
or the body _could_ be put on a separate line without curly braces: | ||
|
||
```java | ||
if (isSilent(inputTrimmed)) | ||
return "Fine. Be that way!"; | ||
``` | ||
|
||
However, the [Java Coding Conventions][coding-conventions] advise always using curly braces for `if` statements, which helps to avoid errors. Your team may choose to overrule them at its own risk. | ||
|
||
[trim]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim() | ||
[coding-conventions]: https://www.oracle.com/java/technologies/javase/codeconventions-statements.html#449 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
if (isSilent(inputTrimmed)) | ||
return "Fine. Be that way!"; | ||
if (isYelling(inputTrimmed) && isAsking(inputTrimmed)) | ||
return "Calm down, I know what I'm doing!"; | ||
if (isYelling(inputTrimmed)) | ||
return "Whoa, chill out!"; | ||
if (isAsking(inputTrimmed)) | ||
return "Sure."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the casing in the headings need changing - there are consistent with most other other approaches. Could you undo the changes to the headings?