Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions exercises/binary-search/.meta/hints.md
Original file line number Diff line number Diff line change
@@ -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<Integer> someList = new LinkedList();`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use LinkedList<>(); here.


Now `someList` can only contain `Integers`. You could also do:

`List<String> someOtherList = new LinkedList()`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use LinkedList<>(); here.


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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to add a reminder that every primitive type has a corresponding reference type!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Premitive Type Reference Equvalent
boolean Boolean
byte Byte
short Short
char Character
int Integer
long Long
float Float
double Double


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.
22 changes: 11 additions & 11 deletions exercises/binary-search/src/test/java/BinarySearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ public class BinarySearchTest {

@Test
public void findsAValueInAnArrayWithOneElement() {
List<Integer> listOfUnitLength = Collections.singletonList(6);
List<Character> listOfUnitLength = Collections.singletonList('6');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as for custom-set; is there a pattern for the generic types being used now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. I've tried to use Integer, Character and String and to make sure I've got a couple of tests using each, but I've not got a strict pattern as I didn't think it mattered too much. I could make it a more strict pattern if you think that would be useful?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, I think it's good as-is!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change it to cycle through Character, String, Integer as it's probably best to be consistent :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I was changing it I remembered why I didn't do that. Some of them can't use Character because they use higher numbers, e.g. 11... So it's only possible to use Character for some of the earlier tests, at least for this exercise. I could make the rest cycle through String and Integer?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I was changing it I remembered why I didn't do that. Some of them can't use Character because they use higher numbers, e.g. 11... So it's only possible to use Character for some of the earlier tests, at least for this exercise. I could make the rest cycle through String and Integer?

This sounds like a good compromise to me!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, will do that :) Will do that for the others too, try to cycle through all three but skip Character if not possible :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, as it turns out String causes problems as well since it will sort it lexicographically and we're using numbers in all our test cases... So should I instead try to cycle through all three, skip Chracter if not possible and also skip String if not possible? And make sure that Character and String are used at least once?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds ideal, yep!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, will do that then :)


BinarySearch<Integer> search = new BinarySearch<>(listOfUnitLength);
BinarySearch<Character> search = new BinarySearch<>(listOfUnitLength);

assertEquals(0, search.indexOf(6));
assertEquals(0, search.indexOf('6'));
}

@Ignore("Remove to run test")
@Test
public void findsAValueInTheMiddleOfAnArray() {
List<Integer> sortedList = Collections.unmodifiableList(
Arrays.asList(1, 3, 4, 6, 8, 9, 11)
List<String> sortedList = Collections.unmodifiableList(
Arrays.asList("1", "3", "4", "6", "8", "9", "11")
);

BinarySearch<Integer> search = new BinarySearch<>(sortedList);
BinarySearch<String> search = new BinarySearch<>(sortedList);

assertEquals(3, search.indexOf(6));
assertEquals(3, search.indexOf("6"));
}

@Ignore("Remove to run test")
Expand Down Expand Up @@ -83,13 +83,13 @@ public void findsAValueInAnArrayOfEvenLength() {
@Ignore("Remove to run test")
@Test
public void identifiesThatAValueIsNotIncludedInTheArray() {
List<Integer> sortedList = Collections.unmodifiableList(
Arrays.asList(1, 3, 4, 6, 8, 9, 11)
List<String> sortedList = Collections.unmodifiableList(
Arrays.asList("1", "3", "4", "6", "8", "9", "11")
);

BinarySearch<Integer> search = new BinarySearch<>(sortedList);
BinarySearch<String> search = new BinarySearch<>(sortedList);

assertEquals(-1, search.indexOf(7));
assertEquals(-1, search.indexOf("7"));
}

@Ignore("Remove to run test")
Expand Down