-
-
Notifications
You must be signed in to change notification settings - Fork 691
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
Open
jagdish-15
wants to merge
32
commits into
exercism:main
Choose a base branch
from
jagdish-15:add-approach-bob
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Adding approach for Bob #2861
Changes from 29 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
a58600a
Adding approach for Bob
jagdish-15 fa97c07
Fixing style errors
jagdish-15 f5f7680
Fixing style errors
jagdish-15 f6ae1f1
Fixing style errors
jagdish-15 4be572f
Adding uuid for new approach of bob exercise
jagdish-15 da71900
Fixing nameing issues
jagdish-15 51a8cd3
Adding snippet.txt
jagdish-15 3ddea06
FIxing formatting of config.josn for approach of bob
jagdish-15 64261e0
Adding approach for Bob
jagdish-15 b644b1c
Fixing style errors
jagdish-15 1dcbe19
Fixing style errors
jagdish-15 f06c782
Fixing style errors
jagdish-15 d0346c1
Adding uuid for new approach of bob exercise
jagdish-15 8f2ac6b
Fixing nameing issues
jagdish-15 0bf84b5
Adding snippet.txt
jagdish-15 e509381
FIxing formatting of config.josn for approach of bob
jagdish-15 f0543c3
Chnaging the introduction.md for consistancy
jagdish-15 e67fc43
Merge branch 'main' into add-approach-bob
jagdish-15 ad27bb2
Chnaging the introduction.md for consistancy
jagdish-15 6def244
Chnaging the introduction.md for fixing styling errors
jagdish-15 498ae46
Update exercises/practice/bob/.approaches/introduction.md
jagdish-15 ff35ee4
Update exercises/practice/bob/.approaches/method-based/content.md
jagdish-15 b017e6c
Merge branch 'main' into add-approach-bob
jagdish-15 e8da28d
Updating approach bob
jagdish-15 c3dcc96
Updating approach name
jagdish-15 173ce32
Updating content of if statements approach
jagdish-15 521160b
Updating content of if statements approach
jagdish-15 eaa5cdf
Updating content of if statements approach
jagdish-15 1802823
Merge branch 'main' into add-approach-bob
jagdish-15 fc74587
Merge branch 'main' into add-approach-bob
jagdish-15 2e6b670
Changing approach names
jagdish-15 1093c5b
Changing content titles for consistancy
jagdish-15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 55 additions & 56 deletions
111
exercises/practice/bob/.approaches/if-statements/content.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,87 @@ | ||
# `if` statements | ||
|
||
```java | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
class Bob { | ||
|
||
final private static Pattern isAlpha = Pattern.compile("[a-zA-Z]"); | ||
final private static Predicate < String > isShout = msg -> isAlpha.matcher(msg).find() && msg == msg.toUpperCase(); | ||
|
||
public String hey(String message) { | ||
var speech = message.trim(); | ||
if (speech.isEmpty()) { | ||
return "Fine. Be that way!"; | ||
} | ||
var questioning = speech.endsWith("?"); | ||
var shouting = isShout.test(speech); | ||
if (questioning) { | ||
if (shouting) { | ||
return "Calm down, I know what I'm doing!"; | ||
} | ||
return "Sure."; | ||
} | ||
if (shouting) { | ||
String hey(String input) { | ||
var inputTrimmed = input.trim(); | ||
|
||
if (isSilent(inputTrimmed)) | ||
return "Fine. Be that way!"; | ||
if (isShouting(inputTrimmed) && isQuestioning(inputTrimmed)) | ||
return "Calm down, I know what I'm doing!"; | ||
if (isShouting(inputTrimmed)) | ||
return "Whoa, chill out!"; | ||
} | ||
if (isQuestioning(inputTrimmed)) | ||
return "Sure."; | ||
|
||
return "Whatever."; | ||
} | ||
|
||
private boolean isShouting(String input) { | ||
return input.chars() | ||
.anyMatch(Character::isLetter) && | ||
input.chars() | ||
.filter(Character::isLetter) | ||
.allMatch(Character::isUpperCase); | ||
} | ||
|
||
private boolean isQuestioning(String input) { | ||
return input.endsWith("?"); | ||
} | ||
|
||
private boolean isSilent(String input) { | ||
return input.length() == 0; | ||
} | ||
} | ||
``` | ||
|
||
In this approach you have a series of `if` statements using the private methods to evaluate the conditions. | ||
As soon as the right condition is found, the correct response is returned. | ||
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. | ||
|
||
Note that there are no `else if` or `else` statements. | ||
If an `if` statement can return, then an `else if` or `else` is not needed. | ||
Execution will either return or will continue to the next statement anyway. | ||
## Explanation | ||
|
||
The `String` [`trim()`][trim] method is applied to the input to eliminate any whitespace at either end of the input. | ||
If the string has no characters left, it returns the response for saying nothing. | ||
This approach simplifies the main method `hey` by breaking down each response condition into helper methods: | ||
|
||
~~~~exercism/caution | ||
Note that a `null` `string` would be different from a `String` of all whitespace. | ||
A `null` `String` would throw a `NullPointerException` if `trim()` were applied to it. | ||
~~~~ | ||
### Trimming the Input | ||
|
||
A [Pattern][pattern] is defined to look for at least one English alphabetic character. | ||
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. | ||
|
||
The first half of the `isShout` [Predicate][predicate] | ||
### Delegating to Helper Methods | ||
|
||
```java | ||
isAlpha.matcher(msg).find() && msg == msg.toUpperCase(); | ||
``` | ||
Each condition is evaluated using the following helper methods: | ||
|
||
1. **`isSilent`**: Checks if the trimmed input has no characters. | ||
2. **`isShouting`**: Checks if the input is all uppercase and contains at least one alphabetic character, indicating shouting. | ||
3. **`isQuestioning`**: Verifies if the trimmed input ends with a question mark. | ||
|
||
This modular approach keeps each condition encapsulated, enhancing code clarity. | ||
|
||
### Order of Checks | ||
|
||
The order of checks within `hey` is important: | ||
|
||
is constructed from the `Pattern` [`matcher()`][matcher-method] method and the [`Matcher`][matcher] [`find()`][find] method | ||
to ensure there is at least one letter character in the `String`. | ||
This is because the second half of the condition tests that the uppercased input is the same as the input. | ||
If the input were only `"123"` it would equal itself uppercased, but without letters it would not be a shout. | ||
1. Silence is evaluated first, as it requires an immediate response. | ||
2. Shouted questions take precedence over individual checks for shouting and questioning. | ||
3. Shouting comes next, requiring its response if not combined with a question. | ||
4. Questioning (a non-shouted question) is checked afterward. | ||
|
||
A question is determined by use of the [`endsWith()`][endswith] method to see if the input ends with a question mark. | ||
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 | ||
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 (speech.isEmpty()) return "Fine. Be that way!"; | ||
if (isSilent(inputTrimmed)) return "Fine. Be that way!"; | ||
``` | ||
|
||
or the body _could_ be put on a separate line without curly braces | ||
or the body _could_ be put on a separate line without curly braces: | ||
|
||
```java | ||
if (speech.isEmpty()) | ||
if (isSilent(inputTrimmed)) | ||
return "Fine. Be that way!"; | ||
``` | ||
|
||
However, the [Java Coding Conventions][coding-conventions] advise to always use curly braces for `if` statements, which helps to avoid errors. | ||
Your team may choose to overrule them at its own risk. | ||
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() | ||
[pattern]: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html | ||
[predicate]: https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html | ||
[matcher]: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html | ||
[matcher-method]: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#matcher-java.lang.CharSequence- | ||
[find]: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#find-- | ||
[endswith]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#endsWith(java.lang.String) | ||
[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 |
14 changes: 7 additions & 7 deletions
14
exercises/practice/bob/.approaches/if-statements/snippet.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
if (questioning) { | ||
if (shouting) | ||
return "Calm down, I know what I'm doing!"; | ||
return "Sure."; | ||
} | ||
if (shouting) | ||
if (isSilent(inputTrimmed)) | ||
return "Fine. Be that way!"; | ||
if (isShouting(inputTrimmed) && isQuestioning(inputTrimmed)) | ||
return "Calm down, I know what I'm doing!"; | ||
if (isShouting(inputTrimmed)) | ||
return "Whoa, chill out!"; | ||
return "Whatever."; | ||
if (isQuestioning(inputTrimmed)) | ||
return "Sure."; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
exercises/practice/bob/.approaches/nested-if-statements/content.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# nested `if` statements | ||
|
||
```java | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
class Bob { | ||
|
||
final private static Pattern isAlpha = Pattern.compile("[a-zA-Z]"); | ||
final private static Predicate < String > isShout = msg -> isAlpha.matcher(msg).find() && msg == msg.toUpperCase(); | ||
|
||
public String hey(String message) { | ||
var speech = message.trim(); | ||
if (speech.isEmpty()) { | ||
return "Fine. Be that way!"; | ||
} | ||
var questioning = speech.endsWith("?"); | ||
var shouting = isShout.test(speech); | ||
if (questioning) { | ||
if (shouting) { | ||
return "Calm down, I know what I'm doing!"; | ||
} | ||
return "Sure."; | ||
} | ||
if (shouting) { | ||
return "Whoa, chill out!"; | ||
} | ||
return "Whatever."; | ||
} | ||
} | ||
``` | ||
|
||
In this approach you have a series of `if` statements using the private methods to evaluate the conditions. | ||
As soon as the right condition is found, the correct response is returned. | ||
|
||
Note that there are no `else if` or `else` statements. | ||
If an `if` statement can return, then an `else if` or `else` is not needed. | ||
Execution will either return or will continue to the next statement anyway. | ||
|
||
The `String` [`trim()`][trim] method is applied to the input to eliminate any whitespace at either end of the input. | ||
If the string has no characters left, it returns the response for saying nothing. | ||
|
||
~~~~exercism/caution | ||
Note that a `null` `string` would be different from a `String` of all whitespace. | ||
A `null` `String` would throw a `NullPointerException` if `trim()` were applied to it. | ||
~~~~ | ||
|
||
A [Pattern][pattern] is defined to look for at least one English alphabetic character. | ||
|
||
The first half of the `isShout` [Predicate][predicate] | ||
|
||
```java | ||
isAlpha.matcher(msg).find() && msg == msg.toUpperCase(); | ||
``` | ||
|
||
is constructed from the `Pattern` [`matcher()`][matcher-method] method and the [`Matcher`][matcher] [`find()`][find] method | ||
to ensure there is at least one letter character in the `String`. | ||
This is because the second half of the condition tests that the uppercased input is the same as the input. | ||
If the input were only `"123"` it would equal itself uppercased, but without letters it would not be a shout. | ||
|
||
A question is determined by use of the [`endsWith()`][endswith] method to see if the input ends with a question mark. | ||
|
||
## 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 (speech.isEmpty()) return "Fine. Be that way!"; | ||
``` | ||
|
||
or the body _could_ be put on a separate line without curly braces | ||
|
||
```java | ||
if (speech.isEmpty()) | ||
return "Fine. Be that way!"; | ||
``` | ||
|
||
However, the [Java Coding Conventions][coding-conventions] advise to always use 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() | ||
[pattern]: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html | ||
[predicate]: https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html | ||
[matcher]: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html | ||
[matcher-method]: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#matcher-java.lang.CharSequence- | ||
[find]: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Matcher.html#find-- | ||
[endswith]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#endsWith(java.lang.String) | ||
[coding-conventions]: https://www.oracle.com/java/technologies/javase/codeconventions-statements.html#449 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Sorry, I think there might been a bit of misundstanding, as I didn't mean to copy Python's approaches entirely. I was hoping keep the original
if-statement
approach as it was before your changes and your approach added asif-statement-using-methods
(or something similar). Sorry for not being clearer earlier.