Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2cf12cd

Browse files
committed
implementation with Abstract class and Interface
1 parent 55d71fe commit 2cf12cd

File tree

6 files changed

+110
-2
lines changed

6 files changed

+110
-2
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package com.techreturners.cats;
22

33
interface Cat {
4+
String getSetting();
45

6+
int getAverageHeight();
7+
8+
void goToSleep();
9+
10+
boolean isAsleep();
11+
12+
void wakeUp();
13+
14+
String eat();
15+
16+
String sleep();
517
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.techreturners.cats;
2+
3+
public abstract class CatCategory implements Cat{
4+
private String setting;
5+
private int averageHeight;
6+
protected boolean isAsleep = false;
7+
8+
9+
public int getAverageHeight() {
10+
return averageHeight;
11+
}
12+
13+
public void setAverageHeight(int averageHeight){
14+
this.averageHeight=averageHeight;
15+
}
16+
17+
18+
public String getSetting() {
19+
return setting;
20+
}
21+
22+
public void setSetting(String setting){
23+
this.setting=setting;
24+
}
25+
26+
public void goToSleep() {
27+
sleep();
28+
isAsleep = true;
29+
}
30+
31+
32+
public void wakeUp() {
33+
isAsleep = false;
34+
35+
}
36+
37+
@Override
38+
public boolean isAsleep() {
39+
return isAsleep;
40+
}
41+
42+
@Override
43+
public String sleep() {
44+
return null;
45+
}
46+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.techreturners.cats;
2+
3+
public class CheetahCat extends CatCategory {
4+
5+
@Override
6+
public String eat() {
7+
return "Zzzzzzz";
8+
}
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.techreturners.cats;
2+
3+
import java.util.Random;
4+
5+
public class DomesticCat extends CatCategory {
6+
7+
public DomesticCat() {
8+
super.setSetting("domestic");
9+
super.setAverageHeight(23);
10+
}
11+
12+
13+
@Override
14+
public String eat() {
15+
String[] randomOutput = {"Purrrrrrr", "Purrrrrrr! It will do I suppose"};
16+
Random rand = new Random();
17+
return randomOutput[rand.nextInt(randomOutput.length)];
18+
}
19+
20+
@Override
21+
public String sleep() {
22+
return "small snooze and snores";
23+
}
24+
25+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.techreturners.cats;
2+
3+
public class LionCat extends CatCategory {
4+
public LionCat() {
5+
super.setAverageHeight(1100);
6+
}
7+
8+
@Override
9+
public String eat() {
10+
return "Roar!!!!";
11+
}
12+
13+
14+
}

src/test/java/com/techreturners/cats/CatTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public void checkLionHeight() {
4545
assertEquals(1100, lionCat.getAverageHeight());
4646
}
4747

48-
4948
@Test
5049
public void feedTheLion() {
5150
Cat lionCat = new LionCat();
@@ -61,6 +60,9 @@ public void feedTheCheetah() {
6160
@Test
6261
public void feedTheCat() {
6362
Cat domesticCat = new DomesticCat();
64-
assertEquals("Purrrrrrr", domesticCat.eat());
63+
//assertEquals("Purrrrrrr", domesticCat.eat());
64+
String eatComment = domesticCat.eat();
65+
assertTrue("Purrrrrrr".equals(eatComment) || "Purrrrrrr! It will do I suppose".equals(eatComment));
66+
6567
}
6668
}

0 commit comments

Comments
 (0)