diff --git a/Checkstyle.xml b/Checkstyle.xml index 6cf2192..36dbd1f 100644 --- a/Checkstyle.xml +++ b/Checkstyle.xml @@ -1,85 +1,111 @@ + "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN" + "https://checkstyle.org/dtds/configuration_1_3.dtd"> + - + - + - + + - + - + + + + + - - + + + - + - + - + - + + + + + + + + + + + + + + + + + + + - + + + + - + - + + @@ -92,42 +118,38 @@ Checkstyle configuration for Think Java, 2nd Edition. - + - - + - + + + - - - + + - + diff --git a/Formatter.xml b/Formatter.xml index f97740d..ab6e68e 100644 --- a/Formatter.xml +++ b/Formatter.xml @@ -1,318 +1,390 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ch06/Strings.java b/ch06/Strings.java index ac6bc05..f8b40f0 100644 --- a/ch06/Strings.java +++ b/ch06/Strings.java @@ -38,6 +38,7 @@ public static void main(String[] args) { System.out.print("Enter a number: "); } double number = in.nextDouble(); + in.nextLine(); // read the newline // String iteration diff --git a/ch07/ArrayExamples.java b/ch07/ArrayExamples.java index 0ac26f3..9fecdc0 100644 --- a/ch07/ArrayExamples.java +++ b/ch07/ArrayExamples.java @@ -30,6 +30,7 @@ public static void main(String[] args) { System.out.println(counts[i]); } + // displaying arrays int[] array = {1, 2, 3, 4}; printArray(array); @@ -49,9 +50,9 @@ public static void main(String[] args) { // copying with Arrays class double[] c = Arrays.copyOf(a, a.length); - // traversal + // traversing arrays for (int i = 0; i < a.length; i++) { - a[i] = Math.pow(a[i], 2.0); + a[i] *= a[i]; } // search @@ -66,10 +67,10 @@ public static void main(String[] args) { /** * Prints the elements of an array. */ - public static void printArray(int[] array) { - System.out.print("{" + array[0]); - for (int i = 1; i < array.length; i++) { - System.out.print(", " + array[i]); + public static void printArray(int[] a) { + System.out.print("{" + a[0]); + for (int i = 1; i < a.length; i++) { + System.out.print(", " + a[i]); } System.out.println("}"); } @@ -77,22 +78,22 @@ public static void printArray(int[] array) { /** * Returns the index of the target in the array, or -1 if not found. */ - public static int search(double[] a, double target) { - for (int i = 0; i < a.length; i++) { - if (a[i] == target) { + public static int search(double[] array, double target) { + for (int i = 0; i < array.length; i++) { + if (array[i] == target) { return i; } } - return -1; + return -1; // not found } /** * Returns the total of the elements in an array. */ - public static double sum(double[] a) { + public static double sum(double[] array) { double total = 0.0; - for (int i = 0; i < a.length; i++) { - total += a[i]; + for (int i = 0; i < array.length; i++) { + total += array[i]; } return total; } diff --git a/ch07/Doubloon.java b/ch07/Doubloon.java index 61103e0..aba5ed7 100644 --- a/ch07/Doubloon.java +++ b/ch07/Doubloon.java @@ -2,7 +2,7 @@ * Example from the end of Chapter 7. */ public class Doubloon { - + public static boolean isDoubloon(String s) { // count the number of times each letter appears @@ -21,10 +21,9 @@ public static boolean isDoubloon(String s) { } return true; } - + public static void main(String[] args) { - System.out.println(isDoubloon("Mama")); - System.out.println(isDoubloon("Lama")); + System.out.println(isDoubloon("Mama")); // true + System.out.println(isDoubloon("Lama")); // false } - } diff --git a/ch07/Histogram.java b/ch07/Histogram.java index 32b029c..fe9f0b7 100644 --- a/ch07/Histogram.java +++ b/ch07/Histogram.java @@ -31,8 +31,7 @@ public static int inRange(int[] a, int low, int high) { } public static void main(String[] args) { - int numValues = 8; - int[] array = randomArray(numValues); + int[] array = randomArray(8); ArrayExamples.printArray(array); int[] scores = randomArray(30); diff --git a/ch08/CodingBat.java b/ch08/CodingBat.java index c51f2f4..f6ad0d5 100644 --- a/ch08/CodingBat.java +++ b/ch08/CodingBat.java @@ -3,26 +3,35 @@ */ public class CodingBat { - public static String noX(String str) { + /** + * See https://codingbat.com/prob/p118230. + */ + public String noX(String str) { if (str.length() == 0) { return ""; } - char c = str.charAt(0); - if (c == 'x') { - return noX(str.substring(1)); + char first = str.charAt(0); + String rest = str.substring(1); + String recurse = noX(rest); + if (first == 'x') { + return recurse; } else { - return c + noX(str.substring(1)); + return first + recurse; } } + /** + * See https://codingbat.com/prob/p135988. + */ public int array11(int[] nums, int index) { if (index >= nums.length) { return 0; } + int recurse = array11(nums, index + 1); if (nums[index] == 11) { - return 1 + array11(nums, index + 1); + return recurse + 1; } else { - return array11(nums, index + 1); + return recurse; } } diff --git a/ch08/Recursion.java b/ch08/Examples.java similarity index 92% rename from ch08/Recursion.java rename to ch08/Examples.java index 04ac198..39b22dd 100644 --- a/ch08/Recursion.java +++ b/ch08/Examples.java @@ -1,19 +1,4 @@ -public class Recursion { - - public static void main(String[] args) { - System.out.println("countdown"); - countdown(3); - - System.out.println("nLines"); - nLines(3); - - System.out.println("countup"); - countup(3); - - System.out.println("displayBinary"); - displayBinary(23); - System.out.println(); - } +public class Examples { public static void countdown(int n) { if (n == 0) { @@ -24,10 +9,6 @@ public static void countdown(int n) { } } - public static void newLine() { - System.out.println(); - } - public static void nLines(int n) { if (n > 0) { System.out.println(); @@ -56,4 +37,21 @@ public static void displayBinary(int value) { } } + public static void main(String[] args) { + System.out.println("countdown"); + countdown(3); + + System.out.println("nLines"); + nLines(3); + + // forever("Ha!"); + + System.out.println("countup"); + countup(3); + + System.out.println("displayBinary"); + displayBinary(23); + System.out.println(); + } + } diff --git a/ch08/Series.java b/ch08/Series.java index 86e8148..fb1c860 100644 --- a/ch08/Series.java +++ b/ch08/Series.java @@ -1,6 +1,3 @@ -/** - * Examples from Chapter 8. - */ public class Series { public static int factorial(int n) { diff --git a/ch09/ArgValid.java b/ch09/ArgValid.java new file mode 100644 index 0000000..29e4d0a --- /dev/null +++ b/ch09/ArgValid.java @@ -0,0 +1,20 @@ +/** + * Example of argument validation. + */ +public class ArgValid { + + public static boolean isCapitalized(String str) { + if (str == null || str.isEmpty()) { + return false; + } + return Character.isUpperCase(str.charAt(0)); + } + + public static void main(String[] args) { + System.out.println(isCapitalized("Hi!")); + System.out.println(isCapitalized("bye")); + System.out.println(isCapitalized("")); + System.out.println(isCapitalized(null)); + } + +} diff --git a/ch09/BigInt.java b/ch09/BigInt.java new file mode 100644 index 0000000..05b4da5 --- /dev/null +++ b/ch09/BigInt.java @@ -0,0 +1,20 @@ +import java.math.BigInteger; + +/** + * BigInteger examples. + */ +public class BigInt { + + public static void main(String[] args) { + long x = 17; + BigInteger big = BigInteger.valueOf(x); + + String s = "12345678901234567890"; + BigInteger bigger = new BigInteger(s); + + BigInteger a = BigInteger.valueOf(17); + BigInteger b = BigInteger.valueOf(1700000000); + BigInteger c = a.add(b); + } + +} diff --git a/ch09/Objects.java b/ch09/Objects.java index 6dde3a0..ae7b350 100644 --- a/ch09/Objects.java +++ b/ch09/Objects.java @@ -1,5 +1,3 @@ -import java.math.BigInteger; - /** * Demonstrates uses of objects and wrappers. */ @@ -14,22 +12,40 @@ public static void main(String[] args) { char[] array = {'c', 'a', 't'}; String word = "dog"; + String word1 = new String("dog"); // creates a string object + String word2 = "dog"; // implicitly creates a string object + + // the null keyword + + String name0 = null; + int[] combo = null; + // System.out.println(name0.length()); // NullPointerException + // System.out.println(combo[0]); // NullPointerException + // Strings are immutable String name = "Alan Turing"; String upperName = name.toUpperCase(); + name.toUpperCase(); // ignores the return value + System.out.println(name); + name = name.toUpperCase(); // references the new string + System.out.println(name); + String text = "Computer Science is fun!"; text = text.replace("Computer Science", "CS"); // Wrapper classes + Integer i = Integer.valueOf(5); + System.out.println(i.equals(5)); // displays true + Integer x = Integer.valueOf(123); Integer y = Integer.valueOf(123); - if (x == y) { // false + if (x == y) { // false System.out.println("x and y are the same object"); } - if (x.equals(y)) { // true + if (x.equals(y)) { // true System.out.println("x and y have the same value"); } @@ -38,18 +54,6 @@ public static void main(String[] args) { num = 12345; str = Integer.toString(num); - - // BigInteger arithmetic - - long z = 17; - BigInteger big = BigInteger.valueOf(z); - - String s = "12345678901234567890"; - BigInteger bigger = new BigInteger(s); - - BigInteger a = BigInteger.valueOf(17); - BigInteger b = BigInteger.valueOf(1700000000); - BigInteger c = a.add(b); } } diff --git a/ch09/Tables.java b/ch09/Tables.java index f1a5423..1ceaec9 100644 --- a/ch09/Tables.java +++ b/ch09/Tables.java @@ -4,73 +4,61 @@ public class Tables { public static void printRow() { - int i = 1; - while (i <= 6) { + for (int i = 1; i <= 6; i++) { System.out.printf("%4d", 2 * i); - i = i + 1; } System.out.println(); } public static void printRow(int n) { - int i = 1; - while (i <= 6) { + for (int i = 1; i <= 6; i++) { System.out.printf("%4d", n * i); // generalized n - i = i + 1; } System.out.println(); } public static void printTable() { - int i = 1; - while (i <= 6) { + for (int i = 1; i <= 6; i++) { printRow(i); - i = i + 1; } } public static void printTable(int rows) { - int i = 1; - while (i <= rows) { // generalized rows + for (int i = 1; i <= rows; i++) { // generalized rows printRow(i); - i = i + 1; } } public static void printRow(int n, int cols) { - int i = 1; - while (i <= cols) { // generalized cols + for (int i = 1; i <= cols; i++) { // generalized cols System.out.printf("%4d", n * i); - i = i + 1; } System.out.println(); } public static void printTable2(int rows) { - int i = 1; - while (i <= rows) { - printRow(i, rows); // added rows argument - i = i + 1; + for (int i = 1; i <= rows; i++) { + printRow(i, rows); } } public static void main(String[] args) { - System.out.println("printRow()"); + System.out.println("\nprintRow()"); printRow(); - System.out.println("printRow(6)"); + System.out.println("\nprintRow(6)"); printRow(6); - System.out.println("printTable()"); + System.out.println("\nprintTable()"); printTable(); - System.out.println("printTable(6)"); + System.out.println("\nprintTable(6)"); printTable(6); - System.out.println("printRow(6, 6)"); + System.out.println("\nprintRow(6, 6)"); printRow(6, 6); - System.out.println("printTable2(6)"); + System.out.println("\nprintTable2(6)"); printTable2(6); } diff --git a/ch10/Append.java b/ch10/Append.java deleted file mode 100644 index 98a9c10..0000000 --- a/ch10/Append.java +++ /dev/null @@ -1,13 +0,0 @@ -import java.util.Scanner; -public class Append { - public static void main(String[] args) { - Scanner in = new Scanner(System.in); - System.out.println("Enter 10 lines:"); - String text = ""; - for (int i = 0; i < 10; i++) { - String line = in.nextLine(); // new string - text = text + line + '\n'; // two more strings - } - System.out.print("You entered:\n" + text); - } -} diff --git a/ch10/Builder.java b/ch10/Builder.java new file mode 100644 index 0000000..8fe955e --- /dev/null +++ b/ch10/Builder.java @@ -0,0 +1,35 @@ +import java.util.Scanner; + +/* + * StringBuilder example. + */ +public class Builder { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + + // less efficient + System.out.println("Enter 10 lines:"); + + String slow = ""; + for (int i = 0; i < 10; i++) { + String line = in.nextLine(); // new string + slow = slow + line + '\n'; // two more strings + } + System.out.print("You entered:\n" + slow); + + // more efficient + System.out.println("Enter 10 lines:"); + + StringBuilder text = new StringBuilder(); + for (int i = 0; i < 10; i++) { + String line = in.nextLine(); + text.append(line); + text.append('\n'); + } + System.out.print("You entered:\n" + text); + + String result = text.toString(); + } + +} diff --git a/ch10/PointRect.java b/ch10/PointRect.java index cb0af07..301dd90 100644 --- a/ch10/PointRect.java +++ b/ch10/PointRect.java @@ -9,27 +9,40 @@ public class PointRect { public static void main(String[] args) { Point blank; blank = new Point(3, 4); - System.out.println(blank); int x = blank.x; System.out.println(blank.x + ", " + blank.y); int sum = blank.x * blank.x + blank.y * blank.y; + // objects as parameters + Point p1 = new Point(0, 0); Point p2 = new Point(3, 4); + double temp = distance(p1, p2); double dist = p1.distance(p2); // dist is 5.0 + System.out.println(blank); + + // objects as return values Rectangle box = new Rectangle(0, 0, 100, 200); - moveRect(box, 50, 100); + Point center = findCenter(box); + + // rectangles are mutable + + moveRect(box, 50, 100); // now at (50, 100, 100, 200) System.out.println(box); + + box = new Rectangle(0, 0, 100, 200); box.translate(50, 100); + // aliasing revisited + Rectangle box1 = new Rectangle(0, 0, 100, 200); Rectangle box2 = box1; - System.out.println(box2.width); box1.grow(50, 50); - System.out.println(box2.width); + System.out.println(box1); + System.out.println(box2); } /** diff --git a/ch11/Time.java b/ch11/Time.java index 951d5bf..fe5bfd6 100644 --- a/ch11/Time.java +++ b/ch11/Time.java @@ -25,6 +25,30 @@ public Time(int hour, int minute, double second) { this.second = second; } + public int getHour() { + return this.hour; + } + + public int getMinute() { + return this.minute; + } + + public double getSecond() { + return this.second; + } + + public void setHour(int hour) { + this.hour = hour; + } + + public void setMinute(int minute) { + this.minute = minute; + } + + public void setSecond(double second) { + this.second = second; + } + /** * Prints the time in a simple format. */ @@ -48,9 +72,10 @@ public String toString() { * Tests whether two times are equivalent. */ public boolean equals(Time that) { + final double DELTA = 0.001; return this.hour == that.hour && this.minute == that.minute - && this.second == that.second; + && Math.abs(this.second - that.second) < DELTA; } /** @@ -72,6 +97,7 @@ public Time add(Time t2) { sum.hour = this.hour + t2.hour; sum.minute = this.minute + t2.minute; sum.second = this.second + t2.second; + if (sum.second >= 60.0) { sum.second -= 60.0; sum.minute += 1; @@ -80,22 +106,10 @@ public Time add(Time t2) { sum.minute -= 60; sum.hour += 1; } - return sum; - } - - /** - * Adds the given number of seconds to this object (modifier). - */ - public void increment(double seconds) { - this.second += seconds; - while (this.second >= 60.0) { - this.second -= 60.0; - this.minute += 1; - } - while (this.minute >= 60) { - this.minute -= 60; - this.hour += 1; + if (sum.hour >= 24) { + sum.hour -= 24; } + return sum; } } diff --git a/ch15/Cell.java b/ch15/Cell.java index d468b6d..5b6a0f9 100644 --- a/ch15/Cell.java +++ b/ch15/Cell.java @@ -3,6 +3,9 @@ /** * A square at a fixed location that changes color. + * + * @author Chris Mayfield + * @version 7.1.0 */ public class Cell { @@ -40,28 +43,32 @@ public void draw(Graphics g) { } /** - * @return true if the cell is OFF + * Tests whether the cell is off. + * + * @return true if the cell is off */ public boolean isOff() { return state == 0; } /** - * @return true if the cell is ON + * Tests whether the cell is on. + * + * @return true if the cell is on */ public boolean isOn() { return state == 1; } /** - * Sets the cell's state to OFF. + * Sets the cell's state to off. */ public void turnOff() { state = 0; } /** - * Sets the cell's state to ON. + * Sets the cell's state to on. */ public void turnOn() { state = 1; diff --git a/ch15/Conway.java b/ch15/Conway.java index 93faf28..5ac48c6 100644 --- a/ch15/Conway.java +++ b/ch15/Conway.java @@ -2,6 +2,9 @@ /** * Conway's Game of Life. + * + * @author Chris Mayfield + * @version 7.1.0 */ public class Conway { @@ -109,8 +112,6 @@ public void update() { /** * The simulation loop. - * - * @param rate frames per second */ private void mainloop() { while (true) { diff --git a/ch15/GridCanvas.java b/ch15/GridCanvas.java index c936135..3856a57 100644 --- a/ch15/GridCanvas.java +++ b/ch15/GridCanvas.java @@ -3,6 +3,9 @@ /** * 2D array of cells representing a rectangular grid. + * + * @author Chris Mayfield + * @version 7.1.0 */ public class GridCanvas extends Canvas { @@ -33,6 +36,8 @@ public GridCanvas(int rows, int cols, int size) { } /** + * Gets the number of rows. + * * @return number of rows */ public int numRows() { @@ -40,6 +45,8 @@ public int numRows() { } /** + * Gets the number of columns. + * * @return number of columns */ public int numCols() { @@ -47,6 +54,8 @@ public int numCols() { } /** + * Gets the cell at index (r, c). + * * @param r row index * @param c column index * @return the cell diff --git a/user.dict b/user.dict new file mode 100644 index 0000000..11b2406 --- /dev/null +++ b/user.dict @@ -0,0 +1,2 @@ +chris +mayfield