Skip to content

Commit bbac886

Browse files
Add concept enums with concept exercise logs-logs-logs (#2496)
* Add `enums` concept * Add concept exercise `logs-logs-logs` * Add enums to practice exercise configs
1 parent 529b75a commit bbac886

File tree

18 files changed

+740
-67
lines changed

18 files changed

+740
-67
lines changed

concepts/enums/.meta/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"blurb": "Enums are useful to create a predefined set of constants.",
3+
"authors": ["sanderploegsma"],
4+
"contributors": []
5+
}

concepts/enums/about.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# About
2+
3+
An _enum type_ is a special data type that enables for a variable to be a set of predefined constants.
4+
The variable must be equal to one of the values that have been predefined for it.
5+
Common examples include compass directions (values of `NORTH`, `SOUTH`, `EAST`, and `WEST`) and the days of the week.
6+
7+
Because they are constants, the names of an enum type's fields are in uppercase letters.
8+
9+
## Defining an enum type
10+
11+
In the Java programming language, you define an enum type by using the `enum` keyword.
12+
For example, you would specify a days-of-the-week enum type as:
13+
14+
```java
15+
public enum DayOfWeek {
16+
SUNDAY,
17+
MONDAY,
18+
TUESDAY,
19+
WEDNESDAY,
20+
THURSDAY,
21+
FRIDAY,
22+
SATURDAY
23+
}
24+
```
25+
26+
You should use enum types any time you need to represent a fixed set of constants.
27+
That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time - for example, the choices on a menu, command line flags, and so on.
28+
29+
## Using an enum type
30+
31+
Here is some code that shows you how to use the `DayOfWeek` enum defined above:
32+
33+
```java
34+
public class Shop {
35+
public String getOpeningHours(DayOfWeek dayOfWeek) {
36+
switch (dayOfWeek) {
37+
case MONDAY:
38+
case TUESDAY:
39+
case WEDNESDAY:
40+
case THURSDAY:
41+
case FRIDAY:
42+
return "9am - 5pm";
43+
case SATURDAY:
44+
return "10am - 4pm"
45+
case SUNDAY:
46+
return "Closed.";
47+
}
48+
}
49+
}
50+
```
51+
52+
```java
53+
var shop = new Shop();
54+
shop.getOpeningHours(DayOfWeek.WEDNESDAY);
55+
// => "9am - 5pm"
56+
```
57+
58+
## Adding methods and fields
59+
60+
Java programming language enum types are much more powerful than their counterparts in other languages.
61+
The `enum` declaration defines a _class_ (called an _enum type_).
62+
The enum class body can include methods and other fields:
63+
64+
```java
65+
public enum Rating {
66+
GREAT(5),
67+
GOOD(4),
68+
OK(3),
69+
BAD(2),
70+
TERRIBLE(1);
71+
72+
private final int numberOfStars;
73+
74+
Rating(int numberOfStars) {
75+
this.numberOfStars = numberOfStars;
76+
}
77+
78+
public int getNumberOfStars() {
79+
return this.numberOfStars;
80+
}
81+
}
82+
```
83+
84+
Calling the `getNumberOfStars` method on a member of the `Rating` enum type:
85+
86+
```java
87+
Rating.GOOD.getNumberOfStars();
88+
// => 4
89+
```
90+
91+
## Built-in methods
92+
93+
The compiler automatically adds some special methods when it creates an enum.
94+
95+
For example, they have a static `values` method that returns an array containing all of the values of the enum in the order they are declared:
96+
97+
```java
98+
for (DayOfWeek dayOfWeek : DayOfWeek.values()) {
99+
System.out.println(dayOfWeek.toString());
100+
}
101+
```
102+
103+
The snippet above would print the following:
104+
105+
```text
106+
SUNDAY
107+
MONDAY
108+
TUESDAY
109+
WEDNESDAY
110+
THURSDAY
111+
FRIDAY
112+
SATURDAY
113+
```
114+
115+
The compiler also adds a static `valueOf` method that returns an enum member based on its name:
116+
117+
```java
118+
DayOfWeek.valueOf("SUNDAY");
119+
// => DayOfWeek.SUNDAY
120+
```

concepts/enums/introduction.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Introduction
2+
3+
An _enum type_ is a special data type that enables for a variable to be a set of predefined constants.
4+
The variable must be equal to one of the values that have been predefined for it.
5+
Common examples include compass directions (values of `NORTH`, `SOUTH`, `EAST`, and `WEST`) and the days of the week.
6+
7+
Because they are constants, the names of an enum type's fields are in uppercase letters.
8+
9+
## Defining an enum type
10+
11+
In the Java programming language, you define an enum type by using the `enum` keyword.
12+
For example, you would specify a days-of-the-week enum type as:
13+
14+
```java
15+
public enum DayOfWeek {
16+
SUNDAY,
17+
MONDAY,
18+
TUESDAY,
19+
WEDNESDAY,
20+
THURSDAY,
21+
FRIDAY,
22+
SATURDAY
23+
}
24+
```
25+
26+
You should use enum types any time you need to represent a fixed set of constants.
27+
That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time - for example, the choices on a menu, command line flags, and so on.
28+
29+
## Using an enum type
30+
31+
Here is some code that shows you how to use the `DayOfWeek` enum defined above:
32+
33+
```java
34+
public class Shop {
35+
public String getOpeningHours(DayOfWeek dayOfWeek) {
36+
switch (dayOfWeek) {
37+
case MONDAY:
38+
case TUESDAY:
39+
case WEDNESDAY:
40+
case THURSDAY:
41+
case FRIDAY:
42+
return "9am - 5pm";
43+
case SATURDAY:
44+
return "10am - 4pm"
45+
case SUNDAY:
46+
return "Closed.";
47+
}
48+
}
49+
}
50+
```
51+
52+
```java
53+
var shop = new Shop();
54+
shop.getOpeningHours(DayOfWeek.WEDNESDAY);
55+
// => "9am - 5pm"
56+
```
57+
58+
## Adding methods and fields
59+
60+
Java programming language enum types are much more powerful than their counterparts in other languages.
61+
The `enum` declaration defines a _class_ (called an _enum type_).
62+
The enum class body can include methods and other fields:
63+
64+
```java
65+
public enum Rating {
66+
GREAT(5),
67+
GOOD(4),
68+
OK(3),
69+
BAD(2),
70+
TERRIBLE(1);
71+
72+
private final int numberOfStars;
73+
74+
Rating(int numberOfStars) {
75+
this.numberOfStars = numberOfStars;
76+
}
77+
78+
public int getNumberOfStars() {
79+
return this.numberOfStars;
80+
}
81+
}
82+
```
83+
84+
Calling the `getNumberOfStars` method on a member of the `Rating` enum type:
85+
86+
```java
87+
Rating.GOOD.getNumberOfStars();
88+
// => 4
89+
```

concepts/enums/links.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"url": "https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html",
4+
"description": "Java Tuturial on Enum Types"
5+
}
6+
]

0 commit comments

Comments
 (0)