From 5bc037ba87865db163348261e0fedec9f369da6d Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Wed, 20 Apr 2016 16:55:30 -0400 Subject: [PATCH 1/8] stubs for ch13 --- ch13/Deck.java | 126 +++---------------------------------------------- 1 file changed, 6 insertions(+), 120 deletions(-) diff --git a/ch13/Deck.java b/ch13/Deck.java index d567844..864d52e 100644 --- a/ch13/Deck.java +++ b/ch13/Deck.java @@ -6,10 +6,6 @@ */ public class Deck { - // This is a class variable so we don't have to create - // a new Random object every time we call randomInt. - private static Random random = new Random(); - private Card[] cards; /** @@ -41,65 +37,24 @@ public Card[] getCards() { } /** - * Returns a string representation of the deck. - */ - public String toString() { - return Arrays.toString(this.cards); - } - - /** - * Swaps the cards at indexes i and j. + * Displays each of the cards in the deck. */ - public void swapCards(int i, int j) { - Card temp = this.cards[i]; - this.cards[i] = this.cards[j]; - this.cards[j] = temp; - } - - /** - * Chooses a random number between low and high, including both. - */ - public int randomInt(int low, int high) { - int range = high - low + 1; - return low + random.nextInt(range); + public void print() { + for (int i = 0; i < this.cards.length; i++) { + System.out.println(this.cards[i]); + } } /** * Randomly permutes the array of cards. */ public void shuffle() { - for (int i = 0; i < this.cards.length - 1; i++) { - int j = this.randomInt(i, this.cards.length - 1); - this.swapCards(i, j); - } - } - - /** - * Finds the index of the lowest card - * between low and high inclusive. - */ - public int indexLowest(int low, int high) { - int index = low; - Card minCard = this.cards[low]; - for (int i = low + 1; i <= high; i++) { - Card card = this.cards[i]; - if (card.compareTo(minCard) < 0) { - index = i; - minCard = card; - } - } - return index; } /** * Sorts the cards (in place) using selection sort. */ public void selectionSort() { - int high = this.cards.length - 1; - for (int i = 0; i < this.cards.length; i++) { - int j = this.indexLowest(i, high); - this.swapCards(i, j); - } } /** @@ -113,85 +68,16 @@ public Deck subdeck(int low, int high) { return sub; } - /** - * Combines two previously sorted subdecks. - */ - public static Deck merge(Deck d1, Deck d2) { - Card[] c1 = d1.cards; - Card[] c2 = d2.cards; - Deck result = new Deck(c1.length + c2.length); - Card[] c3 = result.cards; - int i = 0; // index in c1 - int j = 0; // index in c2 - - // for each index in the result - for (int k = 0; k < c3.length; k++) { - int choice; - - // determine which card to merge next - if (i >= c1.length) { - choice = 2; // c1 is empty - } else if (j >= c2.length) { - choice = 1; // c2 is empty - } else if (c1[i].compareTo(c2[j]) < 0) { - choice = 1; // c1 is lower - } else { - choice = 2; // c2 is lower - } - - // store the chosen card in the result - if (choice == 1) { - c3[k] = c1[i]; - i++; - } else { - c3[k] = c2[j]; - j++; - } - } - return result; - } - /** * Returns a sorted copy of the deck using merge sort. */ public Deck mergeSort() { - - // 0 or 1 cards, already sorted - int len = this.cards.length; - if (len < 2) { - return this; - } - - // cut the deck about in half - int mid = len / 2; - Deck d1 = this.subdeck(0, mid - 1); - Deck d2 = this.subdeck(mid, len - 1); - - // sort each half and merge - d1 = d1.mergeSort(); - d2 = d2.mergeSort(); - return merge(d1, d2); + return this; } /** * Reorders the cards (in place) using insertion sort. */ public void insertionSort() { - for (int i = 1; i < this.cards.length; i++) { - Card card = this.cards[i]; - this.insert(card, i); - } - } - - /** - * Helper method for insertion sort. - */ - private void insert(Card card, int i) { - int j = i; - while (j > 0 && card.compareTo(this.cards[j - 1]) < 0) { - this.cards[j] = this.cards[j - 1]; - j--; - } - this.cards[j] = card; } } From aa70d99c54de2dcbac42787094337e56356f61a7 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Thu, 28 Apr 2016 16:47:22 -0400 Subject: [PATCH 2/8] review ch1-7 --- ch02/Variables.java | 15 ++++++------ ch03/GuessStarter.java | 2 +- ch06/Recursive.java | 2 +- ch06/Series.java | 34 +++++++++++++-------------- ch06/SeriesTest.java | 3 +++ ch07/Loops.java | 52 +++-------------------------------------- ch07/Tables.java | 2 +- ch07/Validate.java | 53 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 86 insertions(+), 77 deletions(-) create mode 100644 ch07/Validate.java diff --git a/ch02/Variables.java b/ch02/Variables.java index b4a99d6..0312023 100644 --- a/ch02/Variables.java +++ b/ch02/Variables.java @@ -14,14 +14,14 @@ public static void main(String[] args) { int hour, minute; message = "Hello!"; // give message the value "Hello!" - hour = 10; // assign the value 10 to hour + hour = 11; // assign the value 11 to hour minute = 59; // set minute to 59 - message = "123"; // legal - // message = 123; not legal + message = "123"; // legal + // message = 123; // not legal String message2 = "Hello!"; - int hour2 = 10; + int hour2 = 11; int minute2 = 59; int a = 5; @@ -43,7 +43,6 @@ public static void main(String[] args) { System.out.print("Number of minutes since midnight: "); System.out.println(hour * 60 + minute); - System.out.print("Fraction of the hour that has passed: "); System.out.println(minute / 60); @@ -61,10 +60,10 @@ public static void main(String[] args) { System.out.println(0.1 * 10); System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1 - + 0.1 + 0.1 + 0.1 + 0.1 + 0.1); + + 0.1 + 0.1 + 0.1 + 0.1 + 0.1); double balance = 123.45; // potential rounding error - int balance2 = 12345; // total number of cents + int balance2 = 12345; // total number of cents System.out.println(1 + 2 + "Hello"); // the output is 3Hello @@ -79,6 +78,6 @@ public static void main(String[] args) { percentage = (minute * 100) / 60; hour = minute + 1; // correct - // minute + 1 = hour; syntax error + // minute + 1 = hour; // compiler error } } diff --git a/ch03/GuessStarter.java b/ch03/GuessStarter.java index 92d7f50..f3240c0 100644 --- a/ch03/GuessStarter.java +++ b/ch03/GuessStarter.java @@ -1,7 +1,7 @@ import java.util.Random; /** - * Starter code for the "guess my number" exercise. + * Starter code for the "Guess My Number" exercise. */ public class GuessStarter { diff --git a/ch06/Recursive.java b/ch06/Recursive.java index 0d29924..f045844 100644 --- a/ch06/Recursive.java +++ b/ch06/Recursive.java @@ -8,7 +8,7 @@ public static int prod(int m, int n) { if (m == n) { return n; } else { - int recurse = prod(m, n-1); + int recurse = prod(m, n - 1); int result = n * recurse; return result; } diff --git a/ch06/Series.java b/ch06/Series.java index 8ce7c30..6db5643 100644 --- a/ch06/Series.java +++ b/ch06/Series.java @@ -12,12 +12,12 @@ public static void countup(int n) { } } - public static double area(double radius) { - double area = Math.PI * radius * radius; - return area; + public static double calculateArea(double radius) { + double result = Math.PI * radius * radius; + return result; } - public static double area2(double radius) { + public static double calculateArea2(double radius) { return Math.PI * radius * radius; } @@ -59,13 +59,13 @@ public static double absoluteValue(double x) { public static double circleArea (double xc, double yc, double xp, double yp) { double radius = distance(xc, yc, xp, yp); - double area = area(radius); + double area = calculateArea(radius); return area; } - public static double area + public static double calculateArea (double xc, double yc, double xp, double yp) { - return area(distance(xc, yc, xp, yp)); + return calculateArea(distance(xc, yc, xp, yp)); } /** @@ -106,17 +106,17 @@ public static void main(String[] args) { countup(3); System.out.println("Have a nice day."); - System.out.println("area"); - System.out.println(area(3.0)); + System.out.println("calculateArea"); + System.out.println(calculateArea(3.0)); - System.out.println("area2"); - System.out.println(area2(3.0)); + System.out.println("calculateArea2"); + System.out.println(calculateArea2(3.0)); System.out.println("circleArea"); System.out.println(circleArea(1.0, 2.0, 4.0, 6.0)); - System.out.println("area with 4 doubles"); - System.out.println(area(1.0, 2.0, 4.0, 6.0)); + System.out.println("calculateArea with 4 doubles"); + System.out.println(calculateArea(1.0, 2.0, 4.0, 6.0)); System.out.println("absolute value"); System.out.println(absoluteValue(-2)); @@ -133,11 +133,11 @@ public static void main(String[] args) { System.out.println(isSingleDigit(2)); boolean bigFlag = !isSingleDigit2(17); - int i = 9; - if (isSingleDigit(i)) { - System.out.println("i is small"); + int z = 9; + if (isSingleDigit(z)) { + System.out.println("z is small"); } else { - System.out.println("i is big"); + System.out.println("z is big"); } System.out.println("factorial"); diff --git a/ch06/SeriesTest.java b/ch06/SeriesTest.java index 7517c85..93b5420 100644 --- a/ch06/SeriesTest.java +++ b/ch06/SeriesTest.java @@ -1,5 +1,8 @@ import junit.framework.TestCase; +/** + * Example JUnit test from Appendix A. + */ public class SeriesTest extends TestCase { public void testFibonacci() { diff --git a/ch07/Loops.java b/ch07/Loops.java index 58c7bd6..d5c915f 100644 --- a/ch07/Loops.java +++ b/ch07/Loops.java @@ -1,5 +1,6 @@ -import java.util.Scanner; - +/** + * Examples from Chapter 7. + */ public class Loops { public static void countdown(int n) { @@ -21,52 +22,6 @@ public static void sequence(int n) { } } - public static double scanDouble() { - Scanner in = new Scanner(System.in); - boolean okay; - do { - System.out.print("Enter a number: "); - if (in.hasNextDouble()) { - okay = true; - } else { - okay = false; - String word = in.next(); - System.err.println(word + " is not a number"); - } - } while (!okay); - double x = in.nextDouble(); - return x; - } - - public static double scanDouble2() { - Scanner in = new Scanner(System.in); - while (true) { - System.out.print("Enter a number: "); - if (in.hasNextDouble()) { - break; - } - String word = in.next(); - System.err.println(word + " is not a number"); - } - double x = in.nextDouble(); - return x; - } - - public static double addNumbers() { - Scanner in = new Scanner(System.in); - int x = -1; - int sum = 0; - while (x != 0) { - x = in.nextInt(); - if (x <= 0) { - continue; - } - System.out.println("Adding " + x); - sum += x; - } - return sum; - } - public static void main(String[] args) { System.out.println("countdown"); countdown(3); @@ -74,5 +29,4 @@ public static void main(String[] args) { System.out.println("sequence"); sequence(10); } - } diff --git a/ch07/Tables.java b/ch07/Tables.java index e586699..0fbfbbe 100644 --- a/ch07/Tables.java +++ b/ch07/Tables.java @@ -1,5 +1,5 @@ /** - * Examples from Chapter 7. + * Generating tables; encapsulation and generalization. */ public class Tables { diff --git a/ch07/Validate.java b/ch07/Validate.java new file mode 100644 index 0000000..eeb81da --- /dev/null +++ b/ch07/Validate.java @@ -0,0 +1,53 @@ +import java.util.Scanner; + +/** + * Do-while, break, and continue. + */ +public class Validate { + + public static double scanDouble() { + Scanner in = new Scanner(System.in); + boolean okay; + do { + System.out.print("Enter a number: "); + if (in.hasNextDouble()) { + okay = true; + } else { + okay = false; + String word = in.next(); + System.err.println(word + " is not a number"); + } + } while (!okay); + double x = in.nextDouble(); + return x; + } + + public static double scanDouble2() { + Scanner in = new Scanner(System.in); + while (true) { + System.out.print("Enter a number: "); + if (in.hasNextDouble()) { + break; + } + String word = in.next(); + System.err.println(word + " is not a number"); + } + double x = in.nextDouble(); + return x; + } + + public static double addNumbers() { + Scanner in = new Scanner(System.in); + int x = -1; + int sum = 0; + while (x != 0) { + x = in.nextInt(); + if (x <= 0) { + continue; + } + System.out.println("Adding " + x); + sum += x; + } + return sum; + } +} From ae811e02f8259d7d5cef3559914605fb3bdce8c0 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Fri, 29 Apr 2016 15:56:57 -0400 Subject: [PATCH 3/8] review ap01..ch07 --- ap01/Series.java | 25 +++++++++++++++++++++++++ {ch06 => ap01}/SeriesTest.java | 1 + ap02/Drawing.java | 5 +---- ap02/Mickey.java | 17 +++++++---------- ap02/Moire.java | 29 +++++++++-------------------- ch01/Goodbye.java | 1 + ch01/Hello.java | 1 + ch02/Variables.java | 2 +- ch03/Convert.java | 1 + ch03/Echo.java | 1 + ch03/GuessStarter.java | 1 + ch03/Input.java | 1 + ch03/ScannerBug.java | 1 + ch04/Methods.java | 1 + ch04/NewLine.java | 1 + ch04/PrintTime.java | 1 + ch04/PrintTwice.java | 1 + ch06/Series.java | 1 + ch07/Exercise.java | 19 +++++++++++++++++++ ch07/Loops.java | 1 + ch07/Tables.java | 1 + ch07/Validate.java | 1 + 22 files changed, 78 insertions(+), 35 deletions(-) create mode 100644 ap01/Series.java rename {ch06 => ap01}/SeriesTest.java (99%) create mode 100644 ch07/Exercise.java diff --git a/ap01/Series.java b/ap01/Series.java new file mode 100644 index 0000000..2d2d66c --- /dev/null +++ b/ap01/Series.java @@ -0,0 +1,25 @@ +/** + * Example method from Chapter 6. + */ +public class Series { + + public static int fibonacci(int n) { + if (n == 1 || n == 2) { + return 1; + } + return fibonacci(n - 1) + fibonacci(n - 2); + } + + public static void main(String[] args) { + if (fibonacci(1) != 1) { + System.err.println("fibonacci(1) is incorrect"); + } + if (fibonacci(2) != 1) { + System.err.println("fibonacci(2) is incorrect"); + } + if (fibonacci(3) != 2) { + System.err.println("fibonacci(3) is incorrect"); + } + } + +} diff --git a/ch06/SeriesTest.java b/ap01/SeriesTest.java similarity index 99% rename from ch06/SeriesTest.java rename to ap01/SeriesTest.java index 93b5420..6309616 100644 --- a/ch06/SeriesTest.java +++ b/ap01/SeriesTest.java @@ -10,4 +10,5 @@ public void testFibonacci() { assertEquals(1, Series.fibonacci(2)); assertEquals(2, Series.fibonacci(3)); } + } diff --git a/ap02/Drawing.java b/ap02/Drawing.java index 1453913..e82bfca 100644 --- a/ap02/Drawing.java +++ b/ap02/Drawing.java @@ -4,10 +4,6 @@ public class Drawing extends Canvas { - // this is here to suppress a warning; you can read about it at - // http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html - static final long serialVersionUID = 1; - public static void main(String[] args) { JFrame frame = new JFrame("My Drawing"); Canvas drawing = new Drawing(); @@ -20,4 +16,5 @@ public static void main(String[] args) { public void paint(Graphics g) { g.fillOval(100, 100, 200, 200); } + } diff --git a/ap02/Mickey.java b/ap02/Mickey.java index 14e93b9..6dfa3bc 100644 --- a/ap02/Mickey.java +++ b/ap02/Mickey.java @@ -1,20 +1,16 @@ import java.awt.Canvas; +import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; - import javax.swing.JFrame; - public class Mickey extends Canvas { - // this is here to suppress a warning; you can read about it at - // http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html - static final long serialVersionUID = 1; - public static void main(String[] args) { - JFrame frame = new JFrame("My Drawing"); + JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); + canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); @@ -25,6 +21,10 @@ public void paint(Graphics g) { mickey(g, bb); } + public void boxOval(Graphics g, Rectangle bb) { + g.fillOval(bb.x, bb.y, bb.width, bb.height); + } + public void mickey(Graphics g, Rectangle bb) { boxOval(g, bb); @@ -39,7 +39,4 @@ public void mickey(Graphics g, Rectangle bb) { boxOval(g, half); } - public void boxOval(Graphics g, Rectangle bb) { - g.fillOval(bb.x, bb.y, bb.width, bb.height); - } } diff --git a/ap02/Moire.java b/ap02/Moire.java index 78a1c22..750bad3 100644 --- a/ap02/Moire.java +++ b/ap02/Moire.java @@ -1,15 +1,19 @@ import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; - import javax.swing.JFrame; - public class Moire extends Canvas { - // this is here to suppress a warning; you can read about it at - // http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html - static final long serialVersionUID = 1; + public static void main(String[] args) { + JFrame frame = new JFrame("Moire Pattern"); + Canvas canvas = new Moire(); + canvas.setSize(400, 400); + canvas.setBackground(Color.white); + frame.add(canvas); + frame.pack(); + frame.setVisible(true); + } public void paint(Graphics g) { int i = 90; @@ -19,19 +23,4 @@ public void paint(Graphics g) { } } - public static void main(String[] args) { - // make the frame - JFrame frame = new JFrame(); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - - // add the canvas - Canvas canvas = new Moire(); - canvas.setSize(400, 400); - canvas.setBackground(Color.white); - frame.getContentPane().add(canvas); - - // show the frame - frame.pack(); - frame.setVisible(true); - } } diff --git a/ch01/Goodbye.java b/ch01/Goodbye.java index 0b165c3..2963e2e 100644 --- a/ch01/Goodbye.java +++ b/ch01/Goodbye.java @@ -10,4 +10,5 @@ public static void main(String[] args) { System.out.print("Goodbye, "); // note the space System.out.println("cruel world"); } + } diff --git a/ch01/Hello.java b/ch01/Hello.java index 13dacf5..593557b 100644 --- a/ch01/Hello.java +++ b/ch01/Hello.java @@ -4,4 +4,5 @@ public static void main(String[] args) { // generate some simple output System.out.println("Hello, World!"); } + } diff --git a/ch02/Variables.java b/ch02/Variables.java index 0312023..a295abf 100644 --- a/ch02/Variables.java +++ b/ch02/Variables.java @@ -6,7 +6,6 @@ public class Variables { public static void main(String[] args) { String message; - int x; String firstName; @@ -80,4 +79,5 @@ public static void main(String[] args) { hour = minute + 1; // correct // minute + 1 = hour; // compiler error } + } diff --git a/ch03/Convert.java b/ch03/Convert.java index 92fc06b..d31fe77 100644 --- a/ch03/Convert.java +++ b/ch03/Convert.java @@ -23,4 +23,5 @@ public static void main(String[] args) { System.out.printf("%.2f cm = %d ft, %d in\n", cm, feet, remainder); } + } diff --git a/ch03/Echo.java b/ch03/Echo.java index b82b2d4..500fd9f 100644 --- a/ch03/Echo.java +++ b/ch03/Echo.java @@ -14,4 +14,5 @@ public static void main(String[] args) { line = in.nextLine(); System.out.println("You also said: " + line); } + } diff --git a/ch03/GuessStarter.java b/ch03/GuessStarter.java index f3240c0..64984df 100644 --- a/ch03/GuessStarter.java +++ b/ch03/GuessStarter.java @@ -11,4 +11,5 @@ public static void main(String[] args) { int number = random.nextInt(100) + 1; System.out.println(number); } + } diff --git a/ch03/Input.java b/ch03/Input.java index 574290d..de97dc0 100644 --- a/ch03/Input.java +++ b/ch03/Input.java @@ -12,4 +12,5 @@ public static void main(String[] args) { double pi = 3.14159; double x = (int) pi * 20.0; } + } diff --git a/ch03/ScannerBug.java b/ch03/ScannerBug.java index 7a2d079..353affb 100644 --- a/ch03/ScannerBug.java +++ b/ch03/ScannerBug.java @@ -22,4 +22,5 @@ public static void main(String[] args) { name = in.nextLine(); System.out.printf("Hello %s, age %d\n", name, age); } + } diff --git a/ch04/Methods.java b/ch04/Methods.java index 035a392..90c1b7a 100644 --- a/ch04/Methods.java +++ b/ch04/Methods.java @@ -18,4 +18,5 @@ public static void main(String[] args) { double x3 = Math.exp(Math.log(10.0)); double x4 = Math.pow(2.0, 10.0); } + } diff --git a/ch04/NewLine.java b/ch04/NewLine.java index b13ddfc..a554e40 100644 --- a/ch04/NewLine.java +++ b/ch04/NewLine.java @@ -15,4 +15,5 @@ public static void main(String[] args) { threeLine(); System.out.println("Second line."); } + } diff --git a/ch04/PrintTime.java b/ch04/PrintTime.java index fe3549c..019d579 100644 --- a/ch04/PrintTime.java +++ b/ch04/PrintTime.java @@ -11,4 +11,5 @@ public static void main(String[] args) { int minute = 59; printTime(hour, minute); } + } diff --git a/ch04/PrintTwice.java b/ch04/PrintTwice.java index c34ed1c..7131085 100644 --- a/ch04/PrintTwice.java +++ b/ch04/PrintTwice.java @@ -8,4 +8,5 @@ public static void printTwice(String s) { public static void main(String[] args) { printTwice("Don't make me say this twice!"); } + } diff --git a/ch06/Series.java b/ch06/Series.java index 6db5643..10d2b41 100644 --- a/ch06/Series.java +++ b/ch06/Series.java @@ -146,4 +146,5 @@ public static void main(String[] args) { System.out.println("fibonacci"); System.out.println(fibonacci(3)); } + } diff --git a/ch07/Exercise.java b/ch07/Exercise.java new file mode 100644 index 0000000..6c7b3df --- /dev/null +++ b/ch07/Exercise.java @@ -0,0 +1,19 @@ +public class Exercise { + + public static void main(String[] args) { + loop(10); + } + + public static void loop(int n) { + int i = n; + while (i > 1) { + System.out.println(i); + if (i % 2 == 0) { + i = i / 2; + } else { + i = i + 1; + } + } + } + +} diff --git a/ch07/Loops.java b/ch07/Loops.java index d5c915f..dfc03ce 100644 --- a/ch07/Loops.java +++ b/ch07/Loops.java @@ -29,4 +29,5 @@ public static void main(String[] args) { System.out.println("sequence"); sequence(10); } + } diff --git a/ch07/Tables.java b/ch07/Tables.java index 0fbfbbe..44e9e62 100644 --- a/ch07/Tables.java +++ b/ch07/Tables.java @@ -153,4 +153,5 @@ public static void main(String[] args) { System.out.println("printTable4"); printTable4(6); } + } diff --git a/ch07/Validate.java b/ch07/Validate.java index eeb81da..e825705 100644 --- a/ch07/Validate.java +++ b/ch07/Validate.java @@ -50,4 +50,5 @@ public static double addNumbers() { } return sum; } + } From a60b1d128cd92c17881aa4fc5f60488e60dcc882 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Fri, 29 Apr 2016 16:03:49 -0400 Subject: [PATCH 4/8] review ch08..ch11 --- ch08/ArrayExamples.java | 56 --------------------------- ch08/Fruit.java | 35 +++++++++++++++++ ch08/Histogram.java | 59 ++++++++++++++++++++++++++++ ch08/MakeDubMus.java | 34 ++++++++++++++++ ch09/Exercise.java | 22 +++++++++++ ch09/Max.java | 5 +++ ch09/Recurse.java | 34 ++++++++++++++++ ch09/StringsThings.java | 86 +++++++++++++++++++++++++++++++---------- ch10/Max.java | 23 ----------- ch10/PointRect.java | 59 +++++++++++++++++++++------- ch10/Pow.java | 24 ++++++++++++ ch10/Riddle.java | 23 +++++++++++ ch11/Time.java | 16 -------- ch11/TimeClient.java | 29 +++++++++++--- 14 files changed, 370 insertions(+), 135 deletions(-) create mode 100644 ch08/Fruit.java create mode 100644 ch08/Histogram.java create mode 100644 ch08/MakeDubMus.java create mode 100644 ch09/Exercise.java create mode 100644 ch09/Recurse.java delete mode 100644 ch10/Max.java create mode 100644 ch10/Pow.java create mode 100644 ch10/Riddle.java diff --git a/ch08/ArrayExamples.java b/ch08/ArrayExamples.java index a9a4e9a..a4cb6c3 100644 --- a/ch08/ArrayExamples.java +++ b/ch08/ArrayExamples.java @@ -1,5 +1,4 @@ import java.util.Arrays; -import java.util.Random; /** * Demonstrates uses of arrays. @@ -62,8 +61,6 @@ public static void main(String[] args) { // reduce double total = sum(a); System.out.println("total = " + total); - - makeHistogram(); } /** @@ -100,57 +97,4 @@ public static double sum(double[] a) { return total; } - /** - * Returns an array of random integers. - */ - public static int[] randomArray(int size) { - Random random = new Random(); - int[] a = new int[size]; - for (int i = 0; i < a.length; i++) { - a[i] = random.nextInt(100); - } - return a; - } - - /** - * Computes the number of array elements in [low, high). - */ - public static int inRange(int[] a, int low, int high) { - int count = 0; - for (int i = 0; i < a.length; i++) { - if (a[i] >= low && a[i] < high) { - count++; - } - } - return count; - } - - /** - * Example code related to histograms. - */ - public static void makeHistogram() { - int numValues = 8; - int[] array = randomArray(numValues); - printArray(array); - - int[] scores = randomArray(30); - int a = inRange(scores, 90, 100); - int b = inRange(scores, 80, 90); - int c = inRange(scores, 70, 80); - int d = inRange(scores, 60, 70); - int f = inRange(scores, 0, 60); - - // making a histogram - int[] counts = new int[100]; - for (int i = 0; i < scores.length; i++) { - int index = scores[i]; - counts[index]++; - } - - // histogram with enhanced for loop - counts = new int[100]; - for (int score : scores) { - counts[score]++; - } - } } diff --git a/ch08/Fruit.java b/ch08/Fruit.java new file mode 100644 index 0000000..50428c5 --- /dev/null +++ b/ch08/Fruit.java @@ -0,0 +1,35 @@ +/** + * Fruit exercise. + */ +public class Fruit { + + public static int banana(int[] a) { + int kiwi = 1; + int i = 0; + while (i < a.length) { + kiwi = kiwi * a[i]; + i++; + } + return kiwi; + } + + public static int grapefruit(int[] a, int grape) { + for (int i = 0; i < a.length; i++) { + if (a[i] == grape) { + return i; + } + } + return -1; + } + + public static int pineapple(int[] a, int apple) { + int pear = 0; + for (int pine: a) { + if (pine == apple) { + pear++; + } + } + return pear; + } + +} diff --git a/ch08/Histogram.java b/ch08/Histogram.java new file mode 100644 index 0000000..32b029c --- /dev/null +++ b/ch08/Histogram.java @@ -0,0 +1,59 @@ +import java.util.Random; + +/** + * Example code related to histograms. + */ +public class Histogram { + + /** + * Returns an array of random integers. + */ + public static int[] randomArray(int size) { + Random random = new Random(); + int[] a = new int[size]; + for (int i = 0; i < a.length; i++) { + a[i] = random.nextInt(100); + } + return a; + } + + /** + * Computes the number of array elements in [low, high). + */ + public static int inRange(int[] a, int low, int high) { + int count = 0; + for (int i = 0; i < a.length; i++) { + if (a[i] >= low && a[i] < high) { + count++; + } + } + return count; + } + + public static void main(String[] args) { + int numValues = 8; + int[] array = randomArray(numValues); + ArrayExamples.printArray(array); + + int[] scores = randomArray(30); + int a = inRange(scores, 90, 100); + int b = inRange(scores, 80, 90); + int c = inRange(scores, 70, 80); + int d = inRange(scores, 60, 70); + int f = inRange(scores, 0, 60); + + // making a histogram + int[] counts = new int[100]; + for (int i = 0; i < scores.length; i++) { + int index = scores[i]; + counts[index]++; + } + + // histogram with enhanced for loop + counts = new int[100]; + for (int score : scores) { + counts[score]++; + } + } + +} diff --git a/ch08/MakeDubMus.java b/ch08/MakeDubMus.java new file mode 100644 index 0000000..9c7e193 --- /dev/null +++ b/ch08/MakeDubMus.java @@ -0,0 +1,34 @@ +/** + * Stack diagram exercise. + */ +public class MakeDubMus { + + public static int[] make(int n) { + int[] a = new int[n]; + for (int i = 0; i < n; i++) { + a[i] = i + 1; + } + return a; + } + + public static void dub(int[] jub) { + for (int i = 0; i < jub.length; i++) { + jub[i] *= 2; + } + } + + public static int mus(int[] zoo) { + int fus = 0; + for (int i = 0; i < zoo.length; i++) { + fus += zoo[i]; + } + return fus; + } + + public static void main(String[] args) { + int[] bob = make(5); + dub(bob); + System.out.println(mus(bob)); + } + +} diff --git a/ch09/Exercise.java b/ch09/Exercise.java new file mode 100644 index 0000000..d8a605d --- /dev/null +++ b/ch09/Exercise.java @@ -0,0 +1,22 @@ +/** + * Exercise on encapsulation and generalization. + */ +public class Exercise { + + public static void main(String[] args) { + String s = "((3 + 7) * 2)"; + int count = 0; + + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c == '(') { + count++; + } else if (c == ')') { + count--; + } + } + + System.out.println(count); + } + +} diff --git a/ch09/Max.java b/ch09/Max.java index 01417f0..2bf923e 100644 --- a/ch09/Max.java +++ b/ch09/Max.java @@ -5,6 +5,9 @@ */ public class Max { + /** + * Converts args to integers and prints the max. + */ public static void main(String[] args) { System.out.println(Arrays.toString(args)); @@ -15,5 +18,7 @@ public static void main(String[] args) { max = value; } } + System.out.println("The max is " + max); } + } diff --git a/ch09/Recurse.java b/ch09/Recurse.java new file mode 100644 index 0000000..c4ba2fb --- /dev/null +++ b/ch09/Recurse.java @@ -0,0 +1,34 @@ +/** + * Recursion exercise. + */ +public class Recurse { + + /** + * Returns the first character of the given String. + */ + public static char first(String s) { + return s.charAt(0); + } + + /** + * Returns all but the first letter of the given String. + */ + public static String rest(String s) { + return s.substring(1); + } + + /** + * Returns all but the first and last letter of the String. + */ + public static String middle(String s) { + return s.substring(1, s.length() - 1); + } + + /** + * Returns the length of the given String. + */ + public static int length(String s) { + return s.length(); + } + +} diff --git a/ch09/StringsThings.java b/ch09/StringsThings.java index 20357c1..8b95e7e 100644 --- a/ch09/StringsThings.java +++ b/ch09/StringsThings.java @@ -3,19 +3,17 @@ */ public class StringsThings { - /** - * Reverses a string, returns a new String. - */ - public static String reverse(String s) { - String r = ""; - for (int i = s.length() - 1; i >= 0; i--) { - r = r + s.charAt(i); - } - return r; - } - public static void main(String[] args) { + // Characters + + String fruit = "banana"; + char letter0 = fruit.charAt(0); + + if (letter0 == 'a') { + System.out.println('?'); + } + System.out.print("Roman alphabet: "); for (char c = 'A'; c <= 'Z'; c++) { System.out.print(c); @@ -28,6 +26,47 @@ public static void main(String[] args) { } System.out.println(); + // Strings are immutable + + String name = "Alan Turing"; + String upperName = name.toUpperCase(); + + String text = "Computer Science is fun!"; + text = text.replace("Computer Science", "CS"); + + // String traversal + + for (int i = 0; i < fruit.length(); i++) { + char letter = fruit.charAt(i); + System.out.println(letter); + } + + for (char letter : fruit.toCharArray()) { + System.out.println(letter); + } + + int length = fruit.length(); + char last = fruit.charAt(length - 1); // correct + + System.out.println(reverse(fruit)); + + // Substrings + + System.out.println(fruit.substring(0)); + System.out.println(fruit.substring(2)); + System.out.println(fruit.substring(6)); + + System.out.println(fruit.substring(0, 3)); + System.out.println(fruit.substring(2, 5)); + System.out.println(fruit.substring(6, 6)); + + // The indexOf method + + int index = fruit.indexOf('a'); + int index2 = fruit.indexOf('a', 2); + + // String comparison + String name1 = "Alan Turing"; String name2 = "Ada Lovelace"; if (name1.equals(name2)) { @@ -43,17 +82,24 @@ public static void main(String[] args) { System.out.println("name2 comes before name1."); } - String fruit = "banana"; + // Wrapper classes - for (int i = 0; i < fruit.length(); i++) { - char letter = fruit.charAt(i); - System.out.println(letter); - } + String str = "12345"; + int num = Integer.parseInt(str); - for (char letter : fruit.toCharArray()) { - System.out.println(letter); - } + num = 12345; + str = Integer.toString(num); + } - System.out.println(reverse(fruit)); + /** + * Reverses a string, returns a new String. + */ + public static String reverse(String s) { + String r = ""; + for (int i = s.length() - 1; i >= 0; i--) { + r = r + s.charAt(i); + } + return r; } + } diff --git a/ch10/Max.java b/ch10/Max.java deleted file mode 100644 index a7ecea5..0000000 --- a/ch10/Max.java +++ /dev/null @@ -1,23 +0,0 @@ -import java.util.Arrays; - -/** - * Demonstrates command-line arguments. - */ -public class Max { - - /** - * Converts the command line arguments to integers and prints the max. - */ - public static void main(String[] args) { - System.out.println(Arrays.toString(args)); - - int max = Integer.MIN_VALUE; - for (String arg : args) { - int value = Integer.parseInt(arg); - if (value > max) { - max = value; - } - } - System.out.println("The max is " + max); - } -} diff --git a/ch10/PointRect.java b/ch10/PointRect.java index d0da1df..36801a5 100644 --- a/ch10/PointRect.java +++ b/ch10/PointRect.java @@ -6,6 +6,28 @@ */ 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; + + Rectangle box = new Rectangle(0, 0, 100, 200); + moveRect(box, 50, 100); + System.out.println(box); + box.translate(50, 100); + + 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); + } + /** * Prints the attributes of a Point object. */ @@ -17,8 +39,8 @@ public static void printPoint(Point p) { * Computes the distance between two points. */ public static double distance(Point p1, Point p2) { - double dx = (double) (p2.x - p1.x); - double dy = (double) (p2.y - p1.y); + int dx = p2.x - p1.x; + int dy = p2.y - p1.y; return Math.sqrt(dx * dx + dy * dy); } @@ -40,22 +62,29 @@ public static void moveRect(Rectangle box, int dx, int dy) { } /** - * Tests the methods in this class. + * Exercise on returning objects. */ - public static void main(String[] args) { - Point blank; - blank = new Point(3, 4); - System.out.println(blank); + public static void exercise2() { + Point blank = new Point(5, 8); - Rectangle box = new Rectangle(0, 0, 100, 200); - moveRect(box, 50, 100); - System.out.println(box); + Rectangle rect = new Rectangle(0, 2, 4, 4); + Point center = findCenter(rect); - Rectangle box1 = new Rectangle(0, 0, 100, 200); - Rectangle box2 = box1; + double dist = distance(center, blank); + System.out.println(dist); + } - System.out.println(box2.width); - box1.grow(50, 50); - System.out.println(box2.width); + /** + * Exercise on aliasing. + */ + public static void exercise3() { + Rectangle box1 = new Rectangle(2, 4, 7, 9); + Point p1 = findCenter(box1); + printPoint(p1); + + box1.grow(1, 1); + Point p2 = findCenter(box1); + printPoint(p2); } + } diff --git a/ch10/Pow.java b/ch10/Pow.java new file mode 100644 index 0000000..9ba0952 --- /dev/null +++ b/ch10/Pow.java @@ -0,0 +1,24 @@ +/** + * BigInteger exercise. + */ +public class Pow { + + /** + * Integer exponentiation. + */ + public static int pow(int x, int n) { + if (n == 0) return 1; + + // find x to the n/2 recursively + int t = pow(x, n / 2); + + // if n is even, the result is t squared + // if n is odd, the result is t squared times x + if (n % 2 == 0) { + return t * t; + } else { + return t * t * x; + } + } + +} diff --git a/ch10/Riddle.java b/ch10/Riddle.java new file mode 100644 index 0000000..cd04a16 --- /dev/null +++ b/ch10/Riddle.java @@ -0,0 +1,23 @@ +import java.awt.Point; + +/** + * Exercise on passing objects as parameters. + */ +public class Riddle { + + public static int riddle(int x, Point p) { + x = x + 7; + return x + p.x + p.y; + } + + public static void main(String[] args) { + int x = 5; + Point blank = new Point(1, 2); + + System.out.println(riddle(x, blank)); + System.out.println(x); + System.out.println(blank.x); + System.out.println(blank.y); + } + +} diff --git a/ch11/Time.java b/ch11/Time.java index 99e75db..53afa0d 100644 --- a/ch11/Time.java +++ b/ch11/Time.java @@ -99,20 +99,4 @@ public void increment(double seconds) { } } - /** - * Test the methods in this class. - */ - public static void main(String[] args) { - Time time = new Time(11, 59, 59.9); - System.out.println(time); - - Time time1 = new Time(9, 30, 0.0); - Time time2 = time1; - Time time3 = new Time(9, 30, 0.0); - - System.out.println(time1 == time2); - System.out.println(time1 == time3); - System.out.println(time1.equals(time2)); - System.out.println(time1.equals(time3)); - } } diff --git a/ch11/TimeClient.java b/ch11/TimeClient.java index 59ef451..aa90401 100644 --- a/ch11/TimeClient.java +++ b/ch11/TimeClient.java @@ -3,12 +3,31 @@ */ public class TimeClient { - /** - * Demonstrates that we cannot access private variables from another - * class. - */ public static void main(String[] args) { Time time = new Time(11, 59, 59.9); - System.out.println(time.hour); // ERROR + System.out.println(time); + + // cannot access private variables from another class + // System.out.println(time.hour); + + String s = time.toString(); + System.out.println(s); + + Time time1 = new Time(9, 30, 0.0); + Time time2 = time1; + Time time3 = new Time(9, 30, 0.0); + + System.out.println(time1 == time2); + System.out.println(time1 == time3); + System.out.println(time1.equals(time2)); + System.out.println(time1.equals(time3)); + + Time startTime = new Time(18, 50, 0.0); + Time runningTime = new Time(2, 16, 0.0); + Time endTime = Time.add(startTime, runningTime); + + // using the instance method + endTime = startTime.add(runningTime); } + } From eaf96b152df96ba3af4eef74917d3fff37665eee Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Fri, 29 Apr 2016 16:07:13 -0400 Subject: [PATCH 5/8] review ch12..ch14 --- ch12/Card.java | 1 + ch12/CardTable.java | 11 +++--- ch12/Search.java | 66 ++++++++++++++++++++---------------- ch13/Deck.java | 36 ++++++++++++++++++++ ch13/Test.java | 1 + ch14/CardCollection.java | 72 ++++++++++++++++++---------------------- ch14/Deck.java | 1 + ch14/Eights.java | 62 +++++++++++++++++----------------- ch14/Hand.java | 1 + ch14/Player.java | 8 +---- ch14/Test.java | 8 ++--- 11 files changed, 151 insertions(+), 116 deletions(-) diff --git a/ch12/Card.java b/ch12/Card.java index 7bd1202..e7a0661 100644 --- a/ch12/Card.java +++ b/ch12/Card.java @@ -79,4 +79,5 @@ public int position() { public String toString() { return RANKS[this.rank] + " of " + SUITS[this.suit]; } + } diff --git a/ch12/CardTable.java b/ch12/CardTable.java index 5b2b8ca..727d307 100644 --- a/ch12/CardTable.java +++ b/ch12/CardTable.java @@ -7,13 +7,10 @@ import javax.swing.JFrame; public class CardTable extends Canvas { + private Image[][] images; private int cardWidth, cardHeight; - // this long is here to suppress a warning; you can read about it at - // http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html - static final long serialVersionUID = 1; - /** * Creates a CardTable. * cardset is the name of the folder that contains the card images. @@ -29,7 +26,8 @@ public CardTable(String cardset) { char c = suits.charAt(suit); for (int rank = 1; rank <= 13; rank++) { - String s = String.format("%s/%02d%c.gif", cardset, rank, c); + String s = String.format("%s/%02d%c.gif", + cardset, rank, c); images[rank][suit] = new ImageIcon(s).getImage(); } } @@ -79,7 +77,7 @@ public void paint(Graphics g) { public static void main(String[] args) { // make the frame - JFrame frame = new JFrame(); + JFrame frame = new JFrame("Card Table"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // add the CardTable @@ -91,4 +89,5 @@ public static void main(String[] args) { frame.pack(); frame.setVisible(true); } + } diff --git a/ch12/Search.java b/ch12/Search.java index 3cfe2d1..2f83023 100644 --- a/ch12/Search.java +++ b/ch12/Search.java @@ -3,6 +3,30 @@ */ public class Search { + /** + * Make an array of 52 cards. + */ + public static Card[] makeDeck() { + Card[] cards = new Card[52]; + int index = 0; + for (int suit = 0; suit <= 3; suit++) { + for (int rank = 1; rank <= 13; rank++) { + cards[index] = new Card(rank, suit); + index++; + } + } + return cards; + } + + /** + * Displays the given deck of cards. + */ + public static void printDeck(Card[] cards) { + for (int i = 0; i < cards.length; i++) { + System.out.println(cards[i]); + } + } + /** * Sequential search. */ @@ -24,14 +48,14 @@ public static int binarySearch(Card[] cards, Card target) { while (low <= high) { System.out.println(low + ", " + high); - int mid = (low + high) / 2; // step 1 + int mid = (low + high) / 2; // step 1 int comp = cards[mid].compareTo(target); - if (comp == 0) { // step 2 + if (comp == 0) { // step 2 return mid; - } else if (comp < 0) { // step 3 + } else if (comp < 0) { // step 3 low = mid + 1; - } else { // step 4 + } else { // step 4 high = mid - 1; } } @@ -41,40 +65,25 @@ public static int binarySearch(Card[] cards, Card target) { /** * Binary search (recursive version). */ - public static int binarySearchRec(Card[] cards, Card target, - int low, int high) { + public static int binarySearch(Card[] cards, Card target, + int low, int high) { System.out.println(low + ", " + high); if (high < low) { return -1; } - int mid = (low + high) / 2; // step 1 + int mid = (low + high) / 2; // step 1 int comp = cards[mid].compareTo(target); - if (comp == 0) { // step 2 + if (comp == 0) { // step 2 return mid; - } else if (comp < 0) { // step 3 - return binarySearchRec(cards, target, mid + 1, high); - } else { // step 4 - return binarySearchRec(cards, target, low, mid - 1); + } else if (comp < 0) { // step 3 + return binarySearch(cards, target, mid + 1, high); + } else { // step 4 + return binarySearch(cards, target, low, mid - 1); } } - /** - * Make an array of 52 cards. - */ - public static Card[] makeDeck() { - Card[] cards = new Card[52]; - int index = 0; - for (int suit = 0; suit <= 3; suit++) { - for (int rank = 1; rank <= 13; rank++) { - cards[index] = new Card(rank, suit); - index++; - } - } - return cards; - } - /** * Demonstrates how to call the search methods. */ @@ -96,7 +105,8 @@ public static void main(String[] args) { System.out.println(); System.out.println("Recursive binary search"); - System.out.println(binarySearch(cards, jack)); + System.out.println(binarySearch(cards, jack, 0, 51)); System.out.println(); } + } diff --git a/ch13/Deck.java b/ch13/Deck.java index 864d52e..742120e 100644 --- a/ch13/Deck.java +++ b/ch13/Deck.java @@ -45,12 +45,40 @@ public void print() { } } + /** + * Returns a string representation of the deck. + */ + public String toString() { + return Arrays.toString(this.cards); + } + + /** + * Chooses a random number between low and high, including both. + */ + public int randomInt(int low, int high) { + return 0; + } + + /** + * Swaps the cards at indexes i and j. + */ + public void swapCards(int i, int j) { + } + /** * Randomly permutes the array of cards. */ public void shuffle() { } + /** + * Finds the index of the lowest card + * between low and high inclusive. + */ + public int indexLowest(int low, int high) { + return 0; + } + /** * Sorts the cards (in place) using selection sort. */ @@ -68,6 +96,13 @@ public Deck subdeck(int low, int high) { return sub; } + /** + * Combines two previously sorted subdecks. + */ + public static Deck merge(Deck d1, Deck d2) { + return null; + } + /** * Returns a sorted copy of the deck using merge sort. */ @@ -80,4 +115,5 @@ public Deck mergeSort() { */ public void insertionSort() { } + } diff --git a/ch13/Test.java b/ch13/Test.java index c5431ee..5f49ef1 100644 --- a/ch13/Test.java +++ b/ch13/Test.java @@ -39,4 +39,5 @@ public static void main(String[] args) { deck.insertionSort(); checkSorted(deck); } + } diff --git a/ch14/CardCollection.java b/ch14/CardCollection.java index 5129496..f9c3be2 100644 --- a/ch14/CardCollection.java +++ b/ch14/CardCollection.java @@ -18,44 +18,46 @@ public CardCollection(String label) { } /** - * Returns the label. + * Returns the label of the card collection. */ public String getLabel() { return label; } /** - * Returns the number of cards. + * Adds the given card to the collection. */ - public int size() { - return cards.size(); + public void addCard(Card card) { + cards.add(card); } /** - * True if the collection is empty, false otherwise. + * Removes and returns the card with the given index. */ - public boolean empty() { - return cards.size() == 0; + public Card popCard(int i) { + return cards.remove(i); } /** - * Randomly permute the cards. + * Removes and returns the last card. */ - public void shuffle() { - Random random = new Random(); - for (int i = size() - 1; i > 0; i--) { - int j = random.nextInt(i); - swapCards(i, j); - } + public Card popCard() { + int i = size() - 1; + return popCard(i); } /** - * Swaps the cards at indexes i and j. + * Returns the number of cards. */ - public void swapCards(int i, int j) { - Card temp = cards.get(i); - cards.set(i, cards.get(j)); - cards.set(j, temp); + public int size() { + return cards.size(); + } + + /** + * True if the collection is empty, false otherwise. + */ + public boolean empty() { + return cards.size() == 0; } /** @@ -76,13 +78,6 @@ public void dealAll(CardCollection that) { deal(that, n); } - /** - * Adds the given card to the collection. - */ - public void addCard(Card card) { - cards.add(card); - } - /** * Returns the card with the given index. */ @@ -99,18 +94,23 @@ public Card last() { } /** - * Removes and returns the card with the given index. + * Swaps the cards at indexes i and j. */ - public Card popCard(int i) { - return cards.remove(i); + public void swapCards(int i, int j) { + Card temp = cards.get(i); + cards.set(i, cards.get(j)); + cards.set(j, temp); } /** - * Removes and returns the last card. + * Randomly permute the cards. */ - public Card popCard() { - int i = size() - 1; - return popCard(i); + public void shuffle() { + Random random = new Random(); + for (int i = size() - 1; i > 0; i--) { + int j = random.nextInt(i); + swapCards(i, j); + } } /** @@ -120,10 +120,4 @@ public String toString() { return label + ": " + cards.toString(); } - /** - * Gets the internal cards array (should only be used for testing). - */ - public Card[] getCards() { - return (Card[]) cards.toArray(); - } } diff --git a/ch14/Deck.java b/ch14/Deck.java index 10ba798..8db1814 100644 --- a/ch14/Deck.java +++ b/ch14/Deck.java @@ -15,4 +15,5 @@ public Deck(String label) { } } } + } diff --git a/ch14/Eights.java b/ch14/Eights.java index 78a767b..13b7961 100644 --- a/ch14/Eights.java +++ b/ch14/Eights.java @@ -40,17 +40,6 @@ public Eights() { in = new Scanner(System.in); } - /** - * Displays the state of the game. - */ - public void displayState() { - one.display(); - two.display(); - discardPile.display(); - System.out.print("Draw pile: "); - System.out.println(drawPile.size() + " cards"); - } - /** * Returns true if either hand is empty. */ @@ -75,19 +64,6 @@ public void reshuffle() { drawPile.shuffle(); } - /** - * One player takes a turn. - */ - public void takeTurn(Player player) { - Card prev = discardPile.last(); - Card next = player.play(this, prev); - - System.out.println(player.getName() + " plays " + next); - System.out.println(); - - discardPile.addCard(next); - } - /** * Returns a card from the draw pile. */ @@ -98,13 +74,6 @@ public Card draw() { return drawPile.popCard(); } - /** - * Waits for the user to press enter. - */ - public void waitForUser() { - in.nextLine(); - } - /** * Switches players. */ @@ -116,6 +85,36 @@ public Player nextPlayer(Player current) { } } + /** + * Displays the state of the game. + */ + public void displayState() { + one.display(); + two.display(); + discardPile.display(); + System.out.print("Draw pile: "); + System.out.println(drawPile.size() + " cards"); + } + + /** + * Waits for the user to press enter. + */ + public void waitForUser() { + in.nextLine(); + } + + /** + * One player takes a turn. + */ + public void takeTurn(Player player) { + Card prev = discardPile.last(); + Card next = player.play(this, prev); + discardPile.addCard(next); + + System.out.println(player.getName() + " plays " + next); + System.out.println(); + } + /** * Plays the game. */ @@ -142,4 +141,5 @@ public static void main(String[] args) { Eights game = new Eights(); game.playGame(); } + } diff --git a/ch14/Hand.java b/ch14/Hand.java index a65b7f5..8089a49 100644 --- a/ch14/Hand.java +++ b/ch14/Hand.java @@ -20,4 +20,5 @@ public void display() { } System.out.println(); } + } diff --git a/ch14/Player.java b/ch14/Player.java index c9c9695..433ff28 100644 --- a/ch14/Player.java +++ b/ch14/Player.java @@ -101,13 +101,6 @@ public int score() { return sum; } - /** - * Returns a string representation of the player. - */ - public String toString() { - return name + ": " + hand; - } - /** * Displays the player's hand. */ @@ -121,4 +114,5 @@ public void display() { public void displayScore() { System.out.println(name + " has " + score() + " points"); } + } diff --git a/ch14/Test.java b/ch14/Test.java index 0e5c243..274710c 100644 --- a/ch14/Test.java +++ b/ch14/Test.java @@ -3,13 +3,9 @@ */ public class Test { - /** - * Test code. - */ public static void main(String[] args) { Deck deck = new Deck("Deck"); deck.shuffle(); - System.out.println(deck); Hand hand = new Hand("Hand"); deck.deal(hand, 5); @@ -17,6 +13,8 @@ public static void main(String[] args) { Hand drawPile = new Hand("Draw Pile"); deck.dealAll(drawPile); - System.out.println(drawPile.size()); + System.out.printf("Draw Pile has %d cards.\n", + drawPile.size()); } + } From 390cbbe83b803fe6a01be0baf659804755082de1 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Fri, 6 May 2016 09:33:10 -0400 Subject: [PATCH 6/8] sync with 6.1.0 --- LICENSE | 2 +- README.md | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/LICENSE b/LICENSE index 749dcd6..89ed830 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016 Allen Downey +Copyright (c) 2016 Allen Downey and Chris Mayfield Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index fe3da55..10592d0 100644 --- a/README.md +++ b/README.md @@ -13,17 +13,19 @@ If you don't already have a GitHub account, you'll need to create one. After forking, you'll have your own repository on GitHub that you can use to keep track of code you write. Then you can ``clone'' the repository, which downloads a copy of the files to your computer. -* Or you could clone the repository without forking. If you choose this option, you don't need a GitHub account, but you won't be able to save your changes back in GitHub. +* Alternatively, you could clone the repository without forking. +If you choose this option, you don't need a GitHub account, but you won't be able to save your changes back in GitHub. * If you don't want to use Git at all, you can download the code in a zip archive using the "Download ZIP" button on this page, or [this link](http://tinyurl.com/ThinkJavaCodeZip). -To clone a repository, you need a Git client installed on your computer. The URL of this repository is `https://github.com/AllenDowney/ThinkJavaCode.git`. If you use Git from the command line, you can clone it like this: +To clone a repository, you need a Git client installed on your computer. +The URL of this repository is `https://github.com/AllenDowney/ThinkJavaCode.git`. +If you use Git from the command line, you can clone it like this: git clone https://github.com/AllenDowney/ThinkJavaCode.git After you clone the repository or unzip the zip file, you should have a directory called `ThinkJavaCode` with a subdirectory for each chapter in the book. -All the examples in this book were developed and tested using Java SE Development Kit 7. +All examples in this book were developed and tested using Java SE Development Kit 8. If you are using a more recent version, the examples in this book should still work. If you are using an older version, some of them may not. - From bc927971a210887c511c9ccd942c6728a79f806c Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Tue, 6 Sep 2016 21:27:43 -0400 Subject: [PATCH 7/8] checkstyle 7.1.1 --- ap02/Moire.java | 2 +- ch05/Conditional.java | 2 -- ch05/Exercise.java | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/ap02/Moire.java b/ap02/Moire.java index 750bad3..8e7247f 100644 --- a/ap02/Moire.java +++ b/ap02/Moire.java @@ -18,7 +18,7 @@ public static void main(String[] args) { public void paint(Graphics g) { int i = 90; while (i < getWidth()) { - g.drawOval (0, 0, i, i); + g.drawOval(0, 0, i, i); i = i + 3; } } diff --git a/ch05/Conditional.java b/ch05/Conditional.java index 1fd36ce..f2d6d19 100644 --- a/ch05/Conditional.java +++ b/ch05/Conditional.java @@ -1,5 +1,3 @@ -import java.util.Scanner; - /** * Examples from Chapter 5. */ diff --git a/ch05/Exercise.java b/ch05/Exercise.java index 46774e0..dbbb6e9 100644 --- a/ch05/Exercise.java +++ b/ch05/Exercise.java @@ -18,7 +18,7 @@ public static void main(String[] args) { public static void clink(int fork) { System.out.print("It's "); - zoop("breakfast ", fork) ; + zoop("breakfast ", fork); } public static void ping(String strangStrung) { From 9fb1ef4d514055e2e31c56b786ccbd6c384e41a5 Mon Sep 17 00:00:00 2001 From: Chris Mayfield Date: Tue, 27 Dec 2016 12:27:10 -0500 Subject: [PATCH 8/8] sync with 6.1.2 --- ch05/Logarithm.java | 12 ++++++------ ch07/Tables.java | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ch05/Logarithm.java b/ch05/Logarithm.java index bc21157..5c5986a 100644 --- a/ch05/Logarithm.java +++ b/ch05/Logarithm.java @@ -6,11 +6,13 @@ public static void main(String[] args) { System.out.println("printLogarithm"); printLogarithm(3.0); + Scanner in = new Scanner(System.in); + System.out.println("scandouble"); - scanDouble(); + scanDouble(in); System.out.println("scandouble2"); - scanDouble2(); + scanDouble2(in); } public static void printLogarithm(double x) { @@ -22,15 +24,13 @@ public static void printLogarithm(double x) { System.out.println("The log of x is " + result); } - public static void scanDouble() { - Scanner in = new Scanner(System.in); + public static void scanDouble(Scanner in) { System.out.print("Enter a number: "); double x = in.nextDouble(); printLogarithm(x); } - public static void scanDouble2() { - Scanner in = new Scanner(System.in); + public static void scanDouble2(Scanner in) { System.out.print("Enter a number: "); if (!in.hasNextDouble()) { String word = in.next(); diff --git a/ch07/Tables.java b/ch07/Tables.java index 44e9e62..3e33905 100644 --- a/ch07/Tables.java +++ b/ch07/Tables.java @@ -6,7 +6,7 @@ public class Tables { public static void example() { int i = 1; while (i < 10) { - double x = (double) i; + double x = i; System.out.println(x + " " + Math.log(x)); i = i + 1; } @@ -15,7 +15,7 @@ public static void example() { public static void example2() { int i = 1; while (i < 10) { - double x = (double) i; + double x = i; System.out.println(x + " " + Math.log(x) / Math.log(2)); i = i + 1; } @@ -25,7 +25,7 @@ public static void example3() { final double LOG2 = Math.log(2); int i = 1; while (i < 100) { - double x = (double) i; + double x = i; System.out.println(x + " " + Math.log(x) / LOG2); i = i * 2; }