Skip to content

Commit 902f1a6

Browse files
kahgohjagdish-15
andauthored
Make squeaky clean more beginner friendly (#3013)
This change: - Adds more detail to the chars concept and introduction to try help students get closer to the intended solution the first time. During mentoring, it was noticed there were solutions using String.replace, which was not intended. - Replace Unicode link from chars concept. In 02d488e the concept was updated to remove Unicode. However, this link seems to have been forgotten. - Replace "if-else-statements" with "strings" as a prerequisite for squeaky clean. The solution seems to require obtaining the characters in the String and looping over them. "if-else-statements" is also a prerequisite for "arrays", so students will still have to "if-else-statements" before squeaky clean. Co-authored-by: Jagdish Prajapati <[email protected]>
1 parent 03bfe74 commit 902f1a6

File tree

7 files changed

+275
-21
lines changed

7 files changed

+275
-21
lines changed

concepts/chars/.meta/config.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
"authors": [
44
"ystromm"
55
],
6-
"contributors": []
6+
"contributors": [
7+
"kahgoh"
8+
]
79
}

concepts/chars/about.md

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,108 @@
11
# About
22

3-
`char`s are generally easy to use.
4-
They can be extracted from strings, added back (by means of a string builder), defined and initialised using literals with single quotes, as in `char ch = 'A';`, assigned and compared.
3+
The Java `char` primitive type is a 16 bit representation of a single Unicode character.
54

6-
The Character class encapsulates the char value.
5+
~~~~exercism/note
6+
The `char` type is based on the [original Unicode specification][unicode-specification], which used 16 bits to represent characters.
7+
This is enough to cover most of the common letters and covers characters in the range 0x0000 to 0xFFFF.
8+
The specification has since expanded the range of possible characters up to 0x01FFFF.
9+
10+
[unicode-specification]: https://www.unicode.org/versions/Unicode1.0.0/
11+
~~~~
12+
13+
Multiple `char`s can comprise a string, such as `"word"`, or `char`s can be processed independently.
14+
A `char` literal is surrounded by single quotes (e.g. `'A'`).
15+
16+
```java
17+
char lowerA = 'a';
18+
char upperB = 'B';
19+
```
20+
21+
## Getting the `char`s of a `String`
22+
23+
The `String.toCharArray` method returns a String's chars as an array.
24+
As mentioned in [arrays][concept-arrays], you can use a `for` loop to iterate over the array.
25+
26+
```java
27+
String text = "Hello";
28+
char[] asArray = text.toCharArray();
29+
30+
for (char ch: asArray) {
31+
System.out.println(ch);
32+
}
33+
34+
// Outputs:
35+
// H
36+
// e
37+
// l
38+
// l
39+
// o
40+
```
41+
42+
## The [Character][docs-character] class
43+
44+
There are many builtin library methods to inspect and manipulate `char`s.
45+
These can be found as static methods of the [`java.lang.Character`][docs-character] class.
46+
Here are some examples:
47+
48+
```java
49+
Character.isWhitespace(' '); // true
50+
Character.isWhitespace('#'); // false
51+
52+
Character.isLetter('a'); // true
53+
Character.isLetter('3'); // false
54+
55+
Character.isDigit('6'); // true
56+
Character.isDigit('?'); // false
57+
```
58+
59+
~~~~exercism/note
60+
Some methods in the Character class have an overload so that it can take either an `char` or `int`.
61+
For example, `isDigit` has one that accepts a [`char`][is-digit-char] and another an [`int`][is-digit-int].
62+
As mentioned earlier, the `char` type can only represent the characters in the range from 0x0000 to 0xFFFF.
63+
The `int`, however, can represent all characters, hence the `int` overloads.
64+
65+
[is-digit-char]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Character.html#isDigit(char)
66+
[is-digit-int]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Character.html#isDigit(int)
67+
~~~~
68+
69+
## Adding a `char` to a `String`
70+
71+
The `+` operator can be used to add a `char` to a `String`.
72+
73+
```java
74+
'a' + " banana" // => "a banana"
75+
"banana " + 'a' // => "banana a"
76+
```
77+
78+
~~~~exercism/caution
79+
Becareful _not_ to use `+` to join two `char`s together to form a `String`!
80+
Adding two `char`s this way gives an `int`, _not_ a `String`!
81+
For example:
82+
83+
```java
84+
'b' + 'c';
85+
// => 197 (not the String "bc")
86+
```
87+
88+
This is because Java promotes the `char` to an `int` (see [4.2 Primitive Types and Values ][jls-primitives] of the [Java Language Specification][jls-main]).
89+
90+
[jls-main]: https://docs.oracle.com/javase/specs/jls/se21/html/
91+
[jls-primitives]: https://docs.oracle.com/javase/specs/jls/se21/html/jls-4.html#jls-4.2
92+
~~~~
93+
94+
However, when there are many characters to be added, it can be more efficient to use a [`StringBuilder`][docs-stringBuilder] instead:
95+
96+
```java
97+
StringBuilder builder = new StringBuilder();
98+
builder.append('a');
99+
builder.append('b');
100+
builder.append('c');
101+
102+
String builtString = builder.toString();
103+
// => abc
104+
```
105+
106+
[concept-arrays]: https://exercism.org/tracks/java/concepts/arrays
107+
[docs-character]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Character.html
108+
[docs-stringBuilder]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/StringBuilder.html

concepts/chars/introduction.md

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,87 @@
11
# Introduction
22

3-
The Java `char` type represents the smallest addressable components of text.
4-
Multiple `char`s can comprise a string such as `"word"` or `char`s can be processed independently.
5-
Their literals have single quotes e.g. `'A'`.
3+
## chars
4+
5+
The Java `char` primitive type is a 16 bit representation of a single character.
6+
Multiple `char`s can comprise a string, such as `"word"`, or `char`s can be processed independently.
7+
A `char` literal is surrounded by single quotes (e.g. `'A'`).
8+
9+
```java
10+
char lowerA = 'a';
11+
char upperB = 'B';
12+
```
13+
14+
## Getting the `char`s of a `String`
15+
16+
The `String.toCharArray` method returns a String's chars as an array.
17+
As mentioned in arrays, you can use a `for` loop to iterate over the array.
18+
19+
```java
20+
String text = "Hello";
21+
char[] asArray = text.toCharArray();
22+
23+
for (char ch: asArray) {
24+
System.out.println(ch);
25+
}
26+
27+
// Outputs:
28+
// H
29+
// e
30+
// l
31+
// l
32+
// o
33+
```
34+
35+
## The Character class
636

737
There are many builtin library methods to inspect and manipulate `char`s.
838
These can be found as static methods of the `java.lang.Character` class.
39+
Here are some examples:
40+
41+
```java
42+
Character.isWhitespace(' '); // true
43+
Character.isWhitespace('#'); // false
44+
45+
Character.isLetter('a'); // true
46+
Character.isLetter('3'); // false
47+
48+
Character.isDigit('6'); // true
49+
Character.isDigit('?'); // false
50+
```
51+
52+
## Adding a `char` to a `String`
53+
54+
The `+` operator can be used to add a `char` to a `String`.
55+
56+
```java
57+
'a' + " banana" // => "a banana"
58+
"banana " + 'a' // => "banana a"
59+
```
60+
61+
~~~~exercism/caution
62+
Becareful _not_ to use `+` to join two `char`s together to form a `String`!
63+
Adding two `char`s this way gives an `int`, _not_ a `String`!
64+
For example:
65+
66+
```java
67+
'b' + 'c';
68+
// => 197 (not the String "bc")
69+
```
70+
71+
This is because Java promotes the `char` to an `int` (see [4.2 Primitive Types and Values ][jls-primitives] of the [Java Language Specification][jls-main]).
72+
73+
[jls-main]: https://docs.oracle.com/javase/specs/jls/se21/html/
74+
[jls-primitives]: https://docs.oracle.com/javase/specs/jls/se21/html/jls-4.html#jls-4.2
75+
~~~~
76+
77+
However, when there are many characters to be added, it can be more efficient to use a `StringBuilder` instead:
78+
79+
```java
80+
StringBuilder builder = new StringBuilder();
81+
builder.append('a');
82+
builder.append('b');
83+
builder.append('c');
984

10-
`char`s are sometimes used in conjunction with a `StringBuilder` object.
11-
This object has methods that allow a string to be constructed character by character and manipulated.
12-
At the end of the process `toString` can be called on it to output a complete string.
85+
String builtString = builder.toString();
86+
// => abc
87+
```

concepts/chars/links.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
[
22
{
3-
"url":"https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html",
3+
"url":"https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Character.html",
44
"description":"javadoc"
55
},
66
{
7-
"url": "https://docs.oracle.com/javase/tutorial/i18n/text/unicode.html",
8-
"description": "unicode"
7+
"url": "https://dev.java/learn/numbers-strings/characters/",
8+
"description": "characters"
99
}
10-
11-
1210
]

config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
"chars"
106106
],
107107
"prerequisites": [
108+
"arrays",
108109
"strings"
109110
],
110111
"status": "active"

exercises/concept/squeaky-clean/.docs/introduction.md

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,88 @@
22

33
## Chars
44

5-
The Java `char` type represents the smallest addressable components of text.
6-
Multiple `char`s can comprise a string such as `"word"` or `char`s can be processed independently.
7-
Their literals have single quotes e.g. `'A'`.
5+
### chars
6+
7+
The Java `char` primitive type is a 16 bit representation of a single character.
8+
Multiple `char`s can comprise a string, such as `"word"`, or `char`s can be processed independently.
9+
A `char` literal is surrounded by single quotes (e.g. `'A'`).
10+
11+
```java
12+
char lowerA = 'a';
13+
char upperB = 'B';
14+
```
15+
16+
### Getting the `char`s of a `String`
17+
18+
The `String.toCharArray` method returns a String's chars as an array.
19+
As mentioned in arrays, you can use a `for` loop to iterate over the array.
20+
21+
```java
22+
String text = "Hello";
23+
char[] asArray = text.toCharArray();
24+
25+
for (char ch: asArray) {
26+
System.out.println(ch);
27+
}
28+
29+
// Outputs:
30+
// H
31+
// e
32+
// l
33+
// l
34+
// o
35+
```
36+
37+
### The Character class
838

939
There are many builtin library methods to inspect and manipulate `char`s.
1040
These can be found as static methods of the `java.lang.Character` class.
41+
Here are some examples:
42+
43+
```java
44+
Character.isWhitespace(' '); // true
45+
Character.isWhitespace('#'); // false
46+
47+
Character.isLetter('a'); // true
48+
Character.isLetter('3'); // false
49+
50+
Character.isDigit('6'); // true
51+
Character.isDigit('?'); // false
52+
```
53+
54+
### Adding a `char` to a `String`
55+
56+
The `+` operator can be used to add a `char` to a `String`.
57+
58+
```java
59+
'a' + " banana" // => "a banana"
60+
"banana " + 'a' // => "banana a"
61+
```
62+
63+
~~~~exercism/caution
64+
Becareful _not_ to use `+` to join two `char`s together to form a `String`!
65+
Adding two `char`s this way gives an `int`, _not_ a `String`!
66+
For example:
67+
68+
```java
69+
'b' + 'c';
70+
// => 197 (not the String "bc")
71+
```
72+
73+
This is because Java promotes the `char` to an `int` (see [4.2 Primitive Types and Values ][jls-primitives] of the [Java Language Specification][jls-main]).
74+
75+
[jls-main]: https://docs.oracle.com/javase/specs/jls/se21/html/
76+
[jls-primitives]: https://docs.oracle.com/javase/specs/jls/se21/html/jls-4.html#jls-4.2
77+
~~~~
78+
79+
However, when there are many characters to be added, it can be more efficient to use a `StringBuilder` instead:
80+
81+
```java
82+
StringBuilder builder = new StringBuilder();
83+
builder.append('a');
84+
builder.append('b');
85+
builder.append('c');
1186

12-
`char`s are sometimes used in conjunction with a `StringBuilder` object.
13-
This object has methods that allow a string to be constructed character by character and manipulated.
14-
At the end of the process `toString` can be called on it to output a complete string.
87+
String builtString = builder.toString();
88+
// => abc
89+
```

exercises/concept/squeaky-clean/.meta/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
],
55
"contributors": [
66
"jagdish-15",
7+
"kahgoh",
78
"manumafe98",
89
"mrDonoghue",
910
"sanderploegsma"

0 commit comments

Comments
 (0)