Skip to content

Commit b038f95

Browse files
ystrommErikSchierboom
authored andcommitted
Java bird watcher
* Renames arrays to bird-count * Copies about files from arrays to for-loops and foreach-loops. * Renames arrays to bird-watcher. * Adds about files for for and foreach loops. * Fixed file names in config.json. * Update languages/concepts/foreach-loops/about.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Update languages/concepts/for-loops/about.md Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com> * Improved texts for for and foreach loops. * Updated authors. Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com>
1 parent ccf3de5 commit b038f95

14 files changed

Lines changed: 169 additions & 48 deletions

File tree

concepts/for-loops/about.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
The [for loop][for-loop] provides a mechanism to execute a group of statements repeatedly.
2+
The loop consists of four parts:
3+
4+
````java
5+
for (initialization; test; update)
6+
{
7+
// body
8+
}
9+
````
10+
11+
12+
The initialization sets an initial state for the loop.
13+
Typically it declares and sets a variable used in the test expression and update statement.
14+
For example:
15+
16+
````java
17+
int i=1
18+
````
19+
20+
The test expression tests if the loop should execute the body
21+
and execute the update statement.
22+
23+
If the test evaluates to true the body and the update expression will be executed.
24+
25+
If the expression evaluates to false neither the body nor the update statement will be executed and execution continues after the loop.
26+
An example of a test can be:
27+
28+
````java
29+
i <= 10
30+
````
31+
32+
After executing the loop body, the update expression increments/decrements the loop variable by some value.
33+
Example:
34+
````java
35+
i++
36+
````
37+
38+
A for loop executing over each element in an array can look like this:
39+
```java
40+
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
41+
42+
for (int i = 0; i<vowels.length; i++) {
43+
// Output the vowel
44+
System.out.print(vowels[i]);
45+
}
46+
47+
// => aeiou
48+
```
49+
50+
A `for` loop does have some advantages over a `foreach` loop:
51+
52+
- You can start or stop at the index you want.
53+
- You can use any (boolean) termination condition you want.
54+
- You can skip elements by customizing the incrementing of the loop variable.
55+
- You can process collections from back to front by counting down.
56+
- You can use `for` loops in scenarios that don't involve collections.
57+
58+
[for-loop]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

concepts/for-loops/links.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"link": "https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html",
4+
"description": "for-loop"
5+
}
6+
]

concepts/foreach-loops/about.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
The [foreach loop][foreach-loop] provides a mechanism for executing a statement group for each element in a collection or array.
2+
3+
Syntax:
4+
`````java
5+
for(declaration: collection) {
6+
// body
7+
}
8+
`````
9+
10+
The declaration part declares the variable used to hold the values assigned from the collection:
11+
12+
The collection is an array or a collection holding the values that will be assigned to the loop variable.
13+
14+
The body contains the statements that will be executed once for each value in the collection.
15+
16+
Example:
17+
18+
```java
19+
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
20+
21+
for(char vowel:vowels) {
22+
// Output the vowel
23+
System.out.print(vowel);
24+
}
25+
26+
// => aeiou
27+
```
28+
29+
Generally a `foreach` loop is preferrable over a `for` loop for the following reasons:
30+
31+
- A `foreach` loop is guaranteed to iterate over _all_ values. With a `for` loop, it is easy to miss elements, for example due to an off-by-one error.
32+
- A `foreach` loop is more _declarative_, your code is communicating _what_ you want it to do, instead of a `for` loop that communicates _how_ you want to do it.
33+
- A `foreach` loop is foolproof, whereas with `for` loops it is easy to have an off-by-one error.
34+
- A `foreach` loop works on all collection types, including those that don't support using an indexer to access elements.
35+
36+
To guarantee that a `foreach` loop will iterate over _all_ values, the compiler will not allow updating of a collection within a `foreach` loop:
37+
38+
```java
39+
char[] vowels = ['a', 'e', 'i', 'o', 'u'];
40+
41+
for(char vowel:vowels) {
42+
vowels = ['Y']; // This would result in a compiler error
43+
}
44+
```
45+
46+
[foreach-loop]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
47+

concepts/foreach-loops/links.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"link": "https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html",
4+
"description": "foreach-loop"
5+
}
6+
]

exercises/concept/arrays/.meta/config.json

Lines changed: 0 additions & 13 deletions
This file was deleted.
File renamed without changes.

exercises/concept/arrays/.docs/instructions.md renamed to exercises/concept/bird-watcher/.docs/instructions.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,66 @@ You have six tasks, all dealing with the numbers of birds that visited your gard
44

55
## 1. Check what the counts were last week
66

7-
For comparison purposes, you always keep a copy of last week's counts nearby, which were: 0, 2, 5, 3, 7, 8 and 4. Implement the `BirdCount.getLastWeek()` method that returns last week's counts:
7+
For comparison purposes, you always keep a copy of last week's counts nearby, which were: 0, 2, 5, 3, 7, 8 and 4. Implement the `BirdWatcher.getLastWeek()` method that returns last week's counts:
88

99
```java
10-
BirdCount.getLastWeek();
10+
BirdWatcher.getLastWeek();
1111
// => [0, 2, 5, 3, 7, 8, 4]
1212
```
1313

1414
## 2. Check how many birds visited today
1515

16-
Implement the `BirdCount.getToday()` method to return how many birds visited your garden today. The bird counts are ordered by day, with the first element being the count of the oldest day, and the last element being today's count.
16+
Implement the `BirdWatcher.getToday()` method to return how many birds visited your garden today. The bird counts are ordered by day, with the first element being the count of the oldest day, and the last element being today's count.
1717

1818
```java
1919
int[] birdsPerDay = { 2, 5, 0, 7, 4, 1 };
20-
BirdCount birdCount = new BirdCount(birdsPerDay);
20+
BirdWatcher birdCount = new BirdWatcher(birdsPerDay);
2121
birdCount.getToday();
2222
// => 1
2323
```
2424

2525
## 3. Increment today's count
2626

27-
Implement the `BirdCount.incrementDayCount()` method to increment today's count:
27+
Implement the `BirdWatcher.incrementDayCount()` method to increment today's count:
2828

2929
```java
3030
int[] birdsPerDay = { 2, 5, 0, 7, 4, 1 };
31-
BirdCount birdCount = new BirdCount(birdsPerDay);
31+
BirdWatcher birdCount = new BirdWatcher(birdsPerDay);
3232
birdCount.incrementDayCount();
3333
birdCount.getToday();
3434
// => 2
3535
```
3636

3737
## 4. Check if there was a day with no visiting birds
3838

39-
Implement the `BirdCount.hasDayWithoutBirds()` method that returns `true` if there was a day at which zero birds visited the garden; otherwise, return `false`:
39+
Implement the `BirdWatcher.hasDayWithoutBirds()` method that returns `true` if there was a day at which zero birds visited the garden; otherwise, return `false`:
4040

4141
```java
4242
int[] birdsPerDay = { 2, 5, 0, 7, 4, 1 };
43-
BirdCount birdCount = new BirdCount(birdsPerDay);
43+
BirdWatcher birdCount = new BirdWatcher(birdsPerDay);
4444
birdCount.hasDayWithoutBirds();
4545
// => true
4646
```
4747

4848
## 5. Calculate the number of visiting birds for the first number of days
4949

50-
Implement the `BirdCount.getCountForFirstDays()` method that returns the number of birds that have visited your garden from the start of the week, but limit the count to the specified number of days from the start of the week.
50+
Implement the `BirdWatcher.getCountForFirstDays()` method that returns the number of birds that have visited your garden from the start of the week, but limit the count to the specified number of days from the start of the week.
5151

5252
```java
5353
int[] birdsPerDay = { 2, 5, 0, 7, 4, 1 };
54-
BirdCount birdCount = new BirdCount(birdsPerDay);
54+
BirdWatcher birdCount = new BirdWatcher(birdsPerDay);
5555
birdCount.getCountForFirstDays(4);
5656
// => 14
5757
```
5858

5959
## 6. Calculate the number of busy days
6060

6161
Some days are busier that others. A busy day is one where five or more birds have visited your garden.
62-
Implement the `BirdCount.getBusyDays()` method to return the number of busy days:
62+
Implement the `BirdWatcher.getBusyDays()` method to return the number of busy days:
6363

6464
```java
6565
int[] birdsPerDay = { 2, 5, 0, 7, 4, 1 };
66-
BirdCount birdCount = new BirdCount(birdsPerDay);
66+
BirdWatcher birdCount = new BirdWatcher(birdsPerDay);
6767
birdCount.getBusyDays();
6868
// => 2
6969
```
File renamed without changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
{
4+
"github_username": "samuelteixeiras",
5+
"exercism_username": "samuelteixeiras"
6+
},
7+
{
8+
"github_username": "ystromm",
9+
"exercism_username": "ystromm"
10+
}
11+
],
12+
"editor": {
13+
"solution_files": ["src/main/java/BirdWatcher.java"],
14+
"test_files": ["src/test/java/BirdWatcherTest.java"]
15+
},
16+
"forked_from": ["csharp/arrays"]
17+
}
File renamed without changes.

0 commit comments

Comments
 (0)