Skip to content
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
14 changes: 14 additions & 0 deletions kata/8-kyu/classy-extentions/README.md
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 11 additions & 0 deletions kata/8-kyu/classy-extentions/main/Animal.java
Original file line number Diff line number Diff line change
@@ -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.";
}
}
10 changes: 10 additions & 0 deletions kata/8-kyu/classy-extentions/main/Cat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Cat extends Animal {
Cat(String name) {
super(name);
}

@Override
public String speak() {
return "%s meows.".formatted(this.name);
}
}
31 changes: 31 additions & 0 deletions kata/8-kyu/classy-extentions/test/SampleTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
1 change: 1 addition & 0 deletions kata/8-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down