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

Skip to content

Commit e4cb378

Browse files
committed
Sample fields and objects code
1 parent 0998f43 commit e4cb378

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/main/java/com/serenitydojo/Cat.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
package com.serenitydojo;
22

3+
/**
4+
* A feline creature.
5+
*/
36
public class Cat {
7+
/**
8+
* The name of the cat
9+
* This is important
10+
*/
411
private String name;
512
private String favoriteFood;
613
private int age;
714

15+
// A very useful field
16+
public static final String CAT_NOISE = "Meow";
17+
18+
public static String usualFood() {
19+
return "Tuna";
20+
}
21+
22+
public Cat(String name, int age) {
23+
this.name = name;
24+
this.age = age;
25+
this.favoriteFood = usualFood();
26+
}
27+
828
public Cat(String name, String favoriteFood, int age) {
929
this.name = name;
1030
this.favoriteFood = favoriteFood;
@@ -26,4 +46,25 @@ public String getFavoriteFood() {
2646
public int getAge() {
2747
return age;
2848
}
49+
50+
public void makeNoise() {
51+
System.out.println(CAT_NOISE);
52+
}
53+
54+
public void feed(String food) {
55+
System.out.println(name + " eats some " + food);
56+
}
57+
58+
public void groom() {
59+
lickPaws();
60+
cleanFur();
61+
}
62+
63+
private void cleanFur() {
64+
System.out.println(name + " cleans his fur");
65+
}
66+
67+
private void lickPaws() {
68+
System.out.println(name + " licks his paws");
69+
}
2970
}

src/test/java/com/serenitydojo/WhenCreatingObjects.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Assert;
44
import org.junit.Test;
5+
import static com.serenitydojo.Cat.usualFood;
56

67
public class WhenCreatingObjects {
78

@@ -24,4 +25,12 @@ public void creating_a_dog() {
2425
// Assert.assertEquals(fido.getAge(), 5);
2526

2627
}
28+
29+
@Test
30+
public void cat_makes_noise() {
31+
Cat felix = new Cat("Felix", 4);
32+
Cat spot = new Cat("Spot","Salmon", 3);
33+
34+
System.out.println("Cats like " + usualFood());
35+
}
2736
}

0 commit comments

Comments
 (0)