diff --git a/kata/8-kyu/classy-extentions/README.md b/kata/8-kyu/classy-extentions/README.md new file mode 100644 index 00000000..a5fea143 --- /dev/null +++ b/kata/8-kyu/classy-extentions/README.md @@ -0,0 +1,14 @@ +# [Classy Extentions](https://www.codewars.com/kata/classy-extentions "https://www.codewars.com/kata/55a14aa4817efe41c20000bc") + +The goal of this kata is to train the basic OOP concepts of inheritance and method overriding. + +You will be preloaded with the `Animal` class, so you should only edit the `Cat` class. + +### Task + +Your task is to complete the `Cat` class which extends `Animal` and replace the `speak` method to return the cats name + meows. +e.g. `'Mr Whiskers meows.'` + +The name attribute is accessible in the class with `this.name`. + +Reference: [Oracle docs](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html) \ No newline at end of file diff --git a/kata/8-kyu/classy-extentions/main/Animal.java b/kata/8-kyu/classy-extentions/main/Animal.java new file mode 100644 index 00000000..b80f82e1 --- /dev/null +++ b/kata/8-kyu/classy-extentions/main/Animal.java @@ -0,0 +1,11 @@ +class Animal { + protected final String name; + + Animal(String name) { + this.name = name; + } + + public String speak() { + return this.name + " makes a noise."; + } +} \ No newline at end of file diff --git a/kata/8-kyu/classy-extentions/main/Cat.java b/kata/8-kyu/classy-extentions/main/Cat.java new file mode 100644 index 00000000..e3718c6d --- /dev/null +++ b/kata/8-kyu/classy-extentions/main/Cat.java @@ -0,0 +1,10 @@ +class Cat extends Animal { + Cat(String name) { + super(name); + } + + @Override + public String speak() { + return "%s meows.".formatted(this.name); + } +} \ No newline at end of file diff --git a/kata/8-kyu/classy-extentions/test/SampleTest.java b/kata/8-kyu/classy-extentions/test/SampleTest.java new file mode 100644 index 00000000..8bbf9b63 --- /dev/null +++ b/kata/8-kyu/classy-extentions/test/SampleTest.java @@ -0,0 +1,31 @@ +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; + +import org.junit.jupiter.api.Test; + +class SampleTest { + @Test + void wildBoar() { + Animal animal = new Animal("Wild Boar"); + assertEquals("Wild Boar makes a noise.", animal.speak()); + } + + @Test + void mrWhiskers() { + Cat cat = new Cat("Mr Whiskers"); + assertEquals("Mr Whiskers meows.", cat.speak()); + assertInstanceOf(Animal.class, cat); + } + + @Test + void lamp() { + Cat cat = new Cat("Lamp"); + assertEquals("Lamp meows.", cat.speak()); + } + + @Test + void moneyBags() { + Cat cat = new Cat("$$Money Bags$$"); + assertEquals("$$Money Bags$$ meows.", cat.speak()); + } +} \ No newline at end of file diff --git a/kata/8-kyu/index.md b/kata/8-kyu/index.md index 5c91d5ec..81ee98c9 100644 --- a/kata/8-kyu/index.md +++ b/kata/8-kyu/index.md @@ -31,6 +31,7 @@ - [Century From Year](century-from-year "5a3fe3dde1ce0e8ed6000097") - [Circles in Polygons](circles-in-polygons "5a026a9cffe75fbace00007f") - [Classic Hello World](classic-hello-world "57036f007fd72e3b77000023") +- [Classy Extentions](classy-extentions "55a14aa4817efe41c20000bc") - [Closest elevator](closest-elevator "5c374b346a5d0f77af500a5a") - [Collatz Conjecture (3n+1)](collatz-conjecture-3n-plus-1 "577a6e90d48e51c55e000217") - [Collinearity](collinearity "65ba420888906c1f86e1e680")