|
| 1 | +package bonuses.english; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import engine.Locale; |
| 6 | +import engine.Sensei; |
| 7 | +import sensei.AboutInterfacesKoans; |
| 8 | + |
| 9 | +public class AboutInterfaces { |
| 10 | + /** |
| 11 | + * # First interface implementations |
| 12 | + * |
| 13 | + * Write a class 'numbers.AddNumbers' which implements interface 'bonuses.teachingmaterial.Combining'. |
| 14 | + * The implementation of the combine() method should return the 2 numbers added together. |
| 15 | + * Write a class 'numbers.MultiplyNumbers' which also implements interface 'bonuses.teachingmaterial.Combining'. |
| 16 | + * This implementation of the combine() method should return the 2 numbers multiplied together. |
| 17 | + * |
| 18 | + * --------- TIPS -------------- |
| 19 | + * |
| 20 | + * Consider the following situation: |
| 21 | + * |
| 22 | + * You are in the middle of a large, empty room, when a zombie suddenly attacks you. |
| 23 | + * You have no weapon. |
| 24 | + * Luckily, a fellow living human is standing in the doorway of the room. |
| 25 | + * "Quick!" you shout at him. "Throw me something I can hit the zombie with!" |
| 26 | + * |
| 27 | + * Now consider: |
| 28 | + * You didn't specify (nor do you care) exactly what your friend will choose to toss; ...But it doesn't matter, as long as: |
| 29 | + * |
| 30 | + * 1) It's something that can be tossed (He can't toss you the sofa) |
| 31 | + * 2) It's something that you can grab hold of (Not a wet soap) |
| 32 | + * 3) It's something you can use to bash the zombie's brains out (That rules out pillows and such) |
| 33 | + * |
| 34 | + * It doesn't matter whether you get a baseball bat or a hammer - as long as it implements your three conditions, you're good. |
| 35 | + * |
| 36 | + * In Java, you sometimes need an object that implements "conditions": one or more specific methods. But you don't care about its class. The interface is what will describe those methods. |
| 37 | + * We can see the interface as a kind of contract that an object would respect. |
| 38 | + * |
| 39 | + * For example, in a game where the situation above would happen, you could create the following interface in a game: |
| 40 | + * |
| 41 | + * public interface Weapon { |
| 42 | + * void hit(Monster monster); |
| 43 | + * } |
| 44 | + * |
| 45 | + * Now, whether your weapon is a sword inflicting 10 damage points, or a knife inflicting only 4 damage points to the monster, the player will be able to hit a monster with it. |
| 46 | + * |
| 47 | + * Respecting the contract of a Java interface is called 'implementing the interface'. A class can implement an interface this way: |
| 48 | + * |
| 49 | + * you declare that this class will implement the Weapon interface |
| 50 | + * vvvvvvvvvvvvvvvvv |
| 51 | + * |
| 52 | + * public class Sword implements Weapon { |
| 53 | + * |
| 54 | + * // Since Sword implements Weapon, it must implement the hit method. |
| 55 | + * @Override // This special annotation means the method is defined elsewhere (our interface in this case) |
| 56 | + * public void hit(Monster monster) { |
| 57 | + * // some code computing and applying damage to the monster, applying some tear and wear on the weapon, etc... |
| 58 | + * } |
| 59 | + * |
| 60 | + * } |
| 61 | + * |
| 62 | + * |
| 63 | + * Now, the code in hit() maybe complicated but it does not matter: it follows the contract of the interface, and you can call it. |
| 64 | + * For example, you could create an object allowing you to hit a zombie with the following code: |
| 65 | + * |
| 66 | + * Monster zombie = ...; // Code getting the object for the zombie in the middle of the room |
| 67 | + * Weapon tossedWeapon = new Sword(); // or new Hammer() or new Axe() or new WhateverImplementsWeapon() |
| 68 | + * tossedWeapon.hit(zombie); // use the Weapon interface |
| 69 | + * |
| 70 | + * Notice the type of the variable 'tossedWeapon': it is a Weapon, not a Sword. Interfaces, like classes, are types you can use for variables, fields, and parameters. |
| 71 | + * Because Sword implements Weapon, Java considers that a Sword object _is_ a Weapon. Having variables and parameters using the interface type allows you to work with any object implementing that interface. |
| 72 | + * |
| 73 | + * Take a look at the bonuses.teachingmaterial.Combining interface. It defines a method which can be implemented in various ways. |
| 74 | + * This exercise is about implementing that interface in 2 ways. |
| 75 | + * |
| 76 | + * ------------------------------- |
| 77 | + * |
| 78 | + * Expected result: |
| 79 | + * |
| 80 | + * The following code: |
| 81 | + * |
| 82 | + * Combining combining = new AddNumbers(); |
| 83 | + * System.out.println(combining.combine(3, 4)); |
| 84 | + * combining = new MultiplyNumbers(); |
| 85 | + * System.out.println(combining.combine(3, 4)); |
| 86 | + * |
| 87 | + * Should display: |
| 88 | + * |
| 89 | + * 7 |
| 90 | + * 12 |
| 91 | + * |
| 92 | + */ |
| 93 | + |
| 94 | + |
| 95 | + public static void main(String[] args) { |
| 96 | + new Sensei(Locale.en, List.of(AboutInterfacesKoans.koans)).offerKoans(); |
| 97 | + } |
| 98 | +} |
0 commit comments