Skip to content
Snippets Groups Projects
Commit 42bedbe1 authored by Dao's avatar Dao
Browse files

feat(part3): Exercise 1.

parent e2a40781
No related branches found
No related tags found
No related merge requests found
## 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment