Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 59a6e35

Browse files
authored
Merge pull request #3 from jeffmorin/tech-review-ch02
Tech review ch02
2 parents 58889d9 + f4d4271 commit 59a6e35

File tree

4 files changed

+34
-37
lines changed

4 files changed

+34
-37
lines changed

‎CCSPiJ/src/chapter2/Gene.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ public enum Nucleotide {
2828

2929
public static class Codon implements Comparable<Codon> {
3030
public final Nucleotide first, second, third;
31+
private final Comparator<Codon> comparator = Comparator.comparing((Codon c) -> c.first)
32+
.thenComparing((Codon c) -> c.second)
33+
.thenComparing((Codon c) -> c.third);
3134

3235
public Codon(String codonStr) {
33-
first = Enum.valueOf(Nucleotide.class, codonStr.substring(0, 1));
34-
second = Enum.valueOf(Nucleotide.class, codonStr.substring(1, 2));
35-
third = Enum.valueOf(Nucleotide.class, codonStr.substring(2, 3));
36+
first = Nucleotide.valueOf(codonStr.substring(0, 1));
37+
second = Nucleotide.valueOf(codonStr.substring(1, 2));
38+
third = Nucleotide.valueOf(codonStr.substring(2, 3));
3639
}
3740

3841
@Override
3942
public int compareTo(Codon other) {
4043
// first is compared first, then second, etc.
4144
// IOW first takes precedence over second and second over third
42-
return Comparator.comparing((Codon c) -> c.first)
43-
.thenComparing((Codon c) -> c.second)
44-
.thenComparing((Codon c) -> c.third)
45-
.compare(this, other);
45+
return comparator.compare(this, other);
4646
}
4747
}
4848

‎CCSPiJ/src/chapter2/GenericSearch.java‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.HashSet;
2222
import java.util.LinkedList;
2323
import java.util.List;
24+
import java.util.Map;
2425
import java.util.PriorityQueue;
2526
import java.util.Queue;
2627
import java.util.Set;
@@ -31,7 +32,7 @@
3132

3233
public class GenericSearch {
3334

34-
public static <T extends Comparable<T>> boolean linearContains(List<T> list, T key) {
35+
public static <T extends Comparable<? superT>> boolean linearContains(List<T> list, T key) {
3536
for (T item : list) {
3637
if (item.compareTo(key) == 0) {
3738
return true; // found a match
@@ -41,7 +42,7 @@ public static <T extends Comparable<T>> boolean linearContains(List<T> list, T k
4142
}
4243

4344
// assumes *list* is already sorted
44-
public static <T extends Comparable<T>> boolean binaryContains(List<T> list, T key) {
45+
public static <T extends Comparable<? superT>> boolean binaryContains(List<T> list, T key) {
4546
int low = 0;
4647
int high = list.size() - 1;
4748
while (low <= high) { // while there is still a search space
@@ -161,7 +162,7 @@ public static <T> Node<T> astar(T initial, Predicate<T> goalTest,
161162
PriorityQueue<Node<T>> frontier = new PriorityQueue<>();
162163
frontier.offer(new Node<>(initial, null, 0.0, heuristic.applyAsDouble(initial)));
163164
// explored is where we've been
164-
HashMap<T, Double> explored = new HashMap<>();
165+
Map<T, Double> explored = new HashMap<>();
165166
explored.put(initial, 0.0);
166167
// keep going while there is more to explore
167168
while (!frontier.isEmpty()) {
@@ -186,9 +187,9 @@ public static <T> Node<T> astar(T initial, Predicate<T> goalTest,
186187
}
187188

188189
public static void main(String[] args) {
189-
System.out.println(GenericSearch.linearContains(List.of(1, 5, 15, 15, 15, 15, 20), 5)); // true
190-
System.out.println(GenericSearch.binaryContains(List.of("a", "d", "e", "f", "z"), "f")); // true
191-
System.out.println(GenericSearch.binaryContains(List.of("john", "mark", "ronald", "sarah"), "sheila")); // false
190+
System.out.println(linearContains(List.of(1, 5, 15, 15, 15, 15, 20), 5)); // true
191+
System.out.println(binaryContains(List.of("a", "d", "e", "f", "z"), "f")); // true
192+
System.out.println(binaryContains(List.of("john", "mark", "ronald", "sarah"), "sheila")); // false
192193
}
193194

194195
}

‎CCSPiJ/src/chapter2/MCState.java‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424

2525
public class MCState {
2626
private static final int MAX_NUM = 3;
27-
finalprivate int wm; // west bank missionaries
28-
finalprivate int wc; // west bank cannibals
29-
finalprivate int em; // east bank missionaries
30-
finalprivate int ec; // east bank cannibals
31-
finalprivate boolean boat; // is boat on west bank?
27+
privatefinal int wm; // west bank missionaries
28+
privatefinal int wc; // west bank cannibals
29+
privatefinal int em; // east bank missionaries
30+
privatefinal int ec; // east bank cannibals
31+
privatefinal boolean boat; // is boat on west bank?
3232

3333
public MCState(int missionaries, int cannibals, boolean boat) {
3434
wm = missionaries;
@@ -109,13 +109,11 @@ public static void displaySolution(List<MCState> path) {
109109
System.out.println(oldState);
110110
for (MCState currentState : path.subList(1, path.size())) {
111111
if (currentState.boat) {
112-
System.out.printf("%d missionaries and %d cannibals moved from the east bank to the west bank." +
113-
System.lineSeparator(),
112+
System.out.printf("%d missionaries and %d cannibals moved from the east bank to the west bank.%n",
114113
oldState.em - currentState.em,
115114
oldState.ec - currentState.ec);
116115
} else {
117-
System.out.printf("%d missionaries and %d cannibals moved from the west bank to the east bank." +
118-
System.lineSeparator(),
116+
System.out.printf("%d missionaries and %d cannibals moved from the west bank to the east bank.%n",
119117
oldState.wm - currentState.wm,
120118
oldState.wc - currentState.wc);
121119
}

‎CCSPiJ/src/chapter2/Maze.java‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,21 @@
2525
public class Maze {
2626

2727
public enum Cell {
28-
EMPTY, BLOCKED, START, GOAL, PATH;
28+
EMPTY(" "),
29+
BLOCKED("X"),
30+
START("S"),
31+
GOAL("G"),
32+
PATH("*");
33+
34+
private final String code;
35+
36+
private Cell(String c) {
37+
code = c;
38+
}
2939

3040
@Override
3141
public String toString() {
32-
switch (this) {
33-
case EMPTY:
34-
return (" ");
35-
case BLOCKED:
36-
return ("X");
37-
case START:
38-
return ("S");
39-
case GOAL:
40-
return ("G");
41-
case PATH:
42-
return ("*");
43-
}
44-
return null; // should never get here
42+
return code;
4543
}
4644
}
4745

@@ -129,7 +127,7 @@ public String toString() {
129127
StringBuilder sb = new StringBuilder();
130128
for (Cell[] row : grid) {
131129
for (Cell cell : row) {
132-
sb.append(cell.toString());
130+
sb.append(cell);
133131
}
134132
sb.append(System.lineSeparator());
135133
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /