Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: kata/java-generic-inheritance #672

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Codewars Handbook ☕️🚀

[![Views statistics +1 👀](https://img.shields.io/badge/dynamic/xml?color=success&label=views&query=//*[name()=%27text%27][3]&url=https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FParanoidUser%2Fcodewars-handbook)](https://hits.seeyoufarm.com/api/count/graph/dailyhits.svg?url=https://github.com/ParanoidUser/codewars-handbook)
[![Completed kata 👌](https://img.shields.io/badge/completed%20kata-68.8%25-red.svg)](https://www.codewars.com/kata/search/java?xids=completed)
[![Completed kata 👌](https://img.shields.io/badge/completed%20kata-68.6%25-red.svg)](https://www.codewars.com/kata/search/java?xids=completed)
[![CI pipeline 🛠](https://img.shields.io/github/actions/workflow/status/ParanoidUser/codewars-handbook/build.yml?branch=main)](https://github.com/ParanoidUser/codewars-handbook/actions/workflows/build.yml)
[![Quality gate 🔎](https://img.shields.io/sonar/alert_status/codewars-handbook?server=https%3A%2F%2Fsonarcloud.io)](https://sonarcloud.io/dashboard?id=codewars-handbook)
[![Let's have a chat! 📞](https://img.shields.io/gitter/room/ParanoidUser/codewars-handbook?color=49c39e)](https://gitter.im/ParanoidUser/codewars-handbook)
Expand All @@ -25,7 +25,7 @@ slug.

| [1 kyu](/kata/1-kyu/index.md) | [2 kyu](/kata/2-kyu/index.md) | [3 kyu](/kata/3-kyu/index.md) | [4 kyu](/kata/4-kyu/index.md) | [5 kyu](/kata/5-kyu/index.md) | [6 kyu](/kata/6-kyu/index.md) | [7 kyu](/kata/7-kyu/index.md) | [8 kyu](/kata/8-kyu/index.md) | [beta](/kata/beta/index.md) | [retired](/kata/retired/index.md) |
|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:---------------------------:|:---------------------------------:|
| 0 | 1 | 2 | 26 | 48 | 431 | 584 | 218 | 56 | 79 |
| 0 | 1 | 2 | 26 | 48 | 431 | 585 | 218 | 56 | 79 |

**Note:** The source code is written in Java 17 and may use language features that are incompatible
with Java 8, 11.
Expand Down
1 change: 1 addition & 0 deletions kata/7-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@
- [Java Functional Programming (Part 1: The Beginning)](java-functional-programming-part-1-the-beginning)
- [Java Functional Programming (Part 2: Multiline Functions)](java-functional-programming-part-2-multiline-functions)
- [Java Functional Programming (Part 3: Closured for Business)](java-functional-programming-part-3-closured-for-business)
- [Java: Generic Inheritance](java-generic-inheritance)
- [Jumping Number (Special Numbers Series #4)](jumping-number-special-numbers-series-number-4)
# K
- [Keypad horror](keypad-horror)
Expand Down
55 changes: 55 additions & 0 deletions kata/7-kyu/java-generic-inheritance/README.md
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?
3 changes: 3 additions & 0 deletions kata/7-kyu/java-generic-inheritance/main/Shape.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
abstract class Shape {
abstract double getArea();
}
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();
}
}
12 changes: 12 additions & 0 deletions kata/7-kyu/java-generic-inheritance/test/Circle.java
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;
}
}
12 changes: 12 additions & 0 deletions kata/7-kyu/java-generic-inheritance/test/Rectangle.java
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;
}
}
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);
}
}
12 changes: 12 additions & 0 deletions kata/7-kyu/java-generic-inheritance/test/Square.java
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;
}
}
Loading