From e28da7b9c12e8ad6a2feede1ba02d4eae476925e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=8A=B9=EC=9E=AC?= Date: Thu, 14 Nov 2024 23:26:15 +0900 Subject: [PATCH 01/12] Add approaches for Parallel Letter Frequency (#2863) --- .../.approaches/config.json | 27 ++++ .../.approaches/fork-join/content.md | 91 +++++++++++ .../.approaches/fork-join/snippet.txt | 7 + .../.approaches/introduction.md | 142 ++++++++++++++++++ .../.approaches/parallel-stream/content.md | 49 ++++++ .../.approaches/parallel-stream/snippet.txt | 7 + 6 files changed, 323 insertions(+) create mode 100644 exercises/practice/parallel-letter-frequency/.approaches/config.json create mode 100644 exercises/practice/parallel-letter-frequency/.approaches/fork-join/content.md create mode 100644 exercises/practice/parallel-letter-frequency/.approaches/fork-join/snippet.txt create mode 100644 exercises/practice/parallel-letter-frequency/.approaches/introduction.md create mode 100644 exercises/practice/parallel-letter-frequency/.approaches/parallel-stream/content.md create mode 100644 exercises/practice/parallel-letter-frequency/.approaches/parallel-stream/snippet.txt diff --git a/exercises/practice/parallel-letter-frequency/.approaches/config.json b/exercises/practice/parallel-letter-frequency/.approaches/config.json new file mode 100644 index 000000000..dad02090c --- /dev/null +++ b/exercises/practice/parallel-letter-frequency/.approaches/config.json @@ -0,0 +1,27 @@ +{ + "introduction": { + "authors": [ + "masiljangajji" + ] + }, + "approaches": [ + { + "uuid": "dee2a79d-3e64-4220-b99f-55667549c12c", + "slug": "fork-join", + "title": "Fork/Join", + "blurb": "Parallel Computation Using Fork/Join", + "authors": [ + "masiljangajji" + ] + }, + { + "uuid": "75e9e93b-4da4-4474-8b6e-3c0cb9b3a9bb", + "slug": "parallel-stream", + "title": "Parallel Stream", + "blurb": "Parallel Computation Using Parallel Stream", + "authors": [ + "masiljangajji" + ] + } + ] +} diff --git a/exercises/practice/parallel-letter-frequency/.approaches/fork-join/content.md b/exercises/practice/parallel-letter-frequency/.approaches/fork-join/content.md new file mode 100644 index 000000000..de2cd7307 --- /dev/null +++ b/exercises/practice/parallel-letter-frequency/.approaches/fork-join/content.md @@ -0,0 +1,91 @@ +# `Fork/Join` + +```java +import java.util.Map; +import java.util.List; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.RecursiveTask; + +class ParallelLetterFrequency { + + List texts; + ConcurrentMap letterCount; + + ParallelLetterFrequency(String[] texts) { + this.texts = List.of(texts); + letterCount = new ConcurrentHashMap<>(); + } + + Map countLetters() { + if (texts.isEmpty()) { + return letterCount; + } + + ForkJoinPool forkJoinPool = new ForkJoinPool(); + forkJoinPool.invoke(new LetterCountTask(texts, 0, texts.size(), letterCount)); + forkJoinPool.shutdown(); + + return letterCount; + } + + private static class LetterCountTask extends RecursiveTask { + private static final int THRESHOLD = 10; + private final List texts; + private final int start; + private final int end; + private final ConcurrentMap letterCount; + + LetterCountTask(List texts, int start, int end, ConcurrentMap letterCount) { + this.texts = texts; + this.start = start; + this.end = end; + this.letterCount = letterCount; + } + + @Override + protected Void compute() { + if (end - start <= THRESHOLD) { + for (int i = start; i < end; i++) { + for (char c : texts.get(i).toLowerCase().toCharArray()) { + if (Character.isAlphabetic(c)) { + letterCount.merge(c, 1, Integer::sum); + } + } + } + } else { + int mid = (start + end) / 2; + LetterCountTask leftTask = new LetterCountTask(texts, start, mid, letterCount); + LetterCountTask rightTask = new LetterCountTask(texts, mid, end, letterCount); + invokeAll(leftTask, rightTask); + } + return null; + } + } +} +``` + +Using [`ConcurrentHashMap`][ConcurrentHashMap] ensures that frequency counting and updates are safely handled in a parallel environment. + +If there are no strings, a validation step prevents unnecessary processing. + +A [`ForkJoinPool`][ForkJoinPool] is then created. +The core of [`ForkJoinPool`][ForkJoinPool] is the Fork/Join mechanism, which divides tasks into smaller units and processes them in parallel. + +`THRESHOLD` is the criterion for task division. +If the range of texts exceeds the `THRESHOLD`, the task is divided into two subtasks, and [`invokeAll(leftTask, rightTask)`][invokeAll] is called to execute both tasks in parallel. +Each subtask in `LetterCountTask` will continue calling `compute()` (via `invokeAll(leftTask, rightTask)`) to divide itself further until the range is smaller than or equal to the `THRESHOLD`. +For tasks that are within the `THRESHOLD`, letter frequency is calculated. + +The [`Character.isAlphabetic`][isAlphabetic] method identifies all characters classified as alphabetic in Unicode, covering characters from various languages like English, Korean, Japanese, Chinese, etc., returning `true`. +Non-alphabetic characters, including numbers, special characters, and spaces, return `false`. + +Additionally, since uppercase and lowercase letters are treated as the same character (e.g., `A` and `a`), each character is converted to lowercase. + +After updating letter frequencies, the final map is returned. + +[ConcurrentHashMap]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html +[ForkJoinPool]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html +[isAlphabetic]: https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isAlphabetic-int- +[invokeAll]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html diff --git a/exercises/practice/parallel-letter-frequency/.approaches/fork-join/snippet.txt b/exercises/practice/parallel-letter-frequency/.approaches/fork-join/snippet.txt new file mode 100644 index 000000000..7e782eb30 --- /dev/null +++ b/exercises/practice/parallel-letter-frequency/.approaches/fork-join/snippet.txt @@ -0,0 +1,7 @@ +for (int i = start; i < end; i++) { + for (char c : texts.get(i).toLowerCase().toCharArray()) { + if (Character.isAlphabetic(c)) { + letterCount.merge(c, 1, Integer::sum); + } + } +} \ No newline at end of file diff --git a/exercises/practice/parallel-letter-frequency/.approaches/introduction.md b/exercises/practice/parallel-letter-frequency/.approaches/introduction.md new file mode 100644 index 000000000..8ca8fbf92 --- /dev/null +++ b/exercises/practice/parallel-letter-frequency/.approaches/introduction.md @@ -0,0 +1,142 @@ +# Introduction + +There are multiple ways to solve the Parallel Letter Frequency problem. +One approach is to use [`Stream.parallelStream`][stream], and another involves using [`ForkJoinPool`][ForkJoinPool]. + +## General guidance + +To count occurrences of items, a map data structure is often used, though arrays and lists can work as well. +A [`map`][map], being a key-value pair structure, is suitable for recording frequency by incrementing the value for each key. +If the data being counted has a limited range (e.g., `Characters` or `Integers`), an `int[] array` or [`List`][list] can be used to record frequencies. + +Parallel processing typically takes place in a multi-[`thread`][thread] environment. +The Java 8 [`stream`][stream] API provides methods that make parallel processing easier, including the [`parallelStream()`][stream] method. +With [`parallelStream()`][stream], developers can use the [`ForkJoinPool`][ForkJoinPool] model for workload division and parallel execution, without the need to manually manage threads or create custom thread pools. + +The [`ForkJoinPool`][ForkJoinPool] class, optimized for dividing and managing tasks, makes parallel processing efficient. +However, [`parallelStream()`][stream] uses the common [`ForkJoinPool`][ForkJoinPool] by default, meaning multiple [`parallelStream`][stream] instances share the same thread pool unless configured otherwise. + +As a result, parallel streams may interfere with each other when sharing this thread pool, potentially affecting performance. +Although this doesn’t directly impact solving the Parallel Letter Frequency problem, it may introduce issues when thread pool sharing causes conflicts in other applications. +Therefore, a custom [`ForkJoinPool`][ForkJoinPool] approach is also provided below. + +## Approach: `parallelStream` + +```java +import java.util.Map; +import java.util.List; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ConcurrentHashMap; + +class ParallelLetterFrequency { + + List texts; + ConcurrentMap letterCount; + + ParallelLetterFrequency(String[] texts) { + this.texts = List.of(texts); + letterCount = new ConcurrentHashMap<>(); + } + + Map countLetters() { + if (!letterCount.isEmpty() || texts.isEmpty()) { + return letterCount; + } + texts.parallelStream().forEach(text -> { + for (char c: text.toLowerCase().toCharArray()) { + if (Character.isAlphabetic(c)) { + letterCount.merge(c, 1, Integer::sum); + } + } + }); + return letterCount; + } + +} +``` + +For more information, check the [`parallelStream` approach][approach-parallel-stream]. + +## Approach: `Fork/Join` + +```java +import java.util.Map; +import java.util.List; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.RecursiveTask; + +class ParallelLetterFrequency { + + List texts; + ConcurrentMap letterCount; + + ParallelLetterFrequency(String[] texts) { + this.texts = List.of(texts); + letterCount = new ConcurrentHashMap<>(); + } + + Map countLetters() { + if (!letterCount.isEmpty() || texts.isEmpty()) { + return letterCount; + } + + ForkJoinPool forkJoinPool = new ForkJoinPool(); + forkJoinPool.invoke(new LetterCountTask(texts, 0, texts.size(), letterCount)); + forkJoinPool.shutdown(); + + return letterCount; + } + + private static class LetterCountTask extends RecursiveTask { + private static final int THRESHOLD = 10; + private final List texts; + private final int start; + private final int end; + private final ConcurrentMap letterCount; + + LetterCountTask(List texts, int start, int end, ConcurrentMap letterCount) { + this.texts = texts; + this.start = start; + this.end = end; + this.letterCount = letterCount; + } + + @Override + protected Void compute() { + if (end - start <= THRESHOLD) { + for (int i = start; i < end; i++) { + for (char c : texts.get(i).toLowerCase().toCharArray()) { + if (Character.isAlphabetic(c)) { + letterCount.merge(c, 1, Integer::sum); + } + } + } + } else { + int mid = (start + end) / 2; + LetterCountTask leftTask = new LetterCountTask(texts, start, mid, letterCount); + LetterCountTask rightTask = new LetterCountTask(texts, mid, end, letterCount); + invokeAll(leftTask, rightTask); + } + return null; + } + } +} + +``` + +For more information, check the [`fork/join` approach][approach-fork-join]. + +## Which approach to use? + +When tasks are simple or do not require a dedicated thread pool (such as in this case), the [`parallelStream`][stream] approach is recommended. +However, if the work is complex or there is a need to isolate thread pools from other concurrent tasks, the [`ForkJoinPool`][ForkJoinPool] approach is preferable. + +[thread]: https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html +[stream]: https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html +[ForkJoinPool]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html +[map]: https://docs.oracle.com/javase/8/docs/api/?java/util/Map.html +[list]: https://docs.oracle.com/javase/8/docs/api/?java/util/List.html +[approach-parallel-stream]: https://exercism.org/tracks/java/exercises/parallel-letter-frequency/approaches/parallel-stream +[approach-fork-join]: https://exercism.org/tracks/java/exercises/parallel-letter-frequency/approaches/fork-join diff --git a/exercises/practice/parallel-letter-frequency/.approaches/parallel-stream/content.md b/exercises/practice/parallel-letter-frequency/.approaches/parallel-stream/content.md new file mode 100644 index 000000000..6b532b767 --- /dev/null +++ b/exercises/practice/parallel-letter-frequency/.approaches/parallel-stream/content.md @@ -0,0 +1,49 @@ +# `parallelStream` + +```java +import java.util.Map; +import java.util.List; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ConcurrentHashMap; + +class ParallelLetterFrequency { + + List texts; + ConcurrentMap letterCount; + + ParallelLetterFrequency(String[] texts) { + this.texts = List.of(texts); + letterCount = new ConcurrentHashMap<>(); + } + + Map countLetters() { + if (texts.isEmpty()) { + return letterCount; + } + texts.parallelStream().forEach(text -> { + for (char c: text.toLowerCase().toCharArray()) { + if (Character.isAlphabetic(c)) { + letterCount.merge(c, 1, Integer::sum); + } + } + }); + return letterCount; + } + +} +``` + +Using [`ConcurrentHashMap`][ConcurrentHashMap] ensures that frequency counting and updates are safely handled in a parallel environment. + +If there are no strings to process, a validation step avoids unnecessary computation. + +To calculate letter frequency, a parallel stream is used. +The [`Character.isAlphabetic`][isAlphabetic] method identifies all characters classified as alphabetic in Unicode, covering characters from various languages like English, Korean, Japanese, Chinese, etc., returning `true`. +Non-alphabetic characters, including numbers, special characters, and spaces, return `false`. + +Since we treat uppercase and lowercase letters as the same character (e.g., `A` and `a`), characters are converted to lowercase. + +After updating letter frequencies, the final map is returned. + +[ConcurrentHashMap]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html +[isAlphabetic]: https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isAlphabetic-int- diff --git a/exercises/practice/parallel-letter-frequency/.approaches/parallel-stream/snippet.txt b/exercises/practice/parallel-letter-frequency/.approaches/parallel-stream/snippet.txt new file mode 100644 index 000000000..9cbb9cffa --- /dev/null +++ b/exercises/practice/parallel-letter-frequency/.approaches/parallel-stream/snippet.txt @@ -0,0 +1,7 @@ +texts.parallelStream().forEach(text -> { + for (char c: text.toLowerCase().toCharArray()) { + if (Character.isAlphabetic(c)) { + letterCount.merge(c, 1, Integer::sum); + } + } +}); \ No newline at end of file From 64261e0a92b638caa5bda1c3aaead456396472a5 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 10 Nov 2024 01:18:49 +0530 Subject: [PATCH 02/12] 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 03/12] 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 04/12] 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 05/12] 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 06/12] 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 07/12] 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 08/12] 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 09/12] 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 33000139fdba9dc86e2fdc68590007fb517ac7f1 Mon Sep 17 00:00:00 2001 From: Anastasios Chatzialexiou <16361161+tasxatzial@users.noreply.github.com> Date: Sat, 16 Nov 2024 13:44:59 +0200 Subject: [PATCH 10/12] General fixes in scrabble-score approaches (#2865) * fix typos/grammar * format as one sentence per line * in map-reduce approach: convert to list for better readability --- .../.approaches/if-statements/content.md | 12 ++++------- .../.approaches/introduction.md | 16 ++++++--------- .../.approaches/map-reduce/content.md | 20 +++++++++---------- .../.approaches/switch-statement/content.md | 12 ++++------- 4 files changed, 23 insertions(+), 37 deletions(-) diff --git a/exercises/practice/scrabble-score/.approaches/if-statements/content.md b/exercises/practice/scrabble-score/.approaches/if-statements/content.md index ae274fe8c..14d7c2ad8 100644 --- a/exercises/practice/scrabble-score/.approaches/if-statements/content.md +++ b/exercises/practice/scrabble-score/.approaches/if-statements/content.md @@ -35,21 +35,17 @@ class Scrabble { ``` This approach defines a [`private`][private] [`final`][final] variable to be returned by the `getScore()` method. -It is `private` because it does not need to be directly accessed from outside the class, and it is `final` -because its value does not need to be changed once it is set. +It is `private` because it does not need to be directly accessed from outside the class, and it is `final` because its value does not need to be changed once it is set. In the constructor, a local variable is defined for being updated in the [`for` loop][for-loop]. ~~~~exercism/note -Using the same for a variable in a nested local scope that is used in its enclosing higher scope is called -[variable shadowing](https://www.geeksforgeeks.org/shadowing-in-java/). +Using the same for a variable in a nested local scope that is used in its enclosing higher scope is called [variable shadowing](https://www.geeksforgeeks.org/shadowing-in-java/). ~~~~ The variable is updated by a series of `if` statements that checks each letter of the uppercased word. -The letter is selected as a `String` by the [`substring()`][substring] method and is passed to the -[`contains()`][contains] method for the `String` representing the letters for a particular score. -The first `if` statement checks for the most common letters, and each succeeding `if` statement -checks for letters less common than the statement before it. +The letter is selected as a `String` by the [`substring()`][substring] method and is passed to the [`contains()`][contains] method for the `String` representing the letters for a particular score. +The first `if` statement checks for the most common letters, and each succeeding `if` statement checks for letters less common than the statement before it. When the loop is done, the class-level `score` variable is set to the value of the local `score` variable. [private]: https://en.wikibooks.org/wiki/Java_Programming/Keywords/private diff --git a/exercises/practice/scrabble-score/.approaches/introduction.md b/exercises/practice/scrabble-score/.approaches/introduction.md index f0254b392..dd3a630c1 100644 --- a/exercises/practice/scrabble-score/.approaches/introduction.md +++ b/exercises/practice/scrabble-score/.approaches/introduction.md @@ -1,17 +1,14 @@ # Introduction -There are various idiomatiuc ways to solve Scrabble Score. -The approaches could be to use a series of `if` statements, or a single `switch` statment. +There are various idiomatic ways to solve Scrabble Score. +The approaches could be to use a series of `if` statements, or a single `switch` statement. Another approach could be to look up the score in a `HashMap` from inside the `reduce()` method. ## General guidance -Regardless of the approach used, one thing to look out for is to whether to calculate the score -in the constructor (or a method called by the constructor) or in the `getScore()` method. -A benefit to calculating in the constructor is that the score is calculated only once, -no matter how many times `getScore()` is called. -A benefit to calculating in `getScore()` is that, if it is not called, -then the calculation never has to be performed. +Regardless of the approach used, one thing to look out for is to whether to calculate the score in the constructor (or a method called by the constructor) or in the `getScore()` method. +A benefit to calculating in the constructor is that the score is calculated only once, no matter how many times `getScore()` is called. +A benefit to calculating in `getScore()` is that, if it is not called, then the calculation never has to be performed. But then, in that case, why instantiate the `Scrabble` object at all? ## Approach: `if` statements @@ -149,8 +146,7 @@ For more information, check the [`HashMap` with `reduce()` approach][approach-ma ## Which approach to use? -Since benchmarking with the [Java Microbenchmark Harness][jmh] is currently outside the scope of this document, -the choice between the approaches can be made by perceived readability. +Since benchmarking with the [Java Microbenchmark Harness][jmh] is currently outside the scope of this document, the choice between the approaches can be made by perceived readability. [approach-if-statements]: https://exercism.org/tracks/java/exercises/scrabble-score/approaches/if-statements [approach-switch-statement]: https://exercism.org/tracks/java/exercises/scrabble-score/approaches/switch-statement diff --git a/exercises/practice/scrabble-score/.approaches/map-reduce/content.md b/exercises/practice/scrabble-score/.approaches/map-reduce/content.md index af19d9eea..108c67df9 100644 --- a/exercises/practice/scrabble-score/.approaches/map-reduce/content.md +++ b/exercises/practice/scrabble-score/.approaches/map-reduce/content.md @@ -46,25 +46,23 @@ class Scrabble { This approach starts by importing from packages for what is needed. A [`private`][private] [`final`][final] variable is defined to be returned by the `getScore()` method. -It is `private` because it does not need to be directly accessed from outside the class, and it is `final` -because its value does not need to be changed once it is set. +It is `private` because it does not need to be directly accessed from outside the class, and it is `final` because its value does not need to be changed once it is set. Several `private` `final` [`static`][static] variables are then defined: -a [`HashMap`][hashmap] to hold the lookups of the scores for the letters; -a `String` array of the letters grouped by their common score; -and an `int` array of the scores for the letters. + +- a [`HashMap`][hashmap] to hold the lookups of the scores for the letters +- a `String` array of the letters grouped by their common score +- an `int` array of the scores for the letters + They are `static` because they don't need to differ between object instantiations, so they can belong to the class itself. -In a [static block][static-block], the [`IntStream.range()`][range] method is called to iterate an index from `0` -up to but including the length of the array of letters. -In a [`forEach()`][foreach] method, each index value is passed into a [lambda][lambda] which calls the [`chars{}`][chars] -method on each string at the index of the letters array. +In a [static block][static-block], the [`IntStream.range()`][range] method is called to iterate an index from `0` up to but including the length of the array of letters. +In a [`forEach()`][foreach] method, each index value is passed into a [lambda][lambda] which calls the [`chars{}`][chars] method on each string at the index of the letters array. In another `forEach`, each letter is passed into a lambda that adds the letter and its corresponding score to the `HashMap`. This works because the groups of letters and their scores are at the same index in their respective arrays. In the constructor, `chars()` is called on the uppercased word and chained to the [`reduce()`][reduce] method. -The accumulator is initialized to `0`, and the accumulator and each letter is passed to a lambda that adds the score -looked up from the `HashMap` for the letter. +The accumulator is initialized to `0`, and the accumulator and each letter is passed to a lambda that adds the score looked up from the `HashMap` for the letter. The score variable is set to the value returned from `reduce()`, which is the value of its accumulator. [private]: https://en.wikibooks.org/wiki/Java_Programming/Keywords/private diff --git a/exercises/practice/scrabble-score/.approaches/switch-statement/content.md b/exercises/practice/scrabble-score/.approaches/switch-statement/content.md index 30e084a36..80205cfe5 100644 --- a/exercises/practice/scrabble-score/.approaches/switch-statement/content.md +++ b/exercises/practice/scrabble-score/.approaches/switch-statement/content.md @@ -45,26 +45,22 @@ class Scrabble { ``` This approach defines a [`private`][private] [`final`][final] variable to be returned by the `getScore()` method. -It is `private` because it does not need to be directly accessed from outside the class, and it is `final` -because its value does not need to be changed once it is set. +It is `private` because it does not need to be directly accessed from outside the class, and it is `final` because its value does not need to be changed once it is set. In the constructor, a local variable is defined for being updated in the [`for` loop][for-loop]. ~~~~exercism/note -Using the same for a variable in a nested local scope that is used in its enclosing higher scope is called -[variable shadowing](https://www.geeksforgeeks.org/shadowing-in-java/). +Using the same name for a variable in a nested local scope that is used in its enclosing higher scope is called [variable shadowing](https://www.geeksforgeeks.org/shadowing-in-java/). ~~~~ The variable is updated by a [`switch`][switch] statement that checks each letter of the lowercased word. ~~~~exercism/note -If most of the input will already be lower case, it is a bit more performant to normalize the input as lowercased, -since fewer characters will need to be changed. +If most of the input will already be lower case, it is a bit more performant to normalize the input as lowercased, since fewer characters will need to be changed. However, it may be considered that to use upper case letters is more readable. ~~~~ -The letter is selected as a `char` by the [`charAt()`][charat] method and is passed to the -`switch`, with each case representing the letters for a particular score. +The letter is selected as a `char` by the [`charAt()`][charat] method and is passed to the `switch`, with each case representing the letters for a particular score. When the loop is done, the class-level `score` variable is set to the value of the local `score` variable. [private]: https://en.wikibooks.org/wiki/Java_Programming/Keywords/private From c51eab2edb8084d6649acde8336e7704390955cf Mon Sep 17 00:00:00 2001 From: jagdish-15 Date: Sat, 16 Nov 2024 17:24:20 +0530 Subject: [PATCH 11/12] Sync Yacht with problem-specification (#2864) --- exercises/practice/yacht/.meta/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/yacht/.meta/config.json b/exercises/practice/yacht/.meta/config.json index 122b450c0..971e3587d 100644 --- a/exercises/practice/yacht/.meta/config.json +++ b/exercises/practice/yacht/.meta/config.json @@ -29,6 +29,6 @@ ] }, "blurb": "Score a single throw of dice in the game Yacht.", - "source": "James Kilfiger, using wikipedia", + "source": "James Kilfiger, using Wikipedia", "source_url": "https://en.wikipedia.org/wiki/Yacht_(dice_game)" } From 85652b5bad7a9b2a81cfbb1540d2f92b211659f3 Mon Sep 17 00:00:00 2001 From: Anastasios Chatzialexiou <16361161+tasxatzial@users.noreply.github.com> Date: Sun, 17 Nov 2024 15:05:40 +0200 Subject: [PATCH 12/12] annalyns-infiltration: Fix grammar & format (#2866) * Format exercise Markdown files to one sentence per line, as per style guide * Also opportunistically fix grammar --- .../.docs/instructions.md | 49 +++++++++++++------ .../.docs/introduction.md | 4 +- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/exercises/concept/annalyns-infiltration/.docs/instructions.md b/exercises/concept/annalyns-infiltration/.docs/instructions.md index 4ae00ea14..f8e35fd96 100644 --- a/exercises/concept/annalyns-infiltration/.docs/instructions.md +++ b/exercises/concept/annalyns-infiltration/.docs/instructions.md @@ -1,26 +1,35 @@ # Instructions -In this exercise, you'll be implementing the quest logic for a new RPG game a friend is developing. The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog. Unfortunately, disaster strikes, as her best friend was kidnapped while searching for berries in the forest. Annalyn will try to find and free her best friend, optionally taking her dog with her on this quest. +In this exercise, you'll implement the quest logic for a new RPG game that a friend is developing. +The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog. +Unfortunately, disaster strikes: her best friend was kidnapped while searching for berries in the forest. +Annalyn will try to find and rescue her friend, optionally taking her dog along on the quest. -After some time spent following her best friend's trail, she finds the camp in which her best friend is imprisoned. It turns out there are two kidnappers: a mighty knight and a cunning archer. +After some time spent following the trail, Annalyn discovers the camp where her friend is imprisoned. +It turns out there are two kidnappers: a mighty knight and a cunning archer. Having found the kidnappers, Annalyn considers which of the following actions she can engage in: -- Fast attack: a fast attack can be made if the knight is sleeping, as it takes time for him to get his armor on, so he will be vulnerable. -- Spy: the group can be spied upon if at least one of them is awake. Otherwise, spying is a waste of time. -- Signal prisoner: the prisoner can be signalled using bird sounds if the prisoner is awake and the archer is sleeping, as archers are trained in bird signaling, so they could intercept the message. -- _Free prisoner_: Annalyn can try sneaking into the camp to free the prisoner. - This is a risky thing to do and can only succeed in one of two ways: - - If Annalyn has her pet dog with her she can rescue the prisoner if the archer is asleep. +- Fast attack: a fast attack can be made if the knight is sleeping, as it takes time for him to put on his armor, leaving him vulnerable. +- Spy: the group can be spied upon if at least one of them is awake. + Otherwise, spying is a waste of time. +- Signal prisoner: the prisoner can be signaled using bird sounds if the prisoner is awake and the archer is sleeping. + Archers are trained in bird signaling and could intercept the message if they are awake. +- _Free prisoner_: Annalyn can attempt to sneak into the camp to free the prisoner. + This is risky and can only succeed in one of two ways: + - If Annalyn has her pet dog, she can rescue the prisoner if the archer is asleep. The knight is scared of the dog and the archer will not have time to get ready before Annalyn and the prisoner can escape. - - If Annalyn does not have her dog then she and the prisoner must be very sneaky! - Annalyn can free the prisoner if the prisoner is awake and the knight and archer are both sleeping, but if the prisoner is sleeping they can't be rescued: the prisoner would be startled by Annalyn's sudden appearance and wake up the knight and archer. + - If Annalyn does not have her pet dog, then she and the prisoner must be very sneaky! + Annalyn can free the prisoner if the prisoner is awake and both the knight and archer are sleeping. + However, if the prisoner is sleeping, they can't be rescued, as the prisoner would be startled by Annalyn's sudden appearance and wake up the knight and archer. -You have four tasks: to implement the logic for determining if the above actions are available based on the state of the three characters found in the forest and whether Annalyn's pet dog is present or not. +You have four tasks: to implement the logic for determining if the above actions are available based on the state of the three characters in the forest and whether Annalyn's pet dog is present or not. ## 1. Check if a fast attack can be made -Implement the (_static_) `AnnalynsInfiltration.canFastAttack()` method that takes a boolean value that indicates if the knight is awake. This method returns `true` if a fast attack can be made based on the state of the knight. Otherwise, returns `false`: +Implement the (_static_) `AnnalynsInfiltration.canFastAttack()` method, which takes a boolean value indicating whether the knight is awake. +This method returns `true` if a fast attack can be made based on the state of the knight. +Otherwise, it returns `false`: ```java boolean knightIsAwake = true; @@ -30,7 +39,9 @@ AnnalynsInfiltration.canFastAttack(knightIsAwake); ## 2. Check if the group can be spied upon -Implement the (_static_) `AnnalynsInfiltration.canSpy()` method that takes three boolean values, indicating if the knight, archer and the prisoner, respectively, are awake. The method returns `true` if the group can be spied upon, based on the state of the three characters. Otherwise, returns `false`: +Implement the (_static_) `AnnalynsInfiltration.canSpy()` method, which takes three boolean values indicating whether the knight, archer, and prisoner, respectively, are awake. +The method returns `true` if the group can be spied upon based on the state of the three characters. +Otherwise, it returns `false`: ```java boolean knightIsAwake = false; @@ -40,9 +51,11 @@ AnnalynsInfiltration.canSpy(knightIsAwake, archerIsAwake, prisonerIsAwake); // => true ``` -## 3. Check if the prisoner can be signalled +## 3. Check if the prisoner can be signaled -Implement the (_static_) `AnnalynsInfiltration.canSignalPrisoner()` method that takes two boolean values, indicating if the archer and the prisoner, respectively, are awake. The method returns `true` if the prisoner can be signalled, based on the state of the two characters. Otherwise, returns `false`: +Implement the (_static_) `AnnalynsInfiltration.canSignalPrisoner()` method, which takes two boolean values indicating whether the archer and the prisoner, respectively, are awake. +The method returns `true` if the prisoner can be signaled based on the state of the two characters. +Otherwise, it returns `false`: ```java boolean archerIsAwake = false; @@ -53,7 +66,11 @@ AnnalynsInfiltration.canSignalPrisoner(archerIsAwake, prisonerIsAwake); ## 4. Check if the prisoner can be freed -Implement the (_static_) `AnnalynsInfiltration.canFreePrisoner()` method that takes four boolean values. The first three parameters indicate if the knight, archer and the prisoner, respectively, are awake. The last parameter indicates if Annalyn's pet dog is present. The method returns `true` if the prisoner can be freed based on the state of the three characters and Annalyn's pet dog's presence. Otherwise, it returns `false`: +Implement the (_static_) `AnnalynsInfiltration.canFreePrisoner()` method, which takes four boolean values. +The first three parameters indicate whether the knight, archer, and prisoner, respectively, are awake. +The last parameter indicates whether Annalyn's pet dog is present. +The method returns `true` if the prisoner can be freed based on the state of the three characters and the presence of Annalyn's pet dog. +Otherwise, it returns `false`: ```java boolean knightIsAwake = false; diff --git a/exercises/concept/annalyns-infiltration/.docs/introduction.md b/exercises/concept/annalyns-infiltration/.docs/introduction.md index 46597333e..6ff36c4db 100644 --- a/exercises/concept/annalyns-infiltration/.docs/introduction.md +++ b/exercises/concept/annalyns-infiltration/.docs/introduction.md @@ -7,8 +7,8 @@ Booleans in Java are represented by the `boolean` type, which values can be eith Java supports three boolean operators: - `!` (NOT): negates the boolean -- `&&` (AND): takes two booleans and results in true if they're both true -- `||` (OR): results in true if any of the two booleans is true +- `&&` (AND): takes two booleans and returns `true` if they're both `true` +- `||` (OR): returns `true` if any of the two booleans is `true` ### Examples