-
Notifications
You must be signed in to change notification settings - Fork 48
/
ch01_Intro.java
123 lines (94 loc) · 3.24 KB
/
ch01_Intro.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.util.ArrayList;
import java.util.List;
public class ch01_Intro {
static class Author {}
class Book {
private String title;
private List<Author> authors;
public Book(String title) {
this.title = title;
this.authors = new ArrayList<Author>();
}
public void addAuthor(Author author) {
this.authors.add(author);
}
}
static class NotEnoughIngredientsException extends RuntimeException {}
private static void add(String ingredient) {}
private static void heatUpUntilBoiling() {}
private static void addVegetablesUsing(List<String> ingredients) {}
private static void waitMinutes(int minutes) {}
public void makeSoup(List<String> ingredients) {
if(ingredients.contains("water")) {
add("water");
} else throw new NotEnoughIngredientsException();
heatUpUntilBoiling();
addVegetablesUsing(ingredients);
waitMinutes(20);
}
public static class Soup {}
public static int add(int a, int b) {
return a + b;
}
public static char getFirstCharacter(String s) {
return s.charAt(0);
}
public static int divide(int a, int b) {
return a / b;
}
public static void eatSoup(Soup soup) {
// TODO: “eating the soup” algorithm
}
public static int increment(int x) {
return x + 1;
}
public static String concatenate(String a, String b) {
return a + b;
}
public static int calculateScore(String word) {
int score = 0;
for(char c : word.toCharArray()) {
score++;
}
return score;
}
public static int wordScore(String word) {
return word.length();
}
// Coffee Break exercise
public static int calculateScore2(String word) { // named calculateScore in the book
int score = 0;
for(char c : word.toCharArray()) {
if(c != 'a')
score++;
}
return score;
}
public static int wordScore2(String word) { // named wordScore in the book
return word.replace("a", "").length();
}
public static String stringWithoutChar(String s, char c) {
return s.replace(Character.toString(c), "");
}
public static int wordScore3(String word) { // named wordScore in the book
return stringWithoutChar(word, 'a').length();
}
public static void main(String[] args) {
System.out.println(add(2, 4));
assert(add(2, 4) == 6);
System.out.println(getFirstCharacter("Ola"));
assert(getFirstCharacter("Ola") == 'O');
System.out.println(divide(12, 2));
assert(divide(12, 2) == 6);
eatSoup(new Soup());
System.out.println(increment(6));
assert(increment(6) == 7);
System.out.println(concatenate("6", "7"));
assert(concatenate("6", "7").equals("67"));
System.out.println(calculateScore("imperative"));
assert(wordScore("imperative") == 10);
System.out.println(wordScore("declarative"));
assert(wordScore("declarative") == 11);
assert(calculateScore2("imperative") == 9 && wordScore2("declarative") == 9 && wordScore3("declarative") == 9);
}
}