diff --git a/config.json b/config.json index dee792c20..c36d4c062 100644 --- a/config.json +++ b/config.json @@ -639,18 +639,6 @@ "unlocked_by": "word-count", "uuid": "76d28d97-75d3-47eb-bb77-3d347b76f1b6" }, - { - "core": true, - "difficulty": 6, - "slug": "linked-list", - "topics": [ - "algorithms", - "lists", - "generics" - ], - "unlocked_by": null, - "uuid": "7ba5084d-3b75-4406-a0d7-87c26387f9c0" - }, { "core": false, "difficulty": 6, @@ -749,6 +737,18 @@ "unlocked_by": "scrabble-score", "uuid": "4b3f7771-c642-4278-a3d9-2fb958f26361" }, + { + "core": true, + "difficulty": 6, + "slug": "linked-list", + "topics": [ + "algorithms", + "lists", + "generics" + ], + "unlocked_by": null, + "uuid": "7ba5084d-3b75-4406-a0d7-87c26387f9c0" + }, { "core": false, "difficulty": 6, diff --git a/exercises/binary-search/.meta/hints.md b/exercises/binary-search/.meta/hints.md new file mode 100644 index 000000000..3936a1801 --- /dev/null +++ b/exercises/binary-search/.meta/hints.md @@ -0,0 +1,25 @@ +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`. +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; 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")