X Tutup
Skip to content

Commit a7cf840

Browse files
AzumixErikSchierboom
authored andcommitted
Implement concept exercise switch-statement
* Initial setup * case-statement add introduction Just added the introduction.md file content. * case-statement add instruction.md Added instruction.md content * Add exercise case-statement Add base file for implementation Add implemented file for reference (both enhanced and traditionnal switch Add tests * Add exercise case-statement documentation Refresh instruction.md Refresh hints.md Refresh introduction.md * Merge with master * Run Prettier * Implement concept exercise case-statement exercism#1963 Run Prettier and link to issue. * Update languages/exercises/concept/case-statement/.docs/hints.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/.docs/instructions.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/.docs/hints.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/.docs/introduction.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/.docs/introduction.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/.docs/introduction.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/src/main/PlayAnalyzer.java Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Add exercise switch statement rename folder Rename folder from case-statement to switch-statement * Add exercise update hints.md Update hints.md add ressources for break and default statement. * Initial setup * case-statement add introduction Just added the introduction.md file content. * case-statement add instruction.md Added instruction.md content * Add exercise case-statement Add base file for implementation Add implemented file for reference (both enhanced and traditionnal switch Add tests * Add exercise case-statement documentation Refresh instruction.md Refresh hints.md Refresh introduction.md * Merge with master * Implement concept exercise case-statement exercism#1963 Run Prettier and link to issue. * Update languages/exercises/concept/case-statement/.docs/hints.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/.docs/instructions.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/.docs/hints.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/.docs/introduction.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/.docs/introduction.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/.docs/introduction.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/src/main/PlayAnalyzer.java Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Add exercise switch statement rename folder Rename folder from case-statement to switch-statement * Add exercise update hints.md Update hints.md add ressources for break and default statement. * Add exercise case-statement update instruction Update source, reference and instructions.md * Add exercice case-statement add exemple instructions.md * Add exercise case-statement update tests Update tests : - Add @ignore("remove to run test") on all but first - Change the order to reflect the exercise progression * Add exercises case-statement update tests Update tests : - Update in order to use the assertThat syntax * Correct stuff * Run Prettier * Update instructions and introduction * add exercise switch-statement update docs Update documentation and reference implemtation. Reference implementation: - choose between the traditionnal and enhanced switch implementation (traditionnal was choose) Documentation: - update the after.md document in order to explain the enhanced switch. * Update languages/exercises/concept/switch-statement/.meta/src/reference/PlayAnalyzer.java Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/switch-statement/.meta/src/reference/PlayAnalyzer.java Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/switch-statement/.meta/src/reference/PlayAnalyzer.java Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/switch-statement/.docs/introduction.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/switch-statement/.docs/after.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/switch-statement/.docs/after.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update introduction.md * Update after.md Update after.md remove references to exercise. * Update after.md Update after.md : - Add introduction.md in after.md - Make switch expression and statement difference more clear - Rework some exemple * Remove gradle stuff Remove gradle/wrapper, gradlew, gradlew.bat * Add exercise switch-statement Add entry in config.json and add entry in gradle file * Try to solve gradle test * Initial setup * case-statement add introduction Just added the introduction.md file content. * case-statement add instruction.md Added instruction.md content * Add exercise case-statement Add base file for implementation Add implemented file for reference (both enhanced and traditionnal switch Add tests * Add exercise case-statement documentation Refresh instruction.md Refresh hints.md Refresh introduction.md * Add exercise case-statement update instruction Update source, reference and instructions.md * Add exercice case-statement add exemple instructions.md * Add exercise case-statement update tests Update tests : - Add @ignore("remove to run test") on all but first - Change the order to reflect the exercise progression * Add exercises case-statement update tests Update tests : - Update in order to use the assertThat syntax * Initial setup * Merge with master * Implement concept exercise case-statement exercism#1963 Run Prettier and link to issue. * Update languages/exercises/concept/case-statement/.docs/hints.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/.docs/instructions.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/.docs/hints.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/.docs/introduction.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/.docs/introduction.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/exercises/concept/case-statement/.docs/introduction.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Add exercise update hints.md Update hints.md add ressources for break and default statement. * Update instructions and introduction * Update languages/exercises/concept/switch-statement/.docs/introduction.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update introduction.md * Remove gradle stuff Remove gradle/wrapper, gradlew, gradlew.bat * Remove case-statement * Add exercise switch-statement solve gralde check Solve gradle check issue by resolving a typo Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com>
1 parent b841ac3 commit a7cf840

File tree

10 files changed

+347
-0
lines changed

10 files changed

+347
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
Like an _if/else_ statement, a `switch` statement allow you to change the flow of the program by conditionally executing code. The difference is that a `switch` statement can only compare the value of a primitive or string expression against pre-defined constant values.
2+
3+
Some keywords are usefull when using a switch statement.
4+
5+
- `switch` : this keyword allow you to declare the structure of the switch. It his follow by the expression or the variable that will make the result change.
6+
- `case` : you will use this one to declare the differents possibilties for the result.
7+
- `break` : the `break` keyword is very usefull in order to stop the execution of the switch at the end of the wanted flow. If you forget it, the program will continue and may lead to unexpected results.
8+
- `default` : as it's name says use it as a default result when no other case matchs your expression's result.
9+
10+
At their simplest they test a primitive or string expression and make a decision based on its value. For example:
11+
12+
```java
13+
String direction = getDirection();
14+
switch (direction) {
15+
case "left":
16+
goLeft();
17+
break;
18+
case "right":
19+
goRight();
20+
break;
21+
default:
22+
//otherwise
23+
markTime();
24+
break;
25+
}
26+
```
27+
28+
Starting with Java 14 (available as a preview before in Java 12 and 13) it is possible to use the "enhanced" switch implementation.
29+
30+
1. You have the possiblity to assign multiple value in a sigle case.
31+
In the traditional switch-statement you can use fall-throught. In the following example `case 1` and `case 3` will execute the same stuff. This is done by `case 1` not using the `break` keyword.
32+
33+
```java
34+
switch (number) {
35+
case 1:
36+
case 3:
37+
//do same stuff
38+
break;
39+
case 2:
40+
//do different stuff
41+
break;
42+
(...)
43+
}
44+
```
45+
46+
In the enhanced `switch expression` you can directly assign multiple value to a `case`.
47+
Look at the following exemple :
48+
49+
```java
50+
switch (number) {
51+
case 1, 3:
52+
//do stuff
53+
break;
54+
case 2:
55+
//do other stuff
56+
break;
57+
(...)
58+
}
59+
```
60+
61+
2. You can now write a `switch-statement` or a `switch expression`. What is the difference ?
62+
Basicly a statement is expecting some strict logic where an expression can return a value.
63+
Instead of :
64+
65+
```java
66+
String result = "";
67+
switch (expression) {
68+
case "bob":
69+
result = "bob;
70+
break;
71+
(...)
72+
}
73+
```
74+
75+
You can do :
76+
77+
```java
78+
String result = switch(expression) {
79+
case "bob":
80+
yield "bob";
81+
(...)
82+
}
83+
```
84+
85+
The [`yield`][yield-keyword] works like a `return` except it's for switch expression. As `yield` terminates the expression `break` is not needed here.
86+
87+
3. Another difference between _switch statements_ and _switch expressions_: in _switch expressions_ you _**MUST**_ cover all cases. Either by having a `case` for all possible values or using a `default` case.
88+
89+
4. You can use `->` instead of `:`. The `->` allow you to not include the `break` keyword. Both notation can be used but in a switch you have to stick with only one.
90+
91+
```java
92+
switch(expression) {
93+
case 1 -> yield "one"
94+
case 2 -> yield "two"
95+
default: yield "other number" // Removing this will result in a compile error
96+
}
97+
```
98+
99+
5. The scope. Traditionnals `switch` can lead to some unexected behavior because of it's scope as there is only one scope for the whole `switch`.
100+
101+
```java
102+
switch(expression) {
103+
case 1:
104+
String message = "something";
105+
break;
106+
case 2:
107+
String message = "anything";
108+
break;
109+
(...)
110+
}
111+
```
112+
113+
This exemple is not working because message is declared twice in the `switch`.
114+
It could be solved using :
115+
116+
```java
117+
switch(expression) {
118+
case 1: {
119+
String message = "something";
120+
break;
121+
}
122+
case 2: {
123+
String message = "anything";
124+
break;
125+
}
126+
(...)
127+
}
128+
```
129+
130+
As the `{}` is delimiting the scope of the `case`. However it's not intuitive because `{}` are not mandatory.
131+
However if you use the new `->` notation it must be followed by either : a single statement/expression, a `throw` statement or a `{}` block. No more confussion!
132+
133+
You can find more information on enhanced switch [here][switch1], [here][switch2] and on the [oracle documentation][oracle-doc].
134+
135+
[yield-keyword]: https://www.codejava.net/java-core/the-java-language/yield-keyword-in-java
136+
[switch1]: https://www.vojtechruzicka.com/java-enhanced-switch/
137+
[switch2]: https://howtodoinjava.com/java14/switch-expressions/
138+
[oracle-doc]: https://docs.oracle.com/en/java/javase/13/language/switch-expressions.html
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## General
2+
3+
- The [`switch` statement documentation][switch-statement] provides an introduction to `switch` statements.
4+
5+
### 1. Output descriptions of the players based on their shirt number
6+
7+
- The [`break`][break] statement is useful here.
8+
9+
### 2. Raise an alert if an unknown shirt number is encountered
10+
11+
- The [`default`][default] statement can be used here.
12+
13+
[switch-statement]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
14+
[break]: https://www.w3schools.com/java/ref_keyword_break.asp
15+
[default]: https://www.w3schools.com/java/ref_keyword_default.asp
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
You are developing a system to help the staff of a football/soccer club's web site report on matches. Data is received from a variety of sources and piped into a single stream after being cleaned up.
2+
3+
### 1. Output descriptions of the players based on their shirt number
4+
5+
The team only ever plays a 4-3-3 formation and has never agreed with the 1965 change to the rules allowing for substitutions, never mind enlarged squads.
6+
7+
The player descriptions are as follows:
8+
9+
```
10+
1 -> "goalie"
11+
2 -> "left back"
12+
3 & 4 "center back"
13+
5 -> "right back"
14+
6, 7, & 8 -> "midfielder"
15+
9 -> "left wing"
16+
10 -> "striker"
17+
11 -> "right wing"
18+
```
19+
20+
Implement the static `PlayAnalyzer.onField()` method to output a player description based on their shirt number.
21+
22+
```java
23+
PlayAnalyzer.onField(10);
24+
// => "striker"
25+
```
26+
27+
### 2. Raise an alert if an unknown shirt number is encountered
28+
29+
Modify the `PlayAnalyzer.onField()` method to throw an `IllegalArgumentException` when a shirt number outside the range 1-11 is processed.
30+
31+
```java
32+
PlayAnalyer.onField(13);
33+
// => Throw IllegalArgumentException
34+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Like an _if/else_ statement, a `switch` statement allow you to change the flow of the program by conditionally executing code. The difference is that a `switch` statement can only compare the value of a primitive or string expression against pre-defined constant values.
2+
3+
Some keywords are usefull when using a switch statement.
4+
5+
- `switch` : this keyword allow you to declare the structure of the switch. It his follow by the expression or the variable that will make the result change.
6+
- `case` : you will use this one to declare the differents possibilties for the result.
7+
- `break` : the `break` keyword is very usefull in order to stop the execution of the switch at the end of the wanted flow. If you forget it, the program will continue and may lead to unexpected results.
8+
- `default` : as it's name says use it as a default result when no other case matchs your expression's result.
9+
10+
At their simplest they test a primitive or string expression and make a decision based on its value. For example:
11+
12+
```java
13+
String direction = getDirection();
14+
switch (direction) {
15+
case "left":
16+
goLeft();
17+
break;
18+
case "right":
19+
goRight();
20+
break;
21+
default:
22+
//otherwise
23+
markTime();
24+
break;
25+
}
26+
```

exercises/concept/switch-statement/.meta/config.json

Whitespace-only changes.

exercises/concept/switch-statement/.meta/design.md

Whitespace-only changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
public class PlayAnalyzer {
2+
public static String onField(int shirtNum) {
3+
String playerDescription = "";
4+
switch (shirtNum) {
5+
case 1:
6+
playerDescription = "goalie";
7+
break;
8+
case 2:
9+
playerDescription = "left back";
10+
break;
11+
case 5:
12+
playerDescription = "right back";
13+
break;
14+
case 3:
15+
case 4:
16+
playerDescription = "center back";
17+
break;
18+
case 6:
19+
case 7:
20+
case 8:
21+
playerDescription = "midfielder";
22+
break;
23+
case 9:
24+
playerDescription = "left wing";
25+
break;
26+
case 11:
27+
playerDescription = "right wing";
28+
break;
29+
case 10:
30+
playerDescription = "striker";
31+
break;
32+
default:
33+
throw new IllegalArgumentException();
34+
}
35+
return playerDescription;
36+
}
37+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
compileJava.options.encoding = "UTF-8"
6+
compileTestJava.options.encoding = "UTF-8"
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
testCompile "junit:junit:4.13"
14+
testImplementation "org.assertj:assertj-core:3.15.0"
15+
}
16+
17+
test {
18+
testLogging {
19+
exceptionFormat = 'short'
20+
showStandardStreams = true
21+
events = ["passed", "failed", "skipped"]
22+
}
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class PlayAnalyzer {
2+
public static String onField(int shirtNum) {
3+
throw new UnsupportedOperationException("Please implement the (static) PlayAnalyzer.onField() method");
4+
}
5+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import org.junit.Ignore;
2+
import org.junit.Test;
3+
4+
import static org.assertj.core.api.Assertions.*;
5+
6+
public class PlayAnalyzerTest {
7+
8+
@Test
9+
public void test_goal() {
10+
assertThat(PlayAnalyzer.onField(1).contentEquals("goalie"));
11+
}
12+
13+
@Test
14+
@Ignore("Remove to run test")
15+
public void test_left_back() {
16+
assertThat(PlayAnalyzer.onField(2).contentEquals("left back"));
17+
}
18+
19+
@Test
20+
@Ignore("Remove to run test")
21+
public void test_right_back() {
22+
assertThat(PlayAnalyzer.onField(5).contentEquals("right back"));
23+
}
24+
25+
@Test
26+
@Ignore("Remove to run test")
27+
public void test_center_back() {
28+
assertThat(PlayAnalyzer.onField(3).contentEquals("center back"));
29+
assertThat(PlayAnalyzer.onField(4).contentEquals("center back"));
30+
}
31+
32+
@Test
33+
@Ignore("Remove to run test")
34+
public void test_midfielder() {
35+
assertThat(PlayAnalyzer.onField(6).contentEquals("midfielder"));
36+
assertThat(PlayAnalyzer.onField(7).contentEquals("midfielder"));
37+
assertThat(PlayAnalyzer.onField(8).contentEquals("midfielder"));
38+
}
39+
40+
@Test
41+
@Ignore("Remove to run test")
42+
public void test_left_wing() {
43+
assertThat(PlayAnalyzer.onField(9).contentEquals("left wing"));
44+
}
45+
46+
@Test
47+
@Ignore("Remove to run test")
48+
public void test_striker() {
49+
assertThat(PlayAnalyzer.onField(10).contentEquals("striker"));
50+
}
51+
52+
@Test
53+
@Ignore("Remove to run test")
54+
public void test_right_wing() {
55+
assertThat(PlayAnalyzer.onField(11).contentEquals("right wing"));
56+
}
57+
58+
@Test(expected = IllegalArgumentException.class)
59+
@Ignore("Remove to run test")
60+
public void test_exception() {
61+
PlayAnalyzer.onField(13);
62+
}
63+
64+
@Test(expected = IllegalArgumentException.class)
65+
@Ignore("Remove to run test")
66+
public void test_exception_negative_number() {
67+
PlayAnalyzer.onField(-1);
68+
}
69+
}

0 commit comments

Comments
 (0)
X Tutup