-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: kata/java-generic-inheritance (#672)
* docs: kata description * docs: sync progress * feat: kata/java-generic-inheritance --------- Co-authored-by: ParanoidUser <5120290+ParanoidUser@users.noreply.github.com>
- Loading branch information
1 parent
2368ae3
commit dbafaf1
Showing
9 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# [Java: Generic Inheritance](https://www.codewars.com/kata/java-generic-inheritance "https://www.codewars.com/kata/6705c60be3abf45ead76d052") | ||
|
||
I am creating a class hierarchy of geometrical `Shape`s. This is my base class: | ||
```java | ||
abstract class Shape { | ||
public abstract double getArea(); | ||
} | ||
``` | ||
|
||
And this is a circle: | ||
|
||
```java | ||
class Circle extends Shape { | ||
public final double radius; | ||
public Circle(double radius) { | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return Math.PI * radius * radius; | ||
} | ||
} | ||
``` | ||
|
||
There are other shapes, like `Rectangle`s, `Square`s, etc. | ||
|
||
___ | ||
|
||
I also wrote a utility class `ShapeUtilities`. One of those utilities is a function that computes the total area of a list of shapes: | ||
```java | ||
public static double sumAllAreas(List<Shape> shapes) { | ||
double totalArea = 0.0; | ||
for (Shape shape : shapes) | ||
totalArea += shape.getArea(); | ||
return totalArea; | ||
} | ||
``` | ||
|
||
This seems to work fine: | ||
```java | ||
List<Shape> shapes = List.of(new Circle(2), new Rectangle(3, 3), new Square(2)); | ||
double totalArea = ShapeUtilities.sumAllAreas(shapes); // 25.567 | ||
``` | ||
|
||
But when I try to call it with a `List<Circle>`, the code does not compile, even though `Circle` inherits from `Shape` :( | ||
|
||
```java | ||
List<Circle> circles = List.of(new Circle(2), new Circle(1), new Circle(5)); | ||
double totalArea = ShapeUtilities.sumAllAreas(circles); // compilation error ! | ||
``` | ||
|
||
___ | ||
|
||
Can you fix the code so that `sumAllAreas()` works with `Shape` and all its subtypes? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
abstract class Shape { | ||
abstract double getArea(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import java.util.Collection; | ||
|
||
interface ShapeUtilities { | ||
static double sumAllAreas(Collection<? extends Shape> shapes) { | ||
return shapes.stream().mapToDouble(Shape::getArea).sum(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Circle extends Shape { | ||
private final double area; | ||
|
||
Circle(double radius) { | ||
this.area = Math.PI * radius * radius; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return area; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Rectangle extends Shape { | ||
private final double area; | ||
|
||
Rectangle(double height, double width) { | ||
this.area = height * width; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return area; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
kata/7-kyu/java-generic-inheritance/test/ShapeUtilitiesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ShapeUtilitiesTest { | ||
@Test | ||
void shouldWorkForShapes() { | ||
List<Shape> shapes = List.of(new Circle(2), new Rectangle(3, 3), new Square(2)); | ||
assertEquals(25.567, ShapeUtilities.sumAllAreas(shapes), 1e-2); | ||
} | ||
|
||
@Test | ||
void shouldWorkForCircles() { | ||
List<Circle> circles = List.of(new Circle(2), new Circle(1), new Circle(5)); | ||
assertEquals(94.247, ShapeUtilities.sumAllAreas(circles), 1e-2); | ||
} | ||
|
||
@Test | ||
void shouldWorkForRectangles() { | ||
List<Shape> rectangles = List.of(new Rectangle(3, 1), new Square(2), new Rectangle(1, 2)); | ||
assertEquals(9.0, ShapeUtilities.sumAllAreas(rectangles), 1e-2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Square extends Shape { | ||
private final double area; | ||
|
||
Square(double side) { | ||
this.area = side * side; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return area; | ||
} | ||
} |