From a58600a4b02639f99211d509760b0f27ad79de68 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 10 Nov 2024 01:18:49 +0530 Subject: [PATCH 01/25] Adding approach for Bob --- .../practice/bob/.approaches/config.json | 15 ++++ .../practice/bob/.approaches/introduction.md | 78 +++++++++++----- .../bob/.approaches/method-based/content.md | 89 +++++++++++++++++++ 3 files changed, 160 insertions(+), 22 deletions(-) create mode 100644 exercises/practice/bob/.approaches/method-based/content.md diff --git a/exercises/practice/bob/.approaches/config.json b/exercises/practice/bob/.approaches/config.json index d7a8ba83b..65d5e4c84 100644 --- a/exercises/practice/bob/.approaches/config.json +++ b/exercises/practice/bob/.approaches/config.json @@ -2,9 +2,24 @@ "introduction": { "authors": [ "bobahop" + ], + "contributors": [ + "jagdish-15" ] }, "approaches": [ + { + "uuid": "", + "slug": "function-based", + "title": "Function based", + "blurb": "Uses boolean functions to check conditions", + "authors": [ + "jagdish-15" + ], + "contributors": [ + "BenjaminGale" + ] + }, { "uuid": "323eb230-7f27-4301-88ea-19c39d3eb5b6", "slug": "if-statements", diff --git a/exercises/practice/bob/.approaches/introduction.md b/exercises/practice/bob/.approaches/introduction.md index f7b7389b2..a462612f8 100644 --- a/exercises/practice/bob/.approaches/introduction.md +++ b/exercises/practice/bob/.approaches/introduction.md @@ -1,30 +1,58 @@ # Introduction -There are various idiomatic approaches to solve Bob. -A basic approach can use a series of `if` statements to test the conditions. -An array can contain answers from which the right response is selected by an index calculated from scores given to the conditions. +In this exercise, we’re working on a program to determine Bob’s responses based on the tone and style of given messages. Bob responds differently depending on whether a message is a question, a shout, both, or silence. Various approaches can be used to implement this logic efficiently and cleanly, ensuring the code remains readable and easy to maintain. -## General guidance +## General Guidance -Regardless of the approach used, some things you could look out for include +When implementing your solution, consider the following tips to keep your code optimized and idiomatic: -- If the input is trimmed, [`trim()`][trim] only once. +- **Trim the Input Once**: Use [`trim()`][trim] only once at the start to remove any unnecessary whitespace. +- **Use Built-in Methods**: For checking if a message is a question, prefer [`endsWith("?")`][endswith] instead of manually checking the last character. +- **DRY Code**: Avoid duplicating code by combining the logic for determining a shout and a question when handling shouted questions. Following the [DRY][dry] principle helps maintain clear and maintainable code. +- **Single Determinations**: Use variables for `questioning` and `shouting` rather than calling these checks multiple times to improve efficiency. +- **Return Statements**: An early return in an `if` statement eliminates the need for additional `else` blocks, making the code more readable. +- **Curly Braces**: While optional for single-line statements, some teams may require them for readability and consistency. -- Use the [`endsWith()`][endswith] `String` method instead of checking the last character by index for `?`. +## Approach: Method-Based -- Don't copy/paste the logic for determining a shout and for determining a question into determining a shouted question. - Combine the two determinations instead of copying them. - Not duplicating the code will keep the code [DRY][dry]. +```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."; + } + + private boolean isYelling(String input) { + return input.chars() + .anyMatch(Character::isLetter) && + input.chars() + .filter(Character::isLetter) + .allMatch(Character::isUpperCase); + } -- Perhaps consider making `questioning` and `shouting` values set once instead of functions that are possibly called twice. + private boolean isAsking(String input) { + return input.endsWith("?"); + } -- 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. + private boolean isSilent(String input) { + return input.length() == 0; + } +} +``` -- If the body of an `if` statement is only one line, curly braces aren't needed. - Some teams may still require them in their style guidelines, though. +This approach defines helper methods for each type of message—silent, yelling, and asking—to keep each condition clean and easily testable. For more details, refer to the [Method-Based Approach][approach-method-based]. -## Approach: `if` statements +## Approach: `if` Statements ```java import java.util.function.Predicate; @@ -56,9 +84,9 @@ class Bob { } ``` -For more information, check the [`if` statements approach][approach-if]. +This approach utilizes nested `if` statements and a predicate for determining if a message is a shout. For more details, refer to the [`if` Statements Approach][approach-if]. -## Approach: answer array +## Approach: Answer Array ```java import java.util.function.Predicate; @@ -86,16 +114,22 @@ class Bob { } ``` -For more information, check the [Answer array approach][approach-answer-array]. +This approach uses an array of answers and calculates the appropriate index based on flags for shouting and questioning. For more details, refer to the [Answer Array Approach][approach-answer-array]. + +## Which Approach to Use? + +Choosing between the method-based approach, `if` statements, and answer array approach can come down to readability and maintainability. Each has its advantages: -## Which approach to use? +- **Method-Based**: Clear and modular, great for readability. +- **`if` Statements**: Compact and straightforward, suited for smaller projects. +- **Answer Array**: Minimizes condition checks by using indices, efficient for a variety of responses. -Since benchmarking with the [Java Microbenchmark Harness][jmh] is currently outside the scope of this document, -the choice between `if` statements and answers array can be made by perceived readability. +Experiment with these approaches to find the balance between readability and performance that best suits your needs. [trim]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim() [endswith]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#endsWith(java.lang.String) [dry]: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself +[approach-method-based]: https://exercism.org/tracks/java/exercises/bob/approaches/method-based [approach-if]: https://exercism.org/tracks/java/exercises/bob/approaches/if-statements [approach-answer-array]: https://exercism.org/tracks/java/exercises/bob/approaches/answer-array [jmh]: https://github.com/openjdk/jmh diff --git a/exercises/practice/bob/.approaches/method-based/content.md b/exercises/practice/bob/.approaches/method-based/content.md new file mode 100644 index 000000000..48639db00 --- /dev/null +++ b/exercises/practice/bob/.approaches/method-based/content.md @@ -0,0 +1,89 @@ +# Method-Based Approach + +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. + +The main `hey` method determines Bob’s response by delegating each condition to a helper method (`isSilent`, `isYelling`, and `isAsking`), each encapsulating specific logic. + +## Explanation + +This approach simplifies the main method `hey` by breaking down each response condition into helper methods: + +1. **Trimming the Input**: + 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. + +## Code structure + +```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."; + } + + private boolean isYelling(String input) { + return input.chars() + .anyMatch(Character::isLetter) && + input.chars() + .filter(Character::isLetter) + .allMatch(Character::isUpperCase); + } + + private boolean isAsking(String input) { + return input.endsWith("?"); + } + + private boolean isSilent(String input) { + return input.length() == 0; + } +} +``` + +## Advantages of the Method-Based Approach + +- **Readability**: Each method is clearly responsible for a specific condition, making the main response logic easy to follow. +- **Maintainability**: Changes to a condition can be confined to its method, minimizing impacts on the rest of the code. +- **Code Reusability**: Helper methods can be reused or adapted easily if new conditions are added in the future. + +## Considerations + +- **Efficiency**: While this approach introduces multiple method calls, it enhances readability significantly, which is often more valuable in non-performance-critical applications. +- **Error Prevention**: This approach avoids redundant code, reducing the risk of maintenance errors. + +## Shortening Condition Checks + +If each `if` statement body is only a single line, braces can be omitted, or the test expression and result could be placed on a single line. However, [Java Coding Conventions][coding-conventions] recommend always using curly braces for error prevention and easier future modifications. + +### Alternative: Inline Helper Methods + +For smaller projects, consider implementing helper methods inline or as lambdas, though this might reduce readability. + +[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 From fa97c07c2434a0b81b63bb04a7fe41f2a01579ec Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 10 Nov 2024 01:24:48 +0530 Subject: [PATCH 02/25] Fixing style errors --- exercises/practice/bob/.approaches/introduction.md | 1 - 1 file changed, 1 deletion(-) diff --git a/exercises/practice/bob/.approaches/introduction.md b/exercises/practice/bob/.approaches/introduction.md index a462612f8..9ddb760b5 100644 --- a/exercises/practice/bob/.approaches/introduction.md +++ b/exercises/practice/bob/.approaches/introduction.md @@ -132,4 +132,3 @@ Experiment with these approaches to find the balance between readability and per [approach-method-based]: https://exercism.org/tracks/java/exercises/bob/approaches/method-based [approach-if]: https://exercism.org/tracks/java/exercises/bob/approaches/if-statements [approach-answer-array]: https://exercism.org/tracks/java/exercises/bob/approaches/answer-array -[jmh]: https://github.com/openjdk/jmh From f5f76802af1fa5a6f925f81199417738687b9ec1 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 10 Nov 2024 01:27:07 +0530 Subject: [PATCH 03/25] Fixing style errors --- exercises/practice/bob/.approaches/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/bob/.approaches/introduction.md b/exercises/practice/bob/.approaches/introduction.md index 9ddb760b5..8108ff29f 100644 --- a/exercises/practice/bob/.approaches/introduction.md +++ b/exercises/practice/bob/.approaches/introduction.md @@ -8,8 +8,8 @@ When implementing your solution, consider the following tips to keep your code o - **Trim the Input Once**: Use [`trim()`][trim] only once at the start to remove any unnecessary whitespace. - **Use Built-in Methods**: For checking if a message is a question, prefer [`endsWith("?")`][endswith] instead of manually checking the last character. -- **DRY Code**: Avoid duplicating code by combining the logic for determining a shout and a question when handling shouted questions. Following the [DRY][dry] principle helps maintain clear and maintainable code. - **Single Determinations**: Use variables for `questioning` and `shouting` rather than calling these checks multiple times to improve efficiency. +- **DRY Code**: Avoid duplicating code by combining the logic for determining a shout and a question when handling shouted questions. Following the [DRY][dry] principle helps maintain clear and maintainable code. - **Return Statements**: An early return in an `if` statement eliminates the need for additional `else` blocks, making the code more readable. - **Curly Braces**: While optional for single-line statements, some teams may require them for readability and consistency. From f6ae1f1daa78eaba906a43a7161045fd11473b34 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 10 Nov 2024 01:28:29 +0530 Subject: [PATCH 04/25] Fixing style errors --- exercises/practice/bob/.approaches/method-based/content.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/bob/.approaches/method-based/content.md b/exercises/practice/bob/.approaches/method-based/content.md index 48639db00..2cca3e563 100644 --- a/exercises/practice/bob/.approaches/method-based/content.md +++ b/exercises/practice/bob/.approaches/method-based/content.md @@ -8,7 +8,7 @@ The main `hey` method determines Bob’s response by delegating each condition t This approach simplifies the main method `hey` by breaking down each response condition into helper methods: -1. **Trimming the Input**: +1. **Trimming the Input**: 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**: From 4be572f56a4868f19d762d67c6684f8d79dcd8a9 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Thu, 14 Nov 2024 22:29:41 +0530 Subject: [PATCH 05/25] Adding uuid for new approach of bob exercise --- exercises/practice/bob/.approaches/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/bob/.approaches/config.json b/exercises/practice/bob/.approaches/config.json index 65d5e4c84..fa45ba12f 100644 --- a/exercises/practice/bob/.approaches/config.json +++ b/exercises/practice/bob/.approaches/config.json @@ -9,7 +9,7 @@ }, "approaches": [ { - "uuid": "", + "uuid": "6ca5c7c0-f8f1-49b2-b137-951fa39f89eb", "slug": "function-based", "title": "Function based", "blurb": "Uses boolean functions to check conditions", From da71900e60a8bb8cc58642c8c585af5dc76459ae Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Thu, 14 Nov 2024 22:36:05 +0530 Subject: [PATCH 06/25] Fixing nameing issues --- exercises/practice/bob/.approaches/config.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/practice/bob/.approaches/config.json b/exercises/practice/bob/.approaches/config.json index fa45ba12f..1f3b30e96 100644 --- a/exercises/practice/bob/.approaches/config.json +++ b/exercises/practice/bob/.approaches/config.json @@ -10,8 +10,8 @@ "approaches": [ { "uuid": "6ca5c7c0-f8f1-49b2-b137-951fa39f89eb", - "slug": "function-based", - "title": "Function based", + "slug": "method-based", + "title": "Method based", "blurb": "Uses boolean functions to check conditions", "authors": [ "jagdish-15" From 51a8cd314727b41b4a285be8d0b986530ff72426 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Thu, 14 Nov 2024 22:42:57 +0530 Subject: [PATCH 07/25] Adding snippet.txt --- .../practice/bob/.approaches/method-based/snippet.txt | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 exercises/practice/bob/.approaches/method-based/snippet.txt diff --git a/exercises/practice/bob/.approaches/method-based/snippet.txt b/exercises/practice/bob/.approaches/method-based/snippet.txt new file mode 100644 index 000000000..80133d787 --- /dev/null +++ b/exercises/practice/bob/.approaches/method-based/snippet.txt @@ -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."; \ No newline at end of file From 3ddea061a589e474e1887b64e6abe86445719e0d Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Thu, 14 Nov 2024 23:15:21 +0530 Subject: [PATCH 08/25] FIxing formatting of config.josn for approach of bob --- exercises/practice/bob/.approaches/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/bob/.approaches/config.json b/exercises/practice/bob/.approaches/config.json index 1f3b30e96..0b9e8afc5 100644 --- a/exercises/practice/bob/.approaches/config.json +++ b/exercises/practice/bob/.approaches/config.json @@ -4,7 +4,7 @@ "bobahop" ], "contributors": [ - "jagdish-15" + "jagdish-15" ] }, "approaches": [ From 64261e0a92b638caa5bda1c3aaead456396472a5 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 10 Nov 2024 01:18:49 +0530 Subject: [PATCH 09/25] Adding approach for Bob --- .../practice/bob/.approaches/config.json | 15 ++++ .../practice/bob/.approaches/introduction.md | 78 +++++++++++----- .../bob/.approaches/method-based/content.md | 89 +++++++++++++++++++ 3 files changed, 160 insertions(+), 22 deletions(-) create mode 100644 exercises/practice/bob/.approaches/method-based/content.md diff --git a/exercises/practice/bob/.approaches/config.json b/exercises/practice/bob/.approaches/config.json index d7a8ba83b..65d5e4c84 100644 --- a/exercises/practice/bob/.approaches/config.json +++ b/exercises/practice/bob/.approaches/config.json @@ -2,9 +2,24 @@ "introduction": { "authors": [ "bobahop" + ], + "contributors": [ + "jagdish-15" ] }, "approaches": [ + { + "uuid": "", + "slug": "function-based", + "title": "Function based", + "blurb": "Uses boolean functions to check conditions", + "authors": [ + "jagdish-15" + ], + "contributors": [ + "BenjaminGale" + ] + }, { "uuid": "323eb230-7f27-4301-88ea-19c39d3eb5b6", "slug": "if-statements", diff --git a/exercises/practice/bob/.approaches/introduction.md b/exercises/practice/bob/.approaches/introduction.md index f7b7389b2..a462612f8 100644 --- a/exercises/practice/bob/.approaches/introduction.md +++ b/exercises/practice/bob/.approaches/introduction.md @@ -1,30 +1,58 @@ # Introduction -There are various idiomatic approaches to solve Bob. -A basic approach can use a series of `if` statements to test the conditions. -An array can contain answers from which the right response is selected by an index calculated from scores given to the conditions. +In this exercise, we’re working on a program to determine Bob’s responses based on the tone and style of given messages. Bob responds differently depending on whether a message is a question, a shout, both, or silence. Various approaches can be used to implement this logic efficiently and cleanly, ensuring the code remains readable and easy to maintain. -## General guidance +## General Guidance -Regardless of the approach used, some things you could look out for include +When implementing your solution, consider the following tips to keep your code optimized and idiomatic: -- If the input is trimmed, [`trim()`][trim] only once. +- **Trim the Input Once**: Use [`trim()`][trim] only once at the start to remove any unnecessary whitespace. +- **Use Built-in Methods**: For checking if a message is a question, prefer [`endsWith("?")`][endswith] instead of manually checking the last character. +- **DRY Code**: Avoid duplicating code by combining the logic for determining a shout and a question when handling shouted questions. Following the [DRY][dry] principle helps maintain clear and maintainable code. +- **Single Determinations**: Use variables for `questioning` and `shouting` rather than calling these checks multiple times to improve efficiency. +- **Return Statements**: An early return in an `if` statement eliminates the need for additional `else` blocks, making the code more readable. +- **Curly Braces**: While optional for single-line statements, some teams may require them for readability and consistency. -- Use the [`endsWith()`][endswith] `String` method instead of checking the last character by index for `?`. +## Approach: Method-Based -- Don't copy/paste the logic for determining a shout and for determining a question into determining a shouted question. - Combine the two determinations instead of copying them. - Not duplicating the code will keep the code [DRY][dry]. +```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."; + } + + private boolean isYelling(String input) { + return input.chars() + .anyMatch(Character::isLetter) && + input.chars() + .filter(Character::isLetter) + .allMatch(Character::isUpperCase); + } -- Perhaps consider making `questioning` and `shouting` values set once instead of functions that are possibly called twice. + private boolean isAsking(String input) { + return input.endsWith("?"); + } -- 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. + private boolean isSilent(String input) { + return input.length() == 0; + } +} +``` -- If the body of an `if` statement is only one line, curly braces aren't needed. - Some teams may still require them in their style guidelines, though. +This approach defines helper methods for each type of message—silent, yelling, and asking—to keep each condition clean and easily testable. For more details, refer to the [Method-Based Approach][approach-method-based]. -## Approach: `if` statements +## Approach: `if` Statements ```java import java.util.function.Predicate; @@ -56,9 +84,9 @@ class Bob { } ``` -For more information, check the [`if` statements approach][approach-if]. +This approach utilizes nested `if` statements and a predicate for determining if a message is a shout. For more details, refer to the [`if` Statements Approach][approach-if]. -## Approach: answer array +## Approach: Answer Array ```java import java.util.function.Predicate; @@ -86,16 +114,22 @@ class Bob { } ``` -For more information, check the [Answer array approach][approach-answer-array]. +This approach uses an array of answers and calculates the appropriate index based on flags for shouting and questioning. For more details, refer to the [Answer Array Approach][approach-answer-array]. + +## Which Approach to Use? + +Choosing between the method-based approach, `if` statements, and answer array approach can come down to readability and maintainability. Each has its advantages: -## Which approach to use? +- **Method-Based**: Clear and modular, great for readability. +- **`if` Statements**: Compact and straightforward, suited for smaller projects. +- **Answer Array**: Minimizes condition checks by using indices, efficient for a variety of responses. -Since benchmarking with the [Java Microbenchmark Harness][jmh] is currently outside the scope of this document, -the choice between `if` statements and answers array can be made by perceived readability. +Experiment with these approaches to find the balance between readability and performance that best suits your needs. [trim]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim() [endswith]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#endsWith(java.lang.String) [dry]: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself +[approach-method-based]: https://exercism.org/tracks/java/exercises/bob/approaches/method-based [approach-if]: https://exercism.org/tracks/java/exercises/bob/approaches/if-statements [approach-answer-array]: https://exercism.org/tracks/java/exercises/bob/approaches/answer-array [jmh]: https://github.com/openjdk/jmh diff --git a/exercises/practice/bob/.approaches/method-based/content.md b/exercises/practice/bob/.approaches/method-based/content.md new file mode 100644 index 000000000..48639db00 --- /dev/null +++ b/exercises/practice/bob/.approaches/method-based/content.md @@ -0,0 +1,89 @@ +# Method-Based Approach + +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. + +The main `hey` method determines Bob’s response by delegating each condition to a helper method (`isSilent`, `isYelling`, and `isAsking`), each encapsulating specific logic. + +## Explanation + +This approach simplifies the main method `hey` by breaking down each response condition into helper methods: + +1. **Trimming the Input**: + 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. + +## Code structure + +```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."; + } + + private boolean isYelling(String input) { + return input.chars() + .anyMatch(Character::isLetter) && + input.chars() + .filter(Character::isLetter) + .allMatch(Character::isUpperCase); + } + + private boolean isAsking(String input) { + return input.endsWith("?"); + } + + private boolean isSilent(String input) { + return input.length() == 0; + } +} +``` + +## Advantages of the Method-Based Approach + +- **Readability**: Each method is clearly responsible for a specific condition, making the main response logic easy to follow. +- **Maintainability**: Changes to a condition can be confined to its method, minimizing impacts on the rest of the code. +- **Code Reusability**: Helper methods can be reused or adapted easily if new conditions are added in the future. + +## Considerations + +- **Efficiency**: While this approach introduces multiple method calls, it enhances readability significantly, which is often more valuable in non-performance-critical applications. +- **Error Prevention**: This approach avoids redundant code, reducing the risk of maintenance errors. + +## Shortening Condition Checks + +If each `if` statement body is only a single line, braces can be omitted, or the test expression and result could be placed on a single line. However, [Java Coding Conventions][coding-conventions] recommend always using curly braces for error prevention and easier future modifications. + +### Alternative: Inline Helper Methods + +For smaller projects, consider implementing helper methods inline or as lambdas, though this might reduce readability. + +[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 From b644b1c6f87b42a1d8737f55e2ebc921724f0a0c Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 10 Nov 2024 01:24:48 +0530 Subject: [PATCH 10/25] Fixing style errors --- exercises/practice/bob/.approaches/introduction.md | 1 - 1 file changed, 1 deletion(-) diff --git a/exercises/practice/bob/.approaches/introduction.md b/exercises/practice/bob/.approaches/introduction.md index a462612f8..9ddb760b5 100644 --- a/exercises/practice/bob/.approaches/introduction.md +++ b/exercises/practice/bob/.approaches/introduction.md @@ -132,4 +132,3 @@ Experiment with these approaches to find the balance between readability and per [approach-method-based]: https://exercism.org/tracks/java/exercises/bob/approaches/method-based [approach-if]: https://exercism.org/tracks/java/exercises/bob/approaches/if-statements [approach-answer-array]: https://exercism.org/tracks/java/exercises/bob/approaches/answer-array -[jmh]: https://github.com/openjdk/jmh From 1dcbe197577138298155cb57f1658dd31a80d37e Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 10 Nov 2024 01:27:07 +0530 Subject: [PATCH 11/25] Fixing style errors --- exercises/practice/bob/.approaches/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/bob/.approaches/introduction.md b/exercises/practice/bob/.approaches/introduction.md index 9ddb760b5..8108ff29f 100644 --- a/exercises/practice/bob/.approaches/introduction.md +++ b/exercises/practice/bob/.approaches/introduction.md @@ -8,8 +8,8 @@ When implementing your solution, consider the following tips to keep your code o - **Trim the Input Once**: Use [`trim()`][trim] only once at the start to remove any unnecessary whitespace. - **Use Built-in Methods**: For checking if a message is a question, prefer [`endsWith("?")`][endswith] instead of manually checking the last character. -- **DRY Code**: Avoid duplicating code by combining the logic for determining a shout and a question when handling shouted questions. Following the [DRY][dry] principle helps maintain clear and maintainable code. - **Single Determinations**: Use variables for `questioning` and `shouting` rather than calling these checks multiple times to improve efficiency. +- **DRY Code**: Avoid duplicating code by combining the logic for determining a shout and a question when handling shouted questions. Following the [DRY][dry] principle helps maintain clear and maintainable code. - **Return Statements**: An early return in an `if` statement eliminates the need for additional `else` blocks, making the code more readable. - **Curly Braces**: While optional for single-line statements, some teams may require them for readability and consistency. From f06c782cba969d613ad8fe56de35b3b6aa337422 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 10 Nov 2024 01:28:29 +0530 Subject: [PATCH 12/25] Fixing style errors --- exercises/practice/bob/.approaches/method-based/content.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/bob/.approaches/method-based/content.md b/exercises/practice/bob/.approaches/method-based/content.md index 48639db00..2cca3e563 100644 --- a/exercises/practice/bob/.approaches/method-based/content.md +++ b/exercises/practice/bob/.approaches/method-based/content.md @@ -8,7 +8,7 @@ The main `hey` method determines Bob’s response by delegating each condition t This approach simplifies the main method `hey` by breaking down each response condition into helper methods: -1. **Trimming the Input**: +1. **Trimming the Input**: 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**: From d0346c16b1e9fbe03fba8d2d319b1a5d67080b4b Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Thu, 14 Nov 2024 22:29:41 +0530 Subject: [PATCH 13/25] Adding uuid for new approach of bob exercise --- exercises/practice/bob/.approaches/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/bob/.approaches/config.json b/exercises/practice/bob/.approaches/config.json index 65d5e4c84..fa45ba12f 100644 --- a/exercises/practice/bob/.approaches/config.json +++ b/exercises/practice/bob/.approaches/config.json @@ -9,7 +9,7 @@ }, "approaches": [ { - "uuid": "", + "uuid": "6ca5c7c0-f8f1-49b2-b137-951fa39f89eb", "slug": "function-based", "title": "Function based", "blurb": "Uses boolean functions to check conditions", From 8f2ac6b1bc9666ad132729c05754374d5b6906d8 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Thu, 14 Nov 2024 22:36:05 +0530 Subject: [PATCH 14/25] Fixing nameing issues --- exercises/practice/bob/.approaches/config.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/practice/bob/.approaches/config.json b/exercises/practice/bob/.approaches/config.json index fa45ba12f..1f3b30e96 100644 --- a/exercises/practice/bob/.approaches/config.json +++ b/exercises/practice/bob/.approaches/config.json @@ -10,8 +10,8 @@ "approaches": [ { "uuid": "6ca5c7c0-f8f1-49b2-b137-951fa39f89eb", - "slug": "function-based", - "title": "Function based", + "slug": "method-based", + "title": "Method based", "blurb": "Uses boolean functions to check conditions", "authors": [ "jagdish-15" From 0bf84b50816fbc4210c08bcd48457d700da362ae Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Thu, 14 Nov 2024 22:42:57 +0530 Subject: [PATCH 15/25] Adding snippet.txt --- .../practice/bob/.approaches/method-based/snippet.txt | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 exercises/practice/bob/.approaches/method-based/snippet.txt diff --git a/exercises/practice/bob/.approaches/method-based/snippet.txt b/exercises/practice/bob/.approaches/method-based/snippet.txt new file mode 100644 index 000000000..80133d787 --- /dev/null +++ b/exercises/practice/bob/.approaches/method-based/snippet.txt @@ -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."; \ No newline at end of file From e50938174f9766ce5738d05f579b85990d5e31b7 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Thu, 14 Nov 2024 23:15:21 +0530 Subject: [PATCH 16/25] FIxing formatting of config.josn for approach of bob --- exercises/practice/bob/.approaches/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/bob/.approaches/config.json b/exercises/practice/bob/.approaches/config.json index 1f3b30e96..0b9e8afc5 100644 --- a/exercises/practice/bob/.approaches/config.json +++ b/exercises/practice/bob/.approaches/config.json @@ -4,7 +4,7 @@ "bobahop" ], "contributors": [ - "jagdish-15" + "jagdish-15" ] }, "approaches": [ From f0543c373dc8483240bfc7d122eff89ab55d3279 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Tue, 19 Nov 2024 01:05:50 +0530 Subject: [PATCH 17/25] Chnaging the introduction.md for consistancy --- .../bob/.approaches/method-based/content.md | 80 +++++++++---------- 1 file changed, 37 insertions(+), 43 deletions(-) diff --git a/exercises/practice/bob/.approaches/method-based/content.md b/exercises/practice/bob/.approaches/method-based/content.md index 2cca3e563..816b60643 100644 --- a/exercises/practice/bob/.approaches/method-based/content.md +++ b/exercises/practice/bob/.approaches/method-based/content.md @@ -1,36 +1,5 @@ # Method-Based Approach -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. - -The main `hey` method determines Bob’s response by delegating each condition to a helper method (`isSilent`, `isYelling`, and `isAsking`), each encapsulating specific logic. - -## Explanation - -This approach simplifies the main method `hey` by breaking down each response condition into helper methods: - -1. **Trimming the Input**: - 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. - -## Code structure - ```java class Bob { String hey(String input) { @@ -66,24 +35,49 @@ class Bob { } ``` -## Advantages of the Method-Based Approach +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. -- **Readability**: Each method is clearly responsible for a specific condition, making the main response logic easy to follow. -- **Maintainability**: Changes to a condition can be confined to its method, minimizing impacts on the rest of the code. -- **Code Reusability**: Helper methods can be reused or adapted easily if new conditions are added in the future. +## Explanation -## Considerations +This approach simplifies the main method `hey` by breaking down each response condition into helper methods: -- **Efficiency**: While this approach introduces multiple method calls, it enhances readability significantly, which is often more valuable in non-performance-critical applications. -- **Error Prevention**: This approach avoids redundant code, reducing the risk of maintenance errors. +1. **Trimming the Input**: + 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. -## Shortening Condition Checks +2. **Delegating to Helper Methods**: + Each condition is evaluated using the following helper methods: -If each `if` statement body is only a single line, braces can be omitted, or the test expression and result could be placed on a single line. However, [Java Coding Conventions][coding-conventions] recommend always using curly braces for error prevention and easier future modifications. + - **`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. -### Alternative: Inline Helper Methods +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!"; +``` -For smaller projects, consider implementing helper methods inline or as lambdas, though this might reduce readability. +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() +[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 From 6def244a12d1b52240a41d4619c4e8018ae59dea Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Tue, 19 Nov 2024 01:11:34 +0530 Subject: [PATCH 18/25] Chnaging the introduction.md for fixing styling errors --- .../bob/.approaches/method-based/content.md | 57 ------------------- 1 file changed, 57 deletions(-) diff --git a/exercises/practice/bob/.approaches/method-based/content.md b/exercises/practice/bob/.approaches/method-based/content.md index 77b477222..816b60643 100644 --- a/exercises/practice/bob/.approaches/method-based/content.md +++ b/exercises/practice/bob/.approaches/method-based/content.md @@ -1,39 +1,5 @@ # Method-Based Approach -<<<<<<< HEAD -======= -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. - -The main `hey` method determines Bob’s response by delegating each condition to a helper method (`isSilent`, `isYelling`, and `isAsking`), each encapsulating specific logic. - -## Explanation - -This approach simplifies the main method `hey` by breaking down each response condition into helper methods: - -1. **Trimming the Input**: - 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. - -## Code structure - ->>>>>>> e67fc434c32715ee323ebc5079ef0497932738f6 ```java class Bob { String hey(String input) { @@ -69,7 +35,6 @@ class Bob { } ``` -<<<<<<< HEAD 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 @@ -115,26 +80,4 @@ if (isSilent(inputTrimmed)) 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() -======= -## Advantages of the Method-Based Approach - -- **Readability**: Each method is clearly responsible for a specific condition, making the main response logic easy to follow. -- **Maintainability**: Changes to a condition can be confined to its method, minimizing impacts on the rest of the code. -- **Code Reusability**: Helper methods can be reused or adapted easily if new conditions are added in the future. - -## Considerations - -- **Efficiency**: While this approach introduces multiple method calls, it enhances readability significantly, which is often more valuable in non-performance-critical applications. -- **Error Prevention**: This approach avoids redundant code, reducing the risk of maintenance errors. - -## Shortening Condition Checks - -If each `if` statement body is only a single line, braces can be omitted, or the test expression and result could be placed on a single line. However, [Java Coding Conventions][coding-conventions] recommend always using curly braces for error prevention and easier future modifications. - -### Alternative: Inline Helper Methods - -For smaller projects, consider implementing helper methods inline or as lambdas, though this might reduce readability. - -[trim]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim() ->>>>>>> e67fc434c32715ee323ebc5079ef0497932738f6 [coding-conventions]: https://www.oracle.com/java/technologies/javase/codeconventions-statements.html#449 From 498ae46a586aef99af86fbd8559aa91a168be297 Mon Sep 17 00:00:00 2001 From: jagdish-15 Date: Thu, 26 Dec 2024 23:22:36 +0530 Subject: [PATCH 19/25] Update exercises/practice/bob/.approaches/introduction.md Co-authored-by: Kah Goh --- exercises/practice/bob/.approaches/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/bob/.approaches/introduction.md b/exercises/practice/bob/.approaches/introduction.md index 8108ff29f..dd3599d72 100644 --- a/exercises/practice/bob/.approaches/introduction.md +++ b/exercises/practice/bob/.approaches/introduction.md @@ -121,7 +121,7 @@ This approach uses an array of answers and calculates the appropriate index base Choosing between the method-based approach, `if` statements, and answer array approach can come down to readability and maintainability. Each has its advantages: - **Method-Based**: Clear and modular, great for readability. -- **`if` Statements**: Compact and straightforward, suited for smaller projects. +- **`if` Statements**: Compact and straightforward. - **Answer Array**: Minimizes condition checks by using indices, efficient for a variety of responses. Experiment with these approaches to find the balance between readability and performance that best suits your needs. From ff35ee45d5db99d33b4dbb0c7b4d9505533d9e55 Mon Sep 17 00:00:00 2001 From: jagdish-15 Date: Thu, 26 Dec 2024 23:22:47 +0530 Subject: [PATCH 20/25] Update exercises/practice/bob/.approaches/method-based/content.md Co-authored-by: Kah Goh --- exercises/practice/bob/.approaches/method-based/content.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/bob/.approaches/method-based/content.md b/exercises/practice/bob/.approaches/method-based/content.md index 816b60643..abbdfade8 100644 --- a/exercises/practice/bob/.approaches/method-based/content.md +++ b/exercises/practice/bob/.approaches/method-based/content.md @@ -41,7 +41,7 @@ In this approach, the different conditions for Bob’s responses are separated i This approach simplifies the main method `hey` by breaking down each response condition into helper methods: -1. **Trimming the Input**: +### Trimming the Input 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**: From e8da28dc12f469388d4c2bfbbc23532e71cd8f98 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Thu, 26 Dec 2024 23:58:23 +0530 Subject: [PATCH 21/25] Updating approach bob --- .../practice/bob/.approaches/config.json | 18 +-- .../bob/.approaches/if-statements/content.md | 110 +++++++++--------- .../bob/.approaches/if-statements/snippet.txt | 14 +-- .../practice/bob/.approaches/introduction.md | 34 +++--- .../bob/.approaches/method-based/content.md | 83 ------------- .../bob/.approaches/method-based/snippet.txt | 8 -- 6 files changed, 88 insertions(+), 179 deletions(-) delete mode 100644 exercises/practice/bob/.approaches/method-based/content.md delete mode 100644 exercises/practice/bob/.approaches/method-based/snippet.txt diff --git a/exercises/practice/bob/.approaches/config.json b/exercises/practice/bob/.approaches/config.json index 0b9e8afc5..4aefac6c9 100644 --- a/exercises/practice/bob/.approaches/config.json +++ b/exercises/practice/bob/.approaches/config.json @@ -4,27 +4,29 @@ "bobahop" ], "contributors": [ - "jagdish-15" + "jagdish-15", + "kahgoh" ] }, "approaches": [ { "uuid": "6ca5c7c0-f8f1-49b2-b137-951fa39f89eb", - "slug": "method-based", - "title": "Method based", - "blurb": "Uses boolean functions to check conditions", + "slug": "if-statements", + "title": "if statements", + "blurb": "Use if statements to return the answer.", "authors": [ "jagdish-15" ], "contributors": [ - "BenjaminGale" + "BenjaminGale", + "kahgoh" ] }, { "uuid": "323eb230-7f27-4301-88ea-19c39d3eb5b6", - "slug": "if-statements", - "title": "if statements", - "blurb": "Use if statements to return the answer.", + "slug": "nested-if-statements", + "title": "nested if statements", + "blurb": "Use nested if statements to return the answer.", "authors": [ "bobahop" ] diff --git a/exercises/practice/bob/.approaches/if-statements/content.md b/exercises/practice/bob/.approaches/if-statements/content.md index 4c3791fc1..07463de02 100644 --- a/exercises/practice/bob/.approaches/if-statements/content.md +++ b/exercises/practice/bob/.approaches/if-statements/content.md @@ -1,88 +1,86 @@ # `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**: + + Each condition is evaluated using the following helper methods: -```java -isAlpha.matcher(msg).find() && msg == msg.toUpperCase(); -``` + - **`isSilent`**: Checks if the trimmed input has no characters. + - **`isShouting`**: Checks if the input is all uppercase and contains at least one alphabetic character, indicating shouting. + - **`isQuestioning`**: Verifies if the trimmed input ends with a question mark. + + This modular approach keeps each condition encapsulated, enhancing code clarity. -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. +### **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. -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 diff --git a/exercises/practice/bob/.approaches/if-statements/snippet.txt b/exercises/practice/bob/.approaches/if-statements/snippet.txt index 57b3f38a4..4abcfa820 100644 --- a/exercises/practice/bob/.approaches/if-statements/snippet.txt +++ b/exercises/practice/bob/.approaches/if-statements/snippet.txt @@ -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."; \ No newline at end of file diff --git a/exercises/practice/bob/.approaches/introduction.md b/exercises/practice/bob/.approaches/introduction.md index dd3599d72..e60eeeb17 100644 --- a/exercises/practice/bob/.approaches/introduction.md +++ b/exercises/practice/bob/.approaches/introduction.md @@ -2,7 +2,7 @@ In this exercise, we’re working on a program to determine Bob’s responses based on the tone and style of given messages. Bob responds differently depending on whether a message is a question, a shout, both, or silence. Various approaches can be used to implement this logic efficiently and cleanly, ensuring the code remains readable and easy to maintain. -## General Guidance +## General guidance When implementing your solution, consider the following tips to keep your code optimized and idiomatic: @@ -13,7 +13,7 @@ When implementing your solution, consider the following tips to keep your code o - **Return Statements**: An early return in an `if` statement eliminates the need for additional `else` blocks, making the code more readable. - **Curly Braces**: While optional for single-line statements, some teams may require them for readability and consistency. -## Approach: Method-Based +## Approach: `if` statements ```java class Bob { @@ -22,17 +22,17 @@ class Bob { if (isSilent(inputTrimmed)) return "Fine. Be that way!"; - if (isYelling(inputTrimmed) && isAsking(inputTrimmed)) + if (isShouting(inputTrimmed) && isQuestioning(inputTrimmed)) return "Calm down, I know what I'm doing!"; - if (isYelling(inputTrimmed)) + if (isShouting(inputTrimmed)) return "Whoa, chill out!"; - if (isAsking(inputTrimmed)) + if (isQuestioning(inputTrimmed)) return "Sure."; return "Whatever."; } - private boolean isYelling(String input) { + private boolean isShouting(String input) { return input.chars() .anyMatch(Character::isLetter) && input.chars() @@ -40,7 +40,7 @@ class Bob { .allMatch(Character::isUpperCase); } - private boolean isAsking(String input) { + private boolean isQuestioning(String input) { return input.endsWith("?"); } @@ -50,9 +50,9 @@ class Bob { } ``` -This approach defines helper methods for each type of message—silent, yelling, and asking—to keep each condition clean and easily testable. For more details, refer to the [Method-Based Approach][approach-method-based]. +This approach defines helper methods for each type of message—silent, shouting, and questioning—to keep each condition clean and easily testable. For more details, refer to the [`if` Statements Approach][approach-if]. -## Approach: `if` Statements +## Approach: nested `if` statements ```java import java.util.function.Predicate; @@ -84,9 +84,9 @@ class Bob { } ``` -This approach utilizes nested `if` statements and a predicate for determining if a message is a shout. For more details, refer to the [`if` Statements Approach][approach-if]. +This approach utilizes nested `if` statements and a predicate for determining if a message is a shout. For more details, refer to the [nested `if` Statements Approach][approach-nested-if]. -## Approach: Answer Array +## Approach: answer array ```java import java.util.function.Predicate; @@ -118,17 +118,17 @@ This approach uses an array of answers and calculates the appropriate index base ## Which Approach to Use? -Choosing between the method-based approach, `if` statements, and answer array approach can come down to readability and maintainability. Each has its advantages: +The choice between the **`if` Statements Approach**, **Nested `if` Statements Approach**, and the **Answer Array Approach** depends on readability, maintainability, and efficiency: -- **Method-Based**: Clear and modular, great for readability. -- **`if` Statements**: Compact and straightforward. -- **Answer Array**: Minimizes condition checks by using indices, efficient for a variety of responses. +- **`if` Statements Approach**: This is clear and easy to follow but checks conditions multiple times, potentially affecting performance. Storing results in variables like `questioning` and `shouting` can improve efficiency but may reduce clarity slightly. +- **Nested `if` Statements Approach**: This approach can be more efficient by avoiding redundant checks, but its nested structure can reduce readability and maintainability. +- **Answer Array Approach**: Efficient and compact, this method uses an array of responses based on flags for questioning and shouting. However, it may be less intuitive and harder to modify if more responses are needed. -Experiment with these approaches to find the balance between readability and performance that best suits your needs. +Each approach offers a balance between readability and performance, with trade-offs in flexibility and clarity. [trim]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim() [endswith]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#endsWith(java.lang.String) [dry]: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself -[approach-method-based]: https://exercism.org/tracks/java/exercises/bob/approaches/method-based +[approach-nested-if]: https://exercism.org/tracks/java/exercises/bob/approaches/nested-if-statements [approach-if]: https://exercism.org/tracks/java/exercises/bob/approaches/if-statements [approach-answer-array]: https://exercism.org/tracks/java/exercises/bob/approaches/answer-array diff --git a/exercises/practice/bob/.approaches/method-based/content.md b/exercises/practice/bob/.approaches/method-based/content.md deleted file mode 100644 index abbdfade8..000000000 --- a/exercises/practice/bob/.approaches/method-based/content.md +++ /dev/null @@ -1,83 +0,0 @@ -# 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."; - } - - private boolean isYelling(String input) { - return input.chars() - .anyMatch(Character::isLetter) && - input.chars() - .filter(Character::isLetter) - .allMatch(Character::isUpperCase); - } - - private boolean isAsking(String input) { - 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: - -### Trimming the Input - 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 diff --git a/exercises/practice/bob/.approaches/method-based/snippet.txt b/exercises/practice/bob/.approaches/method-based/snippet.txt deleted file mode 100644 index 80133d787..000000000 --- a/exercises/practice/bob/.approaches/method-based/snippet.txt +++ /dev/null @@ -1,8 +0,0 @@ -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."; \ No newline at end of file From c3dcc962ff820a31a13bce97586a02c0019d4fad Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Thu, 26 Dec 2024 23:59:24 +0530 Subject: [PATCH 22/25] Updating approach name --- .../nested-if-statements/content.md | 88 +++++++++++++++++++ .../nested-if-statements/snippet.txt | 8 ++ 2 files changed, 96 insertions(+) create mode 100644 exercises/practice/bob/.approaches/nested-if-statements/content.md create mode 100644 exercises/practice/bob/.approaches/nested-if-statements/snippet.txt diff --git a/exercises/practice/bob/.approaches/nested-if-statements/content.md b/exercises/practice/bob/.approaches/nested-if-statements/content.md new file mode 100644 index 000000000..96f927188 --- /dev/null +++ b/exercises/practice/bob/.approaches/nested-if-statements/content.md @@ -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 diff --git a/exercises/practice/bob/.approaches/nested-if-statements/snippet.txt b/exercises/practice/bob/.approaches/nested-if-statements/snippet.txt new file mode 100644 index 000000000..57b3f38a4 --- /dev/null +++ b/exercises/practice/bob/.approaches/nested-if-statements/snippet.txt @@ -0,0 +1,8 @@ +if (questioning) { + if (shouting) + return "Calm down, I know what I'm doing!"; + return "Sure."; +} +if (shouting) + return "Whoa, chill out!"; +return "Whatever."; From 173ce32bb26bcebd7b4085fef8c1da95eea1b151 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Fri, 27 Dec 2024 00:06:06 +0530 Subject: [PATCH 23/25] Updating content of if statements approach --- .../bob/.approaches/if-statements/content.md | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/exercises/practice/bob/.approaches/if-statements/content.md b/exercises/practice/bob/.approaches/if-statements/content.md index 07463de02..3d3462146 100644 --- a/exercises/practice/bob/.approaches/if-statements/content.md +++ b/exercises/practice/bob/.approaches/if-statements/content.md @@ -45,23 +45,23 @@ This approach simplifies the main method `hey` by breaking down each response co 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. -### **Delegating to Helper Methods**: - +### Delegating to Helper Methods + Each condition is evaluated using the following helper methods: - - **`isSilent`**: Checks if the trimmed input has no characters. - - **`isShouting`**: Checks if the input is all uppercase and contains at least one alphabetic character, indicating shouting. - - **`isQuestioning`**: Verifies if the trimmed input ends with a question mark. + 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**: - +### 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. + 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. This ordering ensures that Bob’s response matches the expected behavior without redundancy. From 521160ba0831c65c4c26de54f53dc63d91271027 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Fri, 27 Dec 2024 00:07:26 +0530 Subject: [PATCH 24/25] Updating content of if statements approach --- exercises/practice/bob/.approaches/if-statements/content.md | 1 + 1 file changed, 1 insertion(+) diff --git a/exercises/practice/bob/.approaches/if-statements/content.md b/exercises/practice/bob/.approaches/if-statements/content.md index 3d3462146..b42452003 100644 --- a/exercises/practice/bob/.approaches/if-statements/content.md +++ b/exercises/practice/bob/.approaches/if-statements/content.md @@ -58,6 +58,7 @@ This approach simplifies the main method `hey` by breaking down each response co ### Order of Checks The order of checks within `hey` is important: + 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. From eaa5cdf79201c040dc15d3c03f0cf4a20c5df641 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Fri, 27 Dec 2024 00:10:00 +0530 Subject: [PATCH 25/25] Updating content of if statements approach --- exercises/practice/bob/.approaches/if-statements/content.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/bob/.approaches/if-statements/content.md b/exercises/practice/bob/.approaches/if-statements/content.md index b42452003..bbd57aa88 100644 --- a/exercises/practice/bob/.approaches/if-statements/content.md +++ b/exercises/practice/bob/.approaches/if-statements/content.md @@ -58,7 +58,7 @@ This approach simplifies the main method `hey` by breaking down each response co ### Order of Checks The order of checks within `hey` is important: - + 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.