Skip to content

Commit 7f7eda1

Browse files
TalesDiasSilvamirkoperillo
committed
Implement concept exercise conditionals
* implemented exercise and tests * changed docs * hotfix: design.md formatting * style review * add missing config entry * fix json Co-authored-by: Silva <silva@localhost.localdomain> Co-authored-by: mirkoperillo <mirko.perillo@gmail.com>
1 parent 198f7a2 commit 7f7eda1

10 files changed

Lines changed: 747 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
## Logical Operators
2+
3+
Java supports three [logical operators][logical-operators] `&&` (AND), `||` (OR), and `!` (NOT).
4+
5+
## If statement
6+
7+
The underlying type of any conditional operation is the `boolean` type, which can have the value of `true` or `false`. Conditionals are often used as flow control mechanisms to check for various conditions. For checking a particular case an [`if` statement][if-statement] can be used, which executes its code if the underlying condition is `true` like this:
8+
9+
```java
10+
int val;
11+
12+
if(val == 9) {
13+
// conditional code
14+
}
15+
```
16+
17+
In scenarios involving more than one case many `if` statements can be chained together using the `else if` and `else` statements.
18+
19+
```java
20+
if(val == 10) {
21+
// conditional code
22+
} else if(val == 17) {
23+
// conditional code
24+
} else {
25+
// executes when val is different from 10 and 17
26+
}
27+
```
28+
29+
## Switch statement
30+
31+
Java also provides a [`switch` statement][switch-statement] for scenarios with multiple options. It can be used to switch on a variable's content as a replacement for simple `if ... else if` statements. A switch statement can have a `default` case which is executed if no other case applies.
32+
33+
In Java you can't use any type as the value in a `switch`, only integer and enumerated data types, plus the `String` class are allowed.
34+
35+
If there are three or more cases in a single `if` (e.g. `if ... else if ... else`), it should be replaced by a `switch` statement. A `switch` with a single case should be replaced by an `if` statement.
36+
37+
```java
38+
int val;
39+
40+
// switch statement on variable content
41+
switch(val) {
42+
case 1:
43+
// conditional code
44+
break;
45+
case 2: case 3: case 4:
46+
// conditional code
47+
break;
48+
default:
49+
// if all cases fail
50+
break;
51+
}
52+
```
53+
54+
Note: Make sure the expression in the switch statement is not null, otherwise a NullPointerException will be thrown
55+
56+
To learn more about this topic it is recommended to check these sources:
57+
58+
- [A refresh of Ternary operators][example-ternary]
59+
- [If/Else datailed with flowcharts][example-ifelse-flowcharts]
60+
- [Switch examples][example-switch]
61+
62+
[logical-operators]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
63+
[if-statement]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
64+
[switch-statement]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
65+
[example-ifelse-flowcharts]: https://www.javatpoint.com/java-if-else
66+
[example-ternary]: https://www.programiz.com/java-programming/ternary-operator
67+
[example-switch]: https://www.geeksforgeeks.org/switch-statement-in-java/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## General
2+
3+
Conditionals are used to check for certain conditions and/or criteria. The most basic way of performing a conditional operation is using a single `if` statement.
4+
5+
## 1. Calculate the score of any given card.
6+
7+
The `parseCard` function should take the `card` string (e.g. `ace`) and turn it into its value (e.g. 11).
8+
9+
- Use a big [`switch` statement][switch-statement] on the `card` variable.
10+
- King, Queen, Jack and 10 can be handled with a single case.
11+
- The switch can have a `default` case. In any case the function should return `0` for unknown cards.
12+
13+
## 2. Determine if two cards make up a Blackjack.
14+
15+
`isBlackJack` checks if 2 cards have the combined value of 21.
16+
17+
- Should use the `parseCard` function to get the value for each card.
18+
- Should sum up the values of the 2 cards.
19+
- Should return `true` if the sum is equal to `21`.
20+
- No aditional statement is needed here. The result for the comparison can be returned.
21+
22+
## 3. Implement the decision logic for hand scores larger than 20 points.
23+
24+
As the `largeHand` function is only called for hands with a value larger than 20, there are only 2 different possible hands: A **BlackJack** with a total value of `21` and **2 Aces** with a total value of `22`.
25+
26+
- The function should check [if][if-statement] `isBlackJack` is `true` and return "P" otherwise.
27+
- If `isBlackJack` is `true`, the dealerScore needs to be checked for being lower than 10. [If][if-statement] it is lower, return "W" otherwise "S".
28+
29+
## 4. Implement the decision logic for hand scores with less than 21 points.
30+
31+
The `smallHand` function is only called if there are no Aces on the hand (`handScore` is less than 21).
32+
33+
- Implement every condition using [logical operators][logical-operators] if necessary:
34+
- [If][if-statement] your cards sum up to 17 or higher you should always _stand_.
35+
- [If][if-statement] your cards sum up to 11 or lower you should always _hit_.
36+
- [If][if-statement] your cards sum up to a value within the range [12, 16] you should always _stand_ if the dealer has a 6 or lower.
37+
- [If][if-statement] your cards sum up to a value within the range [12, 16] you should always _hit_ if the dealer has a 7 or higher.
38+
- (optional) Try to optimize the conditions:
39+
- Pull together the conditions for _stand_ into one.
40+
- Pull together the conditions for _hit_ into one.
41+
- Remove redundant parts of the conditions (e.g. `A || !A && B` can be `A || B`).
42+
43+
[logical-operators]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
44+
[if-statement]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
45+
[switch-statement]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
In this exercise we will simulate the first turn of a [Blackjack](https://en.wikipedia.org/wiki/Blackjack) game.
2+
3+
You will receive two cards and will be able to see the face up card of the dealer. All cards are represented using a string such as "ace", "king", "three", "two", etc. The values of each card are:
4+
5+
| card | value | card | value |
6+
| :---: | :---: | :---: | :---: |
7+
| ace | 11 | eight | 8 |
8+
| two | 2 | nine | 9 |
9+
| three | 3 | ten | 10 |
10+
| four | 4 | jack | 10 |
11+
| five | 5 | queen | 10 |
12+
| six | 6 | king | 10 |
13+
| seven | 7 | other | 0 |
14+
15+
**Note**: Commonly, aces can take the value of 1 or 11 but for simplicity we will assume that they can only take the value of 11.
16+
17+
Depending on your two cards and the card of the dealer, there is a strategy for the first turn of the game, in which you have the following options:
18+
19+
- Stand (S)
20+
- Hit (H)
21+
- Split (P)
22+
- Automatically win (W)
23+
24+
Although not optimal yet, you will follow the strategy your friend Alex has been developing, which is as follows:
25+
26+
Category: Large Hand
27+
28+
- If you have a pair of aces you must always split them.
29+
- If you have a Blackjack (two cards that sum up to a value of 21), and the dealer does not have an ace, a figure or a ten then you automatically win. If the dealer does have any of those cards then you'll have to stand and wait for the reveal of the other card.
30+
31+
Category: Small Hand
32+
33+
- If your cards sum up to 17 or higher you should always stand.
34+
- If your cards sum up to 11 or lower you should always hit.
35+
- If your cards sum up to a value within the range [12, 16] you should always stand unless the dealer has a 7 or higher, in which case you should always hit.
36+
37+
The overall logic has already been implemented. You have four tasks:
38+
39+
## 1. Calculate the score of any given card.
40+
41+
Implement a function to calculate the numerical value of a card given its name using coditionals.
42+
43+
```java
44+
parseCard("ace")
45+
// returns 11
46+
```
47+
48+
## 2. Determine if two cards make up a Blackjack.
49+
50+
Implement a function that returns `true` if two cards form a Blackjack, `false` otherwise.
51+
52+
```java
53+
isBlackjack("queen", "ace")
54+
// returns true
55+
```
56+
57+
## 3. Implement the decision logic for hand scores larger than 20 points.
58+
59+
Implement a function that returns the string representation of a decision given your cards. This function is only called if the `handScore` is larger than 20. It will receive 2 arguments: `isBlackJack` and `dealerScore`. It should implement the bulletpoints in the category "Large Hand" above.
60+
61+
```java
62+
isBlackJack = true
63+
dealerScore = 7
64+
largeHand(isBlackJack, dealerScore)
65+
// returns "W"
66+
```
67+
68+
## 4. Implement the decision logic for hand scores with less than 21 points.
69+
70+
Implement a function that returns the string representation of a decision given your cards. This function is only called if the `handScore` is less than 21. It will receive 2 arguments: `handScore` and `dealerScore`. It should implement the bulletpoints in the category "Small Hand" above.
71+
72+
```java
73+
handScore = 15
74+
dealerScore = 12
75+
SmallHand(handScore, dealerScore)
76+
// returns "H"
77+
```
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Logical Operators
2+
3+
Java supports the three logical operators `&&` (AND), `||` (OR), and `!` (NOT).
4+
5+
## If statement
6+
7+
The underlying type of any conditional operation is the `boolean` type, which can have the value of `true` or `false`. Conditionals are often used as flow control mechanisms to check for various conditions. For checking a particular case an `if` statement can be used, which executes its code if the underlying condition is `true` like this:
8+
9+
```java
10+
int val;
11+
12+
if(val == 9) {
13+
// conditional code
14+
}
15+
```
16+
17+
In scenarios involving more than one case many `if` statements can be chained together using the `else if` and `else` statements.
18+
19+
```java
20+
if(val == 10) {
21+
// conditional code
22+
} else if(val == 17) {
23+
// conditional code
24+
} else {
25+
// executes when val is different from 10 and 17
26+
}
27+
```
28+
29+
## Switch statement
30+
31+
Java also provides a `switch` statement for scenarios with multiple options.
32+
33+
```java
34+
int val;
35+
36+
// switch statement on variable content
37+
switch(val) {
38+
case 1:
39+
// conditional code
40+
break;
41+
case 2: case 3: case 4:
42+
// conditional code
43+
break;
44+
default:
45+
// if all cases fail
46+
break;
47+
}
48+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"authors": [
3+
{
4+
"github_username": "TalesDias",
5+
"exercism_username": "TalesDias"
6+
}
7+
],
8+
"forked_from": ["go/conditionals"]
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Goal
2+
3+
The goal of this exercise is to teach the student the basics of the Concept of Conditionals in Java.
4+
5+
## Learning objectives
6+
7+
- Know of the existence of the `bool` type.
8+
- Know how to define an if statement.
9+
- Know to avoid else after return.
10+
- Know how to define a switch statement.
11+
12+
## Out of scope
13+
14+
- iterations
15+
- continue (as there are no iterations)
16+
17+
## Concepts
18+
19+
- `booleans`
20+
- `conditionals-if`
21+
- `conditionals-switch`
22+
23+
## Prequisites
24+
25+
This exercise's prerequisites Concepts are:
26+
27+
- `basic-strings`
28+
- `numbers`
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
public class Blackjack {
2+
3+
public int parseCard(String card) {
4+
switch (card) {
5+
case "ace":
6+
return 11;
7+
8+
case "two":
9+
return 2;
10+
11+
case "three":
12+
return 3;
13+
14+
case "four":
15+
return 4;
16+
17+
case "five":
18+
return 5;
19+
20+
case "six":
21+
return 6;
22+
23+
case "seven":
24+
return 7;
25+
26+
case "eight":
27+
return 8;
28+
29+
case "nine":
30+
return 9;
31+
32+
case "ten": case "jack": case "queen": case "king":
33+
return 10;
34+
35+
default:
36+
return 0;
37+
}
38+
}
39+
40+
public boolean isBlackjack(String card1, String card2) {
41+
return parseCard(card1) + parseCard(card2) == 21;
42+
}
43+
44+
public String largeHand(boolean isBlackjack, int dealerScore) {
45+
if (isBlackjack) {
46+
if (dealerScore < 10) {
47+
return "W";
48+
}
49+
return "S";
50+
}
51+
return "P";
52+
}
53+
54+
public String smallHand(int handScore, int dealerScore) {
55+
if (handScore >= 17 || (handScore >= 12 && dealerScore < 7)) {
56+
return "S";
57+
}
58+
return "H";
59+
}
60+
61+
// FirstTurn returns the semi-optimal decision for the first turn, given the cards of the player and the dealer.
62+
// This function is already implemented and does not need to be edited. It pulls the other functions together in a
63+
// complete decision tree for the first turn.
64+
public String firstTurn(String card1, String card2, String dealerCard) {
65+
int handScore = parseCard(card1) + parseCard(card2);
66+
int dealerScore = parseCard(dealerCard);
67+
68+
if (20 < handScore) {
69+
return largeHand(isBlackjack(card1, card2), dealerScore);
70+
} else {
71+
return smallHand(handScore, dealerScore);
72+
}
73+
}
74+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.13"
11+
testImplementation "org.assertj:assertj-core:3.15.0"
12+
}
13+
14+
test {
15+
testLogging {
16+
exceptionFormat = 'short'
17+
showStandardStreams = true
18+
events = ["passed", "failed", "skipped"]
19+
}
20+
}

0 commit comments

Comments
 (0)