You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercises/concept/annalyns-infiltration/.docs/instructions.md
+33-16Lines changed: 33 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,35 @@
1
1
# Instructions
2
2
3
-
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.
3
+
In this exercise, you'll implement the quest logic for a new RPG game that a friend is developing.
4
+
The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog.
5
+
Unfortunately, disaster strikes: her best friend was kidnapped while searching for berries in the forest.
6
+
Annalyn will try to find and rescue her friend, optionally taking her dog along on the quest.
4
7
5
-
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.
8
+
After some time spent following the trail, Annalyn discovers the camp where her friend is imprisoned.
9
+
It turns out there are two kidnappers: a mighty knight and a cunning archer.
6
10
7
11
Having found the kidnappers, Annalyn considers which of the following actions she can engage in:
8
12
9
-
- 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.
10
-
- Spy: the group can be spied upon if at least one of them is awake. Otherwise, spying is a waste of time.
11
-
- 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.
12
-
-_Free prisoner_: Annalyn can try sneaking into the camp to free the prisoner.
13
-
This is a risky thing to do and can only succeed in one of two ways:
14
-
- If Annalyn has her pet dog with her she can rescue the prisoner if the archer is asleep.
13
+
- 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.
14
+
- Spy: the group can be spied upon if at least one of them is awake.
15
+
Otherwise, spying is a waste of time.
16
+
- Signal prisoner: the prisoner can be signaled using bird sounds if the prisoner is awake and the archer is sleeping.
17
+
Archers are trained in bird signaling and could intercept the message if they are awake.
18
+
-_Free prisoner_: Annalyn can attempt to sneak into the camp to free the prisoner.
19
+
This is risky and can only succeed in one of two ways:
20
+
- If Annalyn has her pet dog, she can rescue the prisoner if the archer is asleep.
15
21
The knight is scared of the dog and the archer will not have time to get ready before Annalyn and the prisoner can escape.
16
-
- If Annalyn does not have her dog then she and the prisoner must be very sneaky!
17
-
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.
22
+
- If Annalyn does not have her pet dog, then she and the prisoner must be very sneaky!
23
+
Annalyn can free the prisoner if the prisoner is awake and both the knight and archer are sleeping.
24
+
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.
18
25
19
-
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.
26
+
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.
20
27
21
28
## 1. Check if a fast attack can be made
22
29
23
-
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`:
30
+
Implement the (_static_) `AnnalynsInfiltration.canFastAttack()` method, which takes a boolean value indicating whether the knight is awake.
31
+
This method returns `true` if a fast attack can be made based on the state of the knight.
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`:
42
+
Implement the (_static_) `AnnalynsInfiltration.canSpy()` method, which takes three boolean values indicating whether the knight, archer, and prisoner, respectively, are awake.
43
+
The method returns `true` if the group can be spied upon based on the state of the three characters.
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`:
56
+
Implement the (_static_) `AnnalynsInfiltration.canSignalPrisoner()` method, which takes two boolean values indicating whether the archer and the prisoner, respectively, are awake.
57
+
The method returns `true` if the prisoner can be signaled based on the state of the two characters.
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`:
69
+
Implement the (_static_) `AnnalynsInfiltration.canFreePrisoner()` method, which takes four boolean values.
70
+
The first three parameters indicate whether the knight, archer, and prisoner, respectively, are awake.
71
+
The last parameter indicates whether Annalyn's pet dog is present.
72
+
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.
Copy file name to clipboardExpand all lines: exercises/practice/bob/.approaches/method-based/content.md
+57Lines changed: 57 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,39 @@
1
1
# Method-Based Approach
2
2
3
+
<<<<<<< HEAD
4
+
=======
5
+
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.
6
+
7
+
The main `hey` method determines Bob’s response by delegating each condition to a helper method (`isSilent`, `isYelling`, and `isAsking`), each encapsulating specific logic.
8
+
9
+
## Explanation
10
+
11
+
This approach simplifies the main method `hey` by breaking down each response condition into helper methods:
12
+
13
+
1.**Trimming the Input**:
14
+
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.
15
+
16
+
2.**Delegating to Helper Methods**:
17
+
Each condition is evaluated using the following helper methods:
18
+
19
+
-**`isSilent`**: Checks if the trimmed input has no characters.
20
+
-**`isYelling`**: Checks if the input is all uppercase and contains at least one alphabetic character, indicating shouting.
21
+
-**`isAsking`**: Verifies if the trimmed input ends with a question mark.
22
+
23
+
This modular approach keeps each condition encapsulated, enhancing code clarity.
24
+
25
+
3.**Order of Checks**:
26
+
The order of checks within `hey` is important:
27
+
- Silence is evaluated first, as it requires an immediate response.
28
+
- Shouted questions take precedence over individual checks for yelling and asking.
29
+
- Yelling comes next, requiring its response if not combined with a question.
30
+
- Asking (a non-shouted question) is checked afterward.
31
+
32
+
This ordering ensures that Bob’s response matches the expected behavior without redundancy.
33
+
34
+
## Code structure
35
+
36
+
>>>>>>> e67fc434c32715ee323ebc5079ef0497932738f6
3
37
```java
4
38
classBob {
5
39
Stringhey(Stringinput) {
@@ -35,6 +69,7 @@ class Bob {
35
69
}
36
70
```
37
71
72
+
<<<<<<< HEAD
38
73
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.
39
74
40
75
## Explanation
@@ -80,4 +115,26 @@ if (isSilent(inputTrimmed))
80
115
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.
-**Readability**: Each method is clearly responsible for a specific condition, making the main response logic easy to follow.
122
+
-**Maintainability**: Changes to a condition can be confined to its method, minimizing impacts on the rest of the code.
123
+
-**Code Reusability**: Helper methods can be reused or adapted easily if new conditions are added in the future.
124
+
125
+
## Considerations
126
+
127
+
-**Efficiency**: While this approach introduces multiple method calls, it enhances readability significantly, which is often more valuable in non-performance-critical applications.
128
+
-**Error Prevention**: This approach avoids redundant code, reducing the risk of maintenance errors.
129
+
130
+
## Shortening Condition Checks
131
+
132
+
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.
133
+
134
+
### Alternative: Inline Helper Methods
135
+
136
+
For smaller projects, consider implementing helper methods inline or as lambdas, though this might reduce readability.
Using [`ConcurrentHashMap`][ConcurrentHashMap] ensures that frequency counting and updates are safely handled in a parallel environment.
70
+
71
+
If there are no strings, a validation step prevents unnecessary processing.
72
+
73
+
A [`ForkJoinPool`][ForkJoinPool] is then created.
74
+
The core of [`ForkJoinPool`][ForkJoinPool] is the Fork/Join mechanism, which divides tasks into smaller units and processes them in parallel.
75
+
76
+
`THRESHOLD` is the criterion for task division.
77
+
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.
78
+
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`.
79
+
For tasks that are within the `THRESHOLD`, letter frequency is calculated.
80
+
81
+
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`.
82
+
Non-alphabetic characters, including numbers, special characters, and spaces, return `false`.
83
+
84
+
Additionally, since uppercase and lowercase letters are treated as the same character (e.g., `A` and `a`), each character is converted to lowercase.
85
+
86
+
After updating letter frequencies, the final map is returned.
0 commit comments