Skip to content

Commit dc25fd1

Browse files
authored
Implement new concept exercise for list and generic-types (exercism#2007)
* Begin setting up the language-list exercise * Update implementation and config Start setting up the concept documentation as well. * Finish list and generic docs Also mark both unblocked exercises as active
1 parent 0ffaadc commit dc25fd1

File tree

19 files changed

+676
-8
lines changed

19 files changed

+676
-8
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"blurb": "Generics types allow the compiler to enforce type restraints within a class or interface.",
3+
"authors": [
4+
"jmrunkle"
5+
],
6+
"contributors": []
7+
}

concepts/generic-types/about.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# About Generic Types
2+
3+
A **generic type** is a generic class or interface that is parameterized over types.
4+
This allows the compiler to enforce type safety on the class or interface.
5+
6+
Consider this non-generic `Container` interface:
7+
8+
```java
9+
class Container {
10+
private Object object;
11+
12+
public void set(Object object) { this.object = object; }
13+
public Object get() { return object; }
14+
}
15+
```
16+
17+
Since it accepts and returns Object types, it works with any non-primitive type.
18+
However, this comes at a cost because some code may call `get` expecting `Integer`s while other code calls `set` adding `String`s resulting in a runtime exception.
19+
20+
A **generic class** and **generic interface** have the following formats:
21+
22+
```java
23+
class ClassName<T1, T2, ..., Tn> { ... }
24+
interface InterfaceName<T1, T2, ..., Tn> { ... }
25+
```
26+
27+
The type parameter section, delimited by angle brackets (`<>`), following the class or interface name specifies the type parameters (also called type variables) `T1`, `T2`, ..., and `Tn`.
28+
These can be used inside the body of the class or interface to get generic type safety.
29+
30+
Here is the generic version of `Container`:
31+
32+
```java
33+
class Container<E> {
34+
private E object;
35+
36+
public void set(E object) { this.object = object; }
37+
public E get() { return object; }
38+
}
39+
```
40+
41+
When created, now we have to declare what type it holds and the compiler will enforce that constraint:
42+
43+
```java
44+
// empty <> can infer from context
45+
Container<String> stringContainer = new Container<>();
46+
// compiler knows this is a String, so it is allowed
47+
stringContainer.set("Some string");
48+
// no cast needed, compiler knows it is a String
49+
String result = stringContainer.get();
50+
// this causes a compiler error:
51+
stringContainer.set(42);
52+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Introduction to Generic Types
2+
3+
A **generic type** is a generic class or interface that is parameterized over types.
4+
This allows the compiler to enforce type safety on the class or interface.
5+
6+
Consider this non-generic `Container` interface:
7+
8+
```java
9+
class Container {
10+
private Object object;
11+
12+
public void set(Object object) { this.object = object; }
13+
public Object get() { return object; }
14+
}
15+
```
16+
17+
Since it accepts and returns Object types, it works with any non-primitive type.
18+
However, this comes at a cost because some code may call `get` expecting `Integer`s while other code calls `set` adding `String`s resulting in a runtime exception.
19+
20+
A **generic class** and **generic interface** have the following formats:
21+
22+
```java
23+
class ClassName<T1, T2, ..., Tn> { ... }
24+
interface InterfaceName<T1, T2, ..., Tn> { ... }
25+
```
26+
27+
The type parameter section, delimited by angle brackets (`<>`), following the class or interface name specifies the type parameters (also called type variables) `T1`, `T2`, ..., and `Tn`.
28+
These can be used inside the body of the class or interface to get generic type safety.
29+
30+
Here is the generic version of `Container`:
31+
32+
```java
33+
class Container<E> {
34+
private E object;
35+
36+
public void set(E object) { this.object = object; }
37+
public E get() { return object; }
38+
}
39+
```
40+
41+
When created, now we have to declare what type it holds and the compiler will enforce that constraint:
42+
43+
```java
44+
// empty <> can infer from context
45+
Container<String> stringContainer = new Container<>();
46+
// compiler knows this is a String, so it is allowed
47+
stringContainer.set("Some string");
48+
// no cast needed, compiler knows it is a String
49+
String result = stringContainer.get();
50+
// this causes a compiler error:
51+
stringContainer.set(42);
52+
```

concepts/generic-types/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/generics/types.html",
4+
"description": "generics"
5+
}
6+
]

concepts/lists/.meta/config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"blurb": "TODO: add blurb for lists concept",
2+
"blurb": "Lists are an ordered collection that allows duplicates.",
33
"authors": [
4-
"mirkoperillo"
4+
"jmrunkle"
55
],
66
"contributors": []
77
}

concepts/lists/about.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,38 @@
1-
# About
1+
# About Lists
22

3-
TODO: add information on lists concept
3+
**Lists** are the ordered sequence collection in Java.
4+
Unlike arrays, a [`List`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html) can grow in size to accomodate any number of items.
5+
One standard implementation is the `ArrayList` which is backed by a re-sizable array.
6+
Another standard implementation is the `LinkedList` class which is backed by a doubly-linked list.
7+
8+
`List`s may be empty or hold any number of items (including duplicates).
9+
`List`s are a **generic interface** typed to indicate which type of objects they can contain.
10+
For example:
11+
12+
```java
13+
List<String> emptyListOfStrings = List.of();
14+
List<Integer> singleInteger = List.of(1);
15+
List<Boolean> threeBooleans = List.of(true, false, true);
16+
List<Object> listWithMulitipleTypes = List.of("hello", 1, true);
17+
```
18+
19+
`List`s have various helpful methods to add, remove, get, and check for an element to be present:
20+
21+
```java
22+
List<Character> vowels = new ArrayList<>(List.of('a', 'e', 'i', 'o', 'i', 'e', 'a'));
23+
int startingSize = vowels.size(); // 7
24+
vowels.add('u'); // vowels is now ['a', 'e', 'i', 'o', 'i', 'e', 'a', 'u']
25+
char a = vowels.get(0); // 'a'
26+
boolean hadI = vowels.remove('i'); // true and vowels is now ['a', 'e', 'o', 'i', 'e', 'a', 'u']
27+
boolean hasI = vowels.contains('i'); // true (still have one more left)
28+
```
29+
30+
The [`Collections`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collections.html) class also has some helpful utilities for dealing with collections like `List`:
31+
32+
```java
33+
List<Integer> numbers = new ArrayList<>(List.of(1, 5, 3, 2, 4));
34+
Collections.sort(numbers); // [1, 2, 3, 4, 5]
35+
Collections.reverse(numbers); // [5, 4, 3, 2, 1]
36+
Collections.fill(numbers, 42); // [42, 42, 42, 42, 42]
37+
List<Integer> fiveNines = Collections.nCopies(5, 9); // [9, 9, 9, 9, 9]
38+
```

concepts/lists/introduction.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1-
# Introduction
1+
# Introduction to Lists
22

3-
TODO: add introduction for lists concept
3+
**Lists** are the ordered sequence collection in Java.
4+
Unlike arrays, a [`List`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html) can grow in size to accomodate any number of items.
5+
One standard implementation is the `ArrayList` which is backed by a re-sizable array.
6+
Another standard implementation is the `LinkedList` class which is backed by a doubly-linked list.
7+
8+
`List`s may be empty or hold any number of items (including duplicates).
9+
`List`s are a **generic interface** typed to indicate which type of objects they can contain.
10+
For example:
11+
12+
```java
13+
List<String> emptyListOfStrings = List.of();
14+
List<Integer> singleInteger = List.of(1);
15+
List<Boolean> threeBooleans = List.of(true, false, true);
16+
List<Object> listWithMulitipleTypes = List.of("hello", 1, true);
17+
```
18+
19+
`List`s have various helpful methods to add, remove, get, and check for an element to be present:
20+
21+
```java
22+
List<Character> vowels = new ArrayList<>(List.of('a', 'e', 'i', 'o', 'i', 'e', 'a'));
23+
int startingSize = vowels.size(); // 7
24+
vowels.add('u'); // vowels is now ['a', 'e', 'i', 'o', 'i', 'e', 'a', 'u']
25+
char a = vowels.get(0); // 'a'
26+
boolean hadI = vowels.remove('i'); // true and vowels is now ['a', 'e', 'o', 'i', 'e', 'a', 'u']
27+
boolean hasI = vowels.contains('i'); // true (still have one more left)
28+
```

concepts/lists/links.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
[]
1+
[
2+
{
3+
"url": "https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html",
4+
"description": "javadoc"
5+
},
6+
{
7+
"url": "https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collections.html",
8+
"description": "collections"
9+
}
10+
]

config.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@
7070
],
7171
"status": "active"
7272
},
73+
{
74+
"slug": "karls-languages",
75+
"name": "Karl's Languages",
76+
"uuid": "1ad2ab97-df3e-4bc9-b0f6-e8dcc9eb10f1",
77+
"concepts": [
78+
"lists",
79+
"generic-types"
80+
],
81+
"prerequisites": [
82+
"arrays",
83+
"for-loops",
84+
"strings"
85+
],
86+
"status": "active"
87+
},
7388
{
7489
"slug": "squeaky-clean",
7590
"name": "Squeaky Clean",
@@ -132,7 +147,7 @@
132147
"lists",
133148
"strings"
134149
],
135-
"status": "wip"
150+
"status": "active"
136151
},
137152
{
138153
"slug": "cars-assemble",
@@ -1906,6 +1921,11 @@
19061921
"slug": "for-loops",
19071922
"name": "For Loops"
19081923
},
1924+
{
1925+
"uuid": "7de03b7d-ed91-4699-b275-96a6ab2e441c",
1926+
"slug": "generic-types",
1927+
"name": "Generic Types"
1928+
},
19091929
{
19101930
"uuid": "90776fcc-c6a7-4665-b5fe-5a8a0618358b",
19111931
"slug": "inheritance",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## 1. Define a function to check if the language list is empty
2+
3+
* Try using the [`isEmpty()`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#isEmpty()) method.
4+
5+
## 2. Define a function to add a language to the list
6+
7+
* Try using the [`add(E element)`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#add(E)) method.
8+
* Reminder: methods that return `void` do not need any `return` statements.
9+
10+
## 3. Define a function to remove a language from the list
11+
12+
* Try using the [`remove(Object o)`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#remove(java.lang.Object)) method.
13+
* Reminder: methods that return `void` do not need any `return` statements.
14+
15+
## 4. Define a function to return the first item in the list
16+
17+
* Try using the [`get(int index)`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#get(int)) method.
18+
19+
## 5. Define a function to return how many languages are in the list
20+
21+
* Try using the [`size()`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#size()) method.
22+
23+
## 6. Define a function to determine if a language is in the list
24+
25+
* Try using the [`contains(Object o)`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html#contains(java.lang.Object)) method.
26+
27+
## 7. Define a function to determine if the list is exciting
28+
29+
* Try using a [for-each loop](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html) through all of the elements, checking each one.
30+
* Alternatively, try using the `containsLanguage` method from the previous step.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Instructions
2+
3+
Karl wants to keep track of a list of languages to learn on Exercism's website.
4+
Karl needs to be able to add new languages, remove old ones and check if certain languages are in the list.
5+
It would be very exciting if Karl wants to learn Java or Kotlin!
6+
7+
## 1. Define a function to check if the language list is empty
8+
9+
Karl needs to know if his list of languages ever becomes empty so he can go find more to learn!
10+
Define a method called `isEmpty` which returns `true` if there are no languages in the list.
11+
12+
```java
13+
if (languageList.isEmpty()) {
14+
findMoreLanguagesToLearn();
15+
}
16+
```
17+
18+
## 2. Define a function to add a language to the list
19+
20+
Karl is looking forward to learning Kotlin and Python!
21+
Help Karl get started by defining a method called `addLanguage` which takes the language he wants to learn and adds it to the list.
22+
23+
```java
24+
languageList.addLanguage("Kotlin");
25+
languageList.addLanguage("Python");
26+
```
27+
28+
## 3. Define a function to remove a language from the list
29+
30+
Karl decided he does not want to learn Scala right now.
31+
Help Karl remove it from the list by defining a method called `removeLanguage` which takes the language he is removing and removes it from the list.
32+
33+
```java
34+
languageList.removeLanguage("Scala");
35+
```
36+
37+
## 4. Define a function to return the first item in the list
38+
39+
Karl wants to remember the first language he added to the list (that is still in the list).
40+
Define a method called `firstLanguage` that returns the first language in the list.
41+
42+
```java
43+
String kotlin = languageList.firstLanguage(); // "Kotlin"
44+
```
45+
46+
## 5. Define a function to return how many languages are in the list
47+
48+
Karl needs to know how many languages he is trying to learn.
49+
Help Karl find the answer by defining a method called `count` which returns the number of languages in the list.
50+
51+
```java
52+
int two = languageList.count(); // 2
53+
```
54+
55+
## 6. Define a function to determine if a language is in the list
56+
57+
Karl is trying to remember if he wanted to learn Python or Ruby.
58+
Define a method called `containsLanguage` which takes the language he is asking about so Karl can find out!
59+
60+
```java
61+
boolean learningPython = languageList.containsLanguage("Python"); // true
62+
boolean learningRuby = languageList.containsLanguage("Ruby"); // false
63+
```
64+
65+
## 7. Define a function to determine if the list is exciting
66+
67+
If Karl wants to learn Java or Kotlin, that is very exciting!
68+
Define a method called `isExciting` that returns true if Karl wants to learn Java or Kotlin.
69+
70+
```java
71+
javaLanguageList.isExciting() // true
72+
neitherKotlinNorJavaLanguageList.isExciting() // false
73+
```

0 commit comments

Comments
 (0)