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