Browse Source

binary-search: use generics

The binary-search tests force the practitioner to generify the class
but only one type (integer) was actually being used in the tests.
This should be changed so that more than one type is actually used, to
make it more clear that generics are necessary and to demonstrate how
generics is used.

As discussed in #230, move linked-list to one of the last difficulty 6
exercises, so that binary-seach is the first exercise that introduces
generics.

Add a hint to the binary-search exercise to explain generics as this
is now the first exercise to require it.

Fixes #230.
Frida Tveit 9 years ago
parent
commit
65e2f24012

+ 8
- 8
config.json View File

@@ -563,14 +563,6 @@
563 563
       "uuid": "76d28d97-75d3-47eb-bb77-3d347b76f1b6"
564 564
     },
565 565
     {
566
-      "core": true,
567
-      "difficulty": 6,
568
-      "slug": "linked-list",
569
-      "topics": null,
570
-      "unlocked_by": null,
571
-      "uuid": "7ba5084d-3b75-4406-a0d7-87c26387f9c0"
572
-    },
573
-    {
574 566
       "core": false,
575 567
       "difficulty": 6,
576 568
       "slug": "grade-school",
@@ -651,6 +643,14 @@
651 643
       "uuid": "4b3f7771-c642-4278-a3d9-2fb958f26361"
652 644
     },
653 645
     {
646
+      "core": true,
647
+      "difficulty": 6,
648
+      "slug": "linked-list",
649
+      "topics": null,
650
+      "unlocked_by": null,
651
+      "uuid": "7ba5084d-3b75-4406-a0d7-87c26387f9c0"
652
+    },
653
+    {
654 654
       "core": false,
655 655
       "difficulty": 7,
656 656
       "slug": "anagram",

+ 24
- 0
exercises/binary-search/.meta/hints.md View File

@@ -0,0 +1,24 @@
1
+This exercise introduces [generics](https://docs.oracle.com/javase/tutorial/java/generics/index.html).
2
+To make the tests pass you need to construct your class such that it accepts any type of input, e.g. `Integer` or `String`.
3
+
4
+Generics are useful because they allow you to write more general and reusable code.
5
+The Java [List](https://docs.oracle.com/javase/8/docs/api/java/util/List.html) and [Map](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html) implementations are both examples of classes that use generics.
6
+By using them you can construct a `List` containing `Integers` or a list containing `Strings` or any other type.
7
+
8
+There are a few constraints on the types used in generics.
9
+One of them is that once you've constructed a `List` containing `Integers`, you can't put `Strings` into it.
10
+You have to specify which type you want to put into the class when you construct it, and that instance can then only be used with that type.
11
+
12
+For example you could construct a list of `Integers`:
13
+
14
+`List<Integer> someList = new LinkedList();`
15
+
16
+Now `someList` can only contain `Integers`. You could also do:
17
+
18
+`List<String> someOtherList = new LinkedList()`
19
+
20
+Now `someOtherList` can only contain `Strings`.
21
+
22
+Another constraint is that any type used with generics cannot be a [primitive type](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html), such as `int` or `long`.
23
+
24
+It can help to look at an [example use case of generics](https://docs.oracle.com/javase/tutorial/java/generics/types.html) to get you started.

+ 11
- 11
exercises/binary-search/src/test/java/BinarySearchTest.java View File

@@ -13,23 +13,23 @@ public class BinarySearchTest {
13 13
 
14 14
     @Test
15 15
     public void findsAValueInAnArrayWithOneElement() {
16
-        List<Integer> listOfUnitLength = Collections.singletonList(6);
16
+        List<Character> listOfUnitLength = Collections.singletonList('6');
17 17
 
18
-        BinarySearch<Integer> search = new BinarySearch<>(listOfUnitLength);
18
+        BinarySearch<Character> search = new BinarySearch<>(listOfUnitLength);
19 19
 
20
-        assertEquals(0, search.indexOf(6));
20
+        assertEquals(0, search.indexOf('6'));
21 21
     }
22 22
 
23 23
     @Ignore("Remove to run test")
24 24
     @Test
25 25
     public void findsAValueInTheMiddleOfAnArray() {
26
-        List<Integer> sortedList = Collections.unmodifiableList(
27
-                Arrays.asList(1, 3, 4, 6, 8, 9, 11)
26
+        List<String> sortedList = Collections.unmodifiableList(
27
+                Arrays.asList("1", "3", "4", "6", "8", "9", "11")
28 28
         );
29 29
 
30
-        BinarySearch<Integer> search = new BinarySearch<>(sortedList);
30
+        BinarySearch<String> search = new BinarySearch<>(sortedList);
31 31
 
32
-        assertEquals(3, search.indexOf(6));
32
+        assertEquals(3, search.indexOf("6"));
33 33
     }
34 34
 
35 35
     @Ignore("Remove to run test")
@@ -83,13 +83,13 @@ public class BinarySearchTest {
83 83
     @Ignore("Remove to run test")
84 84
     @Test
85 85
     public void identifiesThatAValueIsNotIncludedInTheArray() {
86
-        List<Integer> sortedList = Collections.unmodifiableList(
87
-                Arrays.asList(1, 3, 4, 6, 8, 9, 11)
86
+        List<String> sortedList = Collections.unmodifiableList(
87
+                Arrays.asList("1", "3", "4", "6", "8", "9", "11")
88 88
         );
89 89
 
90
-        BinarySearch<Integer> search = new BinarySearch<>(sortedList);
90
+        BinarySearch<String> search = new BinarySearch<>(sortedList);
91 91
 
92
-        assertEquals(-1, search.indexOf(7));
92
+        assertEquals(-1, search.indexOf("7"));
93 93
     }
94 94
 
95 95
     @Ignore("Remove to run test")