From 75c90adb3a28754a085c16afffe9718767a562bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Morin?= Date: Fri, 17 Jul 2020 13:51:39 -0400 Subject: [PATCH 1/2] Proofed chapter 2 --- CCSPiJ/src/chapter2/Gene.java | 14 +++++++------- CCSPiJ/src/chapter2/GenericSearch.java | 13 +++++++------ CCSPiJ/src/chapter2/MCState.java | 16 +++++++--------- CCSPiJ/src/chapter2/Maze.java | 26 ++++++++++++-------------- CCSPiJ/src/module-info.java | 2 -- 5 files changed, 33 insertions(+), 38 deletions(-) delete mode 100644 CCSPiJ/src/module-info.java diff --git a/CCSPiJ/src/chapter2/Gene.java b/CCSPiJ/src/chapter2/Gene.java index dcbcc3f..022eef7 100644 --- a/CCSPiJ/src/chapter2/Gene.java +++ b/CCSPiJ/src/chapter2/Gene.java @@ -28,21 +28,21 @@ public enum Nucleotide { public static class Codon implements Comparable { public final Nucleotide first, second, third; + private final Comparator comparator = Comparator.comparing((Codon c) -> c.first) + .thenComparing((Codon c) -> c.second) + .thenComparing((Codon c) -> c.third); public Codon(String codonStr) { - first = Enum.valueOf(Nucleotide.class, codonStr.substring(0, 1)); - second = Enum.valueOf(Nucleotide.class, codonStr.substring(1, 2)); - third = Enum.valueOf(Nucleotide.class, codonStr.substring(2, 3)); + first = Nucleotide.valueOf(codonStr.substring(0, 1)); + second = Nucleotide.valueOf(codonStr.substring(1, 2)); + third = Nucleotide.valueOf(codonStr.substring(2, 3)); } @Override public int compareTo(Codon other) { // first is compared first, then second, etc. // IOW first takes precedence over second and second over third - return Comparator.comparing((Codon c) -> c.first) - .thenComparing((Codon c) -> c.second) - .thenComparing((Codon c) -> c.third) - .compare(this, other); + return comparator.compare(this, other); } } diff --git a/CCSPiJ/src/chapter2/GenericSearch.java b/CCSPiJ/src/chapter2/GenericSearch.java index 5a2408e..a2566ca 100644 --- a/CCSPiJ/src/chapter2/GenericSearch.java +++ b/CCSPiJ/src/chapter2/GenericSearch.java @@ -21,6 +21,7 @@ import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; @@ -31,7 +32,7 @@ public class GenericSearch { - public static > boolean linearContains(List list, T key) { + public static > boolean linearContains(List list, T key) { for (T item : list) { if (item.compareTo(key) == 0) { return true; // found a match @@ -41,7 +42,7 @@ public static > boolean linearContains(List list, T k } // assumes *list* is already sorted - public static > boolean binaryContains(List list, T key) { + public static > boolean binaryContains(List list, T key) { int low = 0; int high = list.size() - 1; while (low <= high) { // while there is still a search space @@ -161,7 +162,7 @@ public static Node astar(T initial, Predicate goalTest, PriorityQueue> frontier = new PriorityQueue<>(); frontier.offer(new Node<>(initial, null, 0.0, heuristic.applyAsDouble(initial))); // explored is where we've been - HashMap explored = new HashMap<>(); + Map explored = new HashMap<>(); explored.put(initial, 0.0); // keep going while there is more to explore while (!frontier.isEmpty()) { @@ -186,9 +187,9 @@ public static Node astar(T initial, Predicate goalTest, } public static void main(String[] args) { - System.out.println(GenericSearch.linearContains(List.of(1, 5, 15, 15, 15, 15, 20), 5)); // true - System.out.println(GenericSearch.binaryContains(List.of("a", "d", "e", "f", "z"), "f")); // true - System.out.println(GenericSearch.binaryContains(List.of("john", "mark", "ronald", "sarah"), "sheila")); // false + System.out.println(linearContains(List.of(1, 5, 15, 15, 15, 15, 20), 5)); // true + System.out.println(binaryContains(List.of("a", "d", "e", "f", "z"), "f")); // true + System.out.println(binaryContains(List.of("john", "mark", "ronald", "sarah"), "sheila")); // false } } diff --git a/CCSPiJ/src/chapter2/MCState.java b/CCSPiJ/src/chapter2/MCState.java index d9d7b1d..18101a2 100644 --- a/CCSPiJ/src/chapter2/MCState.java +++ b/CCSPiJ/src/chapter2/MCState.java @@ -24,11 +24,11 @@ public class MCState { private static final int MAX_NUM = 3; - final private int wm; // west bank missionaries - final private int wc; // west bank cannibals - final private int em; // east bank missionaries - final private int ec; // east bank cannibals - final private boolean boat; // is boat on west bank? + private final int wm; // west bank missionaries + private final int wc; // west bank cannibals + private final int em; // east bank missionaries + private final int ec; // east bank cannibals + private final boolean boat; // is boat on west bank? public MCState(int missionaries, int cannibals, boolean boat) { wm = missionaries; @@ -109,13 +109,11 @@ public static void displaySolution(List path) { System.out.println(oldState); for (MCState currentState : path.subList(1, path.size())) { if (currentState.boat) { - System.out.printf("%d missionaries and %d cannibals moved from the east bank to the west bank." + - System.lineSeparator(), + System.out.printf("%d missionaries and %d cannibals moved from the east bank to the west bank.%n", oldState.em - currentState.em, oldState.ec - currentState.ec); } else { - System.out.printf("%d missionaries and %d cannibals moved from the west bank to the east bank." + - System.lineSeparator(), + System.out.printf("%d missionaries and %d cannibals moved from the west bank to the east bank.%n", oldState.wm - currentState.wm, oldState.wc - currentState.wc); } diff --git a/CCSPiJ/src/chapter2/Maze.java b/CCSPiJ/src/chapter2/Maze.java index 1632d71..8b81960 100644 --- a/CCSPiJ/src/chapter2/Maze.java +++ b/CCSPiJ/src/chapter2/Maze.java @@ -25,23 +25,21 @@ public class Maze { public enum Cell { - EMPTY, BLOCKED, START, GOAL, PATH; + EMPTY(" "), + BLOCKED("X"), + START("S"), + GOAL("G"), + PATH("*"); + + private final String code; + + private Cell(String c) { + code = c; + } @Override public String toString() { - switch (this) { - case EMPTY: - return (" "); - case BLOCKED: - return ("X"); - case START: - return ("S"); - case GOAL: - return ("G"); - case PATH: - return ("*"); - } - return null; // should never get here + return code; } } diff --git a/CCSPiJ/src/module-info.java b/CCSPiJ/src/module-info.java deleted file mode 100644 index c407873..0000000 --- a/CCSPiJ/src/module-info.java +++ /dev/null @@ -1,2 +0,0 @@ -module chapter1 { -} \ No newline at end of file From f4d427103e1ca2cd323f971f341e4e3a5e275f04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Morin?= Date: Tue, 21 Jul 2020 21:57:25 -0400 Subject: [PATCH 2/2] Fixed a .toString() --- CCSPiJ/src/chapter2/Maze.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CCSPiJ/src/chapter2/Maze.java b/CCSPiJ/src/chapter2/Maze.java index 8b81960..1562a3c 100644 --- a/CCSPiJ/src/chapter2/Maze.java +++ b/CCSPiJ/src/chapter2/Maze.java @@ -127,7 +127,7 @@ public String toString() { StringBuilder sb = new StringBuilder(); for (Cell[] row : grid) { for (Cell cell : row) { - sb.append(cell.toString()); + sb.append(cell); } sb.append(System.lineSeparator()); }