diff --git a/part_3/exercise_1/exercise_1.md b/part_3/exercise_1/exercise_1.md new file mode 100644 index 0000000000000000000000000000000000000000..b0e5af2faa21321809fd3f6a46e05af3e8a4a668 --- /dev/null +++ b/part_3/exercise_1/exercise_1.md @@ -0,0 +1,127 @@ +## a) What form of inheritance is involved in the following case? Background theory: inheritance and polymorphism. + +```java +abstract class CommandLineApp { + String ask(String prompt) { + System.out.print(prompt + ": "); + return new java.util.Scanner(System.in).next(); + } +} + +class LoginScreen extends CommandLineApp { + void lock() { + while(true) { + var id = ask("Username"); + var pw = ask("Passoword"); + + if (id.equals("root") && pw.equals("root")) + return; + + try { + Thread.sleep(1000); + } + catch(Exception e) {} + } + } +} +``` + +### Answer: +This form is **Implementation Inheritance**. + +## b) Comment on and evaluate the following code. Why does it work / not work? What are the benefits and drawbacks associated with it? Background theory and context of the task: inheritance and polymorphism. + +```java +interface NaturalResource { + // How much of the natural resources are left? + // @.pre true + // @.post RESULT == (amount let) + float amountLeft(); + + // Spend x percent + // @.pre amount >= 0 + // @.post amountLeft() == amount + OLD(amountLeft()) + void spend(float amount); +} + +class Coal implements NaturalResource { + private float left; + + Coal(float amount) { + left = amount; + } + + @Override + public float amountLeft() { + return left; + } + + @Override + public void spend(float amount) { + left -= amount; + } +} + +class Hydroelectric implements NaturalResource { + private float left = 100; + + @Override + public float amountLeft() { + return left; + } + + // @.pre cant be called + // @.post throws Exception + private void spend(float amount) throws Exception { + throw new Exception("Renewable is limitless!"); + } +} +``` + +### Answer: +It does not work as expected because the post condition of `spend()` method is not correct in the implementations. + +The expected post condition: `amountLeft() == amount + OLD(amountLeft())`. + +But in `Coal` class, the actual result: `amountLeft() == OLD(amountLeft()) - amount`. + +And in `Hydroelectric` class, the actual result is an `exception` is thrown. + + +#### Benefits and drawbacks: +* Benefit: + + Abstraction: as we don't need to know how is it implemented in details. + + Polymorphism: we can have different implementations depends on the need. +* Drawbacks: the implementations should conform to the interface, otherwise may lead to unexpected results. + + +## c) Comment on and evaluate the following code. Why does it work / not work? What are the benefits and drawbacks associated with it? Background theory and context of the task: inheritance and polymorphism. + +```java +class RandomGenerator { + Object generate() { + var random = new java.util.Random(); + return switch (random.nextInt(4)) { + case 1 -> 1; + case 2 -> 2; + case 3 -> "three"; + default -> new Object(); + }; + } +} + +class RandomIntegerGenerator extends RandomGenerator { + @Override + Integer generate() { + return new java.util.Random().nextInt(64); + } +} +``` + +### Answer: +It works because `Integer` is a subclass of `Object`, all public methods of `Object` can be called with `Integer`. + +#### Benefits and drawbacks: +* Benefit: + + Inheritance: as we can reuse and override (replace) the parent methods for a specific need. +* Drawbacks: the implementations should conform to (depend on) the parent (post condition), otherwise may lead to unexpected results (similar to the [exercise a](#a-what-form-of-inheritance-is-involved-in-the-following-case-background-theory-inheritance-and-polymorphism) above). \ No newline at end of file