Skip to content

Commit 72985dc

Browse files
interfaces new exercise
* interfaces substantially complete * interfaces late changes * add exercise to gradle settings * interfaces addressed review points Co-authored-by: mirkoperillo <mirko.perillo@gmail.com>
1 parent e176467 commit 72985dc

16 files changed

Lines changed: 421 additions & 0 deletions
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
[`interfaces`][interfaces] are the primary means of [decoupling][wiki-loose-coupling] the uses of a class from its implementation. This decoupling provides flexibility for maintenance of the implementation and helps support type safe generic behavior.
2+
3+
The syntax of an interface is similar to that of a class except that methods appear as the signature only and no body is provided.
4+
5+
```java
6+
public interface Language {
7+
String speak();
8+
}
9+
10+
public interface ItalianLanguage extends Language {
11+
String speak();
12+
String speakItalian();
13+
}
14+
15+
public interface ScriptConverter {
16+
void setVersion(String version);
17+
String getVersion();
18+
String convertCyrillicToLatin(String cyrillic);
19+
}
20+
```
21+
22+
The implementing class must implement all operations defined by the interface.
23+
24+
Interfaces typically do one or more of the following:
25+
26+
- allow a number of different classes to be treated generically by the using code. In this case interfaces are playing the same role as a base class. An example of this is [java.io.InputStream][input-stream],
27+
- expose a subset of functionality for some specific purpose (such as [`Comparable<T>`][comparable]) or
28+
- expose the public API of a class so that multiple implementations can co-exist. One example is that of a [test double][wiki-test-double]
29+
30+
```java
31+
public class ItalianTraveller implements ItalianLanguage {
32+
33+
public String speak() {
34+
return "Ciao mondo";
35+
}
36+
37+
public String speakItalian() {
38+
return speak();
39+
}
40+
}
41+
42+
public class ItalianTravellerV2 implements ItalianLanguage {
43+
44+
public String speak() {
45+
return "migliorata - Ciao mondo";
46+
}
47+
48+
public String speakItalian() {
49+
return speak();
50+
}
51+
}
52+
53+
public class FrenchTraveller implements Language {
54+
public String speak() {
55+
return "Ça va?";
56+
}
57+
}
58+
59+
public class RussianTraveller implements Language, ScriptConverter {
60+
61+
private String version;
62+
63+
public void setVersion(String version) {
64+
this.version = version;
65+
}
66+
67+
public String getVersion() {
68+
return version;
69+
}
70+
71+
public String speak()
72+
{
73+
return "Привет мир";
74+
}
75+
76+
public String convertCyrillicToLatin(String cyrillic) {
77+
throw new UnsupportedOperationException();
78+
}
79+
}
80+
81+
public class DocumentTranslator implements ScriptConverter {
82+
83+
private String version;
84+
85+
public void setVersion(String version) {
86+
this.version = version;
87+
}
88+
89+
public String getVersion() {
90+
return version;
91+
}
92+
93+
public String translate(String russian) {
94+
throw new UnsupportedOperationException();
95+
}
96+
97+
public String convertCyrillicToLatin(String cyrillic) {
98+
throw new UnsupportedOperationException();
99+
}
100+
}
101+
```
102+
103+
Code which uses the above interfaces and classes can:
104+
105+
- treat all speakers in the same way irrespective of language.
106+
- allow some subsystem handling script conversion to operate without caring about what specific types it is dealing with.
107+
- remain unaware of the changes to the italian speaker which is convenient if the class code and user code are maintained by different teams
108+
109+
Interfaces are widely used to support testing as they allow for easy [mocking][so-mocking-interfaces].
110+
111+
Interfaces can extend other interfaces with the `extend` keyword.
112+
113+
Members of an interface are public by default.
114+
115+
Interfaces can contain nested types: `interfaces`, `enums` and `classes`. Here, the containing interfaces act as [namespaces][wiki-namespaces]. Nested types are accessed outside the interface by prefixing the interface name and using dot syntax to identify the member.
116+
117+
By design, Java does not support multiple inheritance, but it facilitates a kind of multiple inheritance through interfaces.
118+
119+
Moreover, the concept of [polymorphism can be implemented through interfaces][interface-polymorphism] underpins the interface mechanism.
120+
121+
[interface-polymorphism]: https://www.cs.utexas.edu/~mitra/csSummer2013/cs312/lectures/interfaces.html
122+
[so-mocking-interfaces]: https://stackoverflow.com/a/9226437/96167
123+
[comparable]: https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html
124+
[wiki-test-double]: https://en.wikipedia.org/wiki/Test_double
125+
[wiki-polymorphism]: https://en.wikipedia.org/wiki/Polymorphism_(computer_science)
126+
[wiki-namespaces]: https://en.wikipedia.org/wiki/Namespace
127+
[wiki-loose-coupling]: https://en.wikipedia.org/wiki/Loose_coupling
128+
[interfaces]: https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
129+
[input-stream]: https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## General
2+
3+
- [Interface][interfaces]: what an interface is and how to implement it.
4+
5+
## 1. Enable cars to be driven on the same test track
6+
7+
- This [article][interfaces] discusses interfaces and how to implement them.
8+
9+
## 2. Enable the distance travelled by different models on the test track to be compared
10+
11+
- Setters and getters behave in the same way as any other method.
12+
13+
## 3. Allow the production cars to be ranked
14+
15+
- See [this discussion][sort] of sorting.
16+
- See [here][comparable] for default comparison of objects.
17+
18+
[interfaces]: https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
19+
[sort]: https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html
20+
[comparable]: https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
In this exercise you will be doing some more work on remote control cars.
2+
3+
An experimental car has been developed and the test track needs to be adapted to handle both production and experimental models. The two types of car have already been built and you need to find a way to deal with them both on the test track.
4+
5+
In addition, production cars are beginning to have some success. The team boss is keen to maintain the competitive spirit by publishing a ranking of the production cars.
6+
7+
## 1. Enable cars to be driven on the same test track
8+
9+
Please add a method to the `RemoteControlCar` interface to encapsulate the behavior of `drive()` for the two types of car.
10+
11+
```java
12+
TestTrack.race(new ProductionRemoteControlCar());
13+
TestTrack.race(new ExperimentalRemoteControlCar());
14+
// this should execute without an exception being thrown
15+
```
16+
17+
## 2. Enable the distance travelled by different models on the test track to be compared
18+
19+
Please add a method to the `RemoteControlCar` interface to encapsulate the behavior of the `getDistanceTravelled()` method for the two types of car.
20+
21+
```java
22+
ProductionRemoteControlCar prod = new ProductionRemoteControlCar();
23+
TestTrack.race(prod);
24+
ExperimentalRemoteControlCar exp = new ExperimentalRemoteControlCar();
25+
TestTrack.race(exp);
26+
prod.getDistanceTravelled();
27+
// => 10
28+
exp.getDistanceTravelled();
29+
// => 20
30+
```
31+
32+
## 3. Allow the production cars to be ranked
33+
34+
Please implement the `Comparable<T>` interface in the `ProductionRemoteControlCar` class. The default sort order for cars should be ascending order of victories.
35+
36+
Implement the static `TestTrack.getRankedCars()` to return the cars passed is sorted in ascending order of number of victories.
37+
38+
```java
39+
ProductionRemoteControlCar prc1 = new ProductionRemoteControlCar();
40+
ProductionRemoteControlCar prc2 = new ProductionRemoteControlCar();
41+
prc1.setNumberOfVictories(3);
42+
prc2.setNumberOfVictories(2);
43+
int rankings = TestTrack.getRankedCars(prc1, prc2);
44+
// => rankings[1] == prc1
45+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
An interface is a type containing members defining a group of related functionality. It distances the uses of a class from the implementation allowing multiple different implementations or support for some generic behavior such as formatting, comparison or conversion.
2+
3+
The syntax of an interface is similar to that of a class except that methods appear as the signature only and no body is provided.
4+
5+
```java
6+
public interface Language {
7+
String getLanguageName();
8+
String speak();
9+
}
10+
11+
public class ItalianTaveller implements Language, Cloneable {
12+
13+
// from Language interface
14+
public String getLanguageName() {
15+
return "Italiano";
16+
}
17+
18+
// from Language interface
19+
public String speak() {
20+
return "Ciao mondo";
21+
}
22+
23+
// from Cloneable interface
24+
public Object Clone() {
25+
ItalianTaveller it = new ItalianTaveller();
26+
return it;
27+
}
28+
}
29+
```
30+
31+
All operations defined by the interface must be implemented by the implementing class.
32+
33+
Interfaces usually contain instance methods.
34+
35+
An example of an interface found in the Java Class Library, apart from `Clonable` illustrated above, is `Comparable<T>`. The `Comparable<T>` interface can be implemented where a default generic sort order in collections is required.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"authors": [
3+
{
4+
"github_username": "mikedamay",
5+
"exercism_username": "mikedamay"
6+
}
7+
]
8+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Learning objectives
2+
3+
- Know what interfaces are.
4+
- Know how to use interfaces.
5+
- Know how to define an interface.
6+
- Know how to implement an interface.
7+
8+
## Out of scope
9+
10+
- Default interface methods.
11+
12+
## Concepts
13+
14+
- `interfaces`: know what interfaces are; know how to use interfaces; know how to define an interface; know how to implement an interface.
15+
16+
## Prequisites
17+
18+
- `classes`: know how to work with classes.
19+
- `strings`: know how to use strings
20+
- `lists`: including generics and diamond operator
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class ExperimentalRemoteControlCar implements RemoteControlCar {
2+
3+
private int distanceTravelled;
4+
5+
public void drive() {
6+
distanceTravelled += 20;
7+
}
8+
9+
public int getDistanceTravelled() {
10+
return distanceTravelled;
11+
}
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class ProductionRemoteControlCar implements RemoteControlCar, Comparable<ProductionRemoteControlCar> {
2+
3+
private int distanceTravelled;
4+
private int numberofFictories;
5+
6+
public int compareTo(ProductionRemoteControlCar other) {
7+
return Integer.compare(this.getNumberOfVictories(), other.getNumberOfVictories());
8+
}
9+
10+
public void drive() {
11+
distanceTravelled += 10;
12+
}
13+
14+
public int getDistanceTravelled() {
15+
return distanceTravelled;
16+
}
17+
18+
public int getNumberOfVictories() {
19+
return numberofFictories;
20+
}
21+
22+
public void setNumberOfVictories(int numberofFictories) {
23+
this.numberofFictories = numberofFictories;
24+
}
25+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public interface RemoteControlCar {
2+
void drive();
3+
int getDistanceTravelled();
4+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.List;
4+
5+
public class TestTrack {
6+
7+
public static double race(RemoteControlCar car) {
8+
car.drive();
9+
10+
return car.getDistanceTravelled();
11+
}
12+
13+
public static List<ProductionRemoteControlCar> getRankedCars(ProductionRemoteControlCar prc1,
14+
ProductionRemoteControlCar prc2) {
15+
List<ProductionRemoteControlCar> rankings = new ArrayList<>();
16+
rankings.add(prc1);
17+
rankings.add(prc1);
18+
Collections.sort(rankings);
19+
20+
return rankings;
21+
}
22+
}

0 commit comments

Comments
 (0)