From 65e2f240123b3357e310881bc5f2c29e92009343 Mon Sep 17 00:00:00 2001 From: Frida Tveit Date: Fri, 6 Oct 2017 13:11:57 +0100 Subject: [PATCH 1/2] 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. --- config.json | 16 ++++++------- exercises/binary-search/.meta/hints.md | 24 +++++++++++++++++++ .../src/test/java/BinarySearchTest.java | 22 ++++++++--------- 3 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 exercises/binary-search/.meta/hints.md diff --git a/config.json b/config.json index 617abbb64..5ff66e555 100644 --- a/config.json +++ b/config.json @@ -562,14 +562,6 @@ "unlocked_by": "word-count", "uuid": "76d28d97-75d3-47eb-bb77-3d347b76f1b6" }, - { - "core": true, - "difficulty": 6, - "slug": "linked-list", - "topics": null, - "unlocked_by": null, - "uuid": "7ba5084d-3b75-4406-a0d7-87c26387f9c0" - }, { "core": false, "difficulty": 6, @@ -650,6 +642,14 @@ "unlocked_by": "scrabble-score", "uuid": "4b3f7771-c642-4278-a3d9-2fb958f26361" }, + { + "core": true, + "difficulty": 6, + "slug": "linked-list", + "topics": null, + "unlocked_by": null, + "uuid": "7ba5084d-3b75-4406-a0d7-87c26387f9c0" + }, { "core": false, "difficulty": 7, diff --git a/exercises/binary-search/.meta/hints.md b/exercises/binary-search/.meta/hints.md new file mode 100644 index 000000000..8211b79dd --- /dev/null +++ b/exercises/binary-search/.meta/hints.md @@ -0,0 +1,24 @@ +This exercise introduces [generics](https://docs.oracle.com/javase/tutorial/java/generics/index.html). +To make the tests pass you need to construct your class such that it accepts any type of input, e.g. `Integer` or `String`. + +Generics are useful because they allow you to write more general and reusable code. +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. +By using them you can construct a `List` containing `Integers` or a list containing `Strings` or any other type. + +There are a few constraints on the types used in generics. +One of them is that once you've constructed a `List` containing `Integers`, you can't put `Strings` into it. +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. + +For example you could construct a list of `Integers`: + +`List someList = new LinkedList();` + +Now `someList` can only contain `Integers`. You could also do: + +`List someOtherList = new LinkedList()` + +Now `someOtherList` can only contain `Strings`. + +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`. + +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. diff --git a/exercises/binary-search/src/test/java/BinarySearchTest.java b/exercises/binary-search/src/test/java/BinarySearchTest.java index 9aa7c5da3..07fd0ed2a 100644 --- a/exercises/binary-search/src/test/java/BinarySearchTest.java +++ b/exercises/binary-search/src/test/java/BinarySearchTest.java @@ -13,23 +13,23 @@ public class BinarySearchTest { @Test public void findsAValueInAnArrayWithOneElement() { - List listOfUnitLength = Collections.singletonList(6); + List listOfUnitLength = Collections.singletonList('6'); - BinarySearch search = new BinarySearch<>(listOfUnitLength); + BinarySearch search = new BinarySearch<>(listOfUnitLength); - assertEquals(0, search.indexOf(6)); + assertEquals(0, search.indexOf('6')); } @Ignore("Remove to run test") @Test public void findsAValueInTheMiddleOfAnArray() { - List sortedList = Collections.unmodifiableList( - Arrays.asList(1, 3, 4, 6, 8, 9, 11) + List sortedList = Collections.unmodifiableList( + Arrays.asList("1", "3", "4", "6", "8", "9", "11") ); - BinarySearch search = new BinarySearch<>(sortedList); + BinarySearch search = new BinarySearch<>(sortedList); - assertEquals(3, search.indexOf(6)); + assertEquals(3, search.indexOf("6")); } @Ignore("Remove to run test") @@ -83,13 +83,13 @@ public void findsAValueInAnArrayOfEvenLength() { @Ignore("Remove to run test") @Test public void identifiesThatAValueIsNotIncludedInTheArray() { - List sortedList = Collections.unmodifiableList( - Arrays.asList(1, 3, 4, 6, 8, 9, 11) + List sortedList = Collections.unmodifiableList( + Arrays.asList("1", "3", "4", "6", "8", "9", "11") ); - BinarySearch search = new BinarySearch<>(sortedList); + BinarySearch search = new BinarySearch<>(sortedList); - assertEquals(-1, search.indexOf(7)); + assertEquals(-1, search.indexOf("7")); } @Ignore("Remove to run test") From 8b9a7a21b4fa83320362fff811ec1e66e77b72e1 Mon Sep 17 00:00:00 2001 From: Frida Tveit Date: Wed, 18 Oct 2017 19:07:34 +0100 Subject: [PATCH 2/2] Tidy up binary search hints and reference implementation --- exercises/binary-search/.meta/hints.md | 5 +++-- .../.meta/src/reference/java/BinarySearch.java | 10 +++------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/exercises/binary-search/.meta/hints.md b/exercises/binary-search/.meta/hints.md index 8211b79dd..3936a1801 100644 --- a/exercises/binary-search/.meta/hints.md +++ b/exercises/binary-search/.meta/hints.md @@ -11,14 +11,15 @@ You have to specify which type you want to put into the class when you construct For example you could construct a list of `Integers`: -`List someList = new LinkedList();` +`List someList = new LinkedList<>();` Now `someList` can only contain `Integers`. You could also do: -`List someOtherList = new LinkedList()` +`List someOtherList = new LinkedList<>()` Now `someOtherList` can only contain `Strings`. 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`. +However, every primitive type has a corresponding reference type, so instead of `int` you can use [`Integer`](https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html) and instead of `long` you can use [`Long`](https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html). 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. diff --git a/exercises/binary-search/.meta/src/reference/java/BinarySearch.java b/exercises/binary-search/.meta/src/reference/java/BinarySearch.java index 97c50a596..8e5aa56c5 100644 --- a/exercises/binary-search/.meta/src/reference/java/BinarySearch.java +++ b/exercises/binary-search/.meta/src/reference/java/BinarySearch.java @@ -1,24 +1,20 @@ import java.util.List; -public class BinarySearch> { +class BinarySearch> { private List array; private int arraySize; - public BinarySearch(List array) { + BinarySearch(List array) { this.array = array; this.arraySize = array.size(); } - public int indexOf(T value) { + int indexOf(T value) { return search(value); } - public List getArray() { - return array; - } - private int search(T value) { int left = 0; int right = this.arraySize - 1;