Skip to content
Snippets Groups Projects
Commit 6f4a5e74 authored by Dao's avatar Dao
Browse files

feat(part4): Exercise 3.

parent b8435af2
No related branches found
No related tags found
No related merge requests found
In the project base for the exercise in the [exercise3](https://gitlab.utu.fi/open-programming/advanced-course-in-object-oriented-programming-part-4/-/tree/main/exercise3) folder, there is an implementation that includes the classes **Winged**, **Bipedal**, **Bird**, and **Crow**. The class **Exercise 3** demonstrates the use of these classes. Your task is to interpret the given implementation and answer the following questions:
## a) What language feature is involved in the methods challenge1 and challenge2? How does this feature manifest, and how should it be used?
### Answer:
- Subtyping is involved in the methods `challenge1` and `challenge2`.
- We can see this feature in the parameters of these methods.
+ In `challenge1` the input should be an instance of `Bird` or an instance of subtype of `Bird`.
+ In `challenge2` the input should be an instance of class that implements both `Winged` and `Bipedal` interfaces.
## b) The methods challenge1 and challenge2 seem to perform the same way based on their output. What are the key functional differences between them?
## Answer:
- The key functional differences between them are:
+ `challenge1` only accepts the instance of `Bird` or its subtypes.
+ `challenge2` allows the instance of any class that implements both `Winged` and `Bipedal` (so supports `Bird`, subtypes of `Bird` and also other classes which implements both `Winged` and `Bipedal`).
## c) Come up with and implement a meaningful situation from the perspective of object-oriented programming that demonstrates the advantages of the latter method, challenge2. (Presumably, the implementation has some advantages because it has a longer method definition -- why else would someone write a more complex signature for code that does exactly the same thing?).
## Answer:
- We have an example as below:
```java
package fi.utu.tech.ooj.exercise4.exercise3;
interface PerimeterCalculation {
double perimeter();
}
interface AreaCalculation {
double area();
}
class Rectangle implements PerimeterCalculation, AreaCalculation {
private final double length;
private final double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double perimeter() {
return 2 * (length + width);
}
public double area() {
return length * width;
}
}
class Square extends Rectangle {
public Square(double length) {
super(length, length);
}
}
class Circle implements PerimeterCalculation, AreaCalculation {
private final double radius;
public Circle(double radius) {
this.radius = radius;
}
public double perimeter() {
return 2 * Math.PI * radius;
}
public double area() {
return Math.PI * radius * radius;
}
}
public class Exercise3 {
public Exercise3() {
System.out.println("Exercise 3");
challenge1(new Square(10.0));
challenge2(new Circle(10.0));
// challenge1(new Circle(10.0)); is not allowed,
// but the code below can be run.
challenge2(new Square(10.0));
}
void challenge1(Rectangle shape) {
System.out.println("Perimeter: " + shape.perimeter());
System.out.println("Area: " + shape.area());
}
<X extends PerimeterCalculation & AreaCalculation> void challenge2(X shape) {
System.out.println("Perimeter: " + shape.perimeter());
System.out.println("Area: " + shape.area());
}
}
```
In the example above the code `challenge1(new Circle(10.0));` is not accepted, but we can run `challenge2(new Square(10.0));`, it means `challenge2` will support more shapes than `challenge1`.
We write a more complex signature in order to support more cases (more generic).
\ 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