├── .gitignore ├── LICENSE ├── README.md ├── ap01 ├── Series.java └── SeriesTest.java ├── ap02 ├── Drawing.java ├── Mickey.java └── Moire.java ├── ch01 ├── Goodbye.java └── Hello.java ├── ch02 └── Variables.java ├── ch03 ├── Convert.java ├── Echo.java ├── GuessStarter.java ├── Input.java └── ScannerBug.java ├── ch04 ├── Exercise.java ├── Methods.java ├── NewLine.java ├── PrintTime.java └── PrintTwice.java ├── ch05 ├── Buzz.java ├── Conditional.java ├── Exercise.java ├── Logarithm.java └── Recursion.java ├── ch06 ├── Exercise.java ├── Recursive.java └── Series.java ├── ch07 ├── Exercise.java ├── Loops.java ├── Tables.java └── Validate.java ├── ch08 ├── ArrayExamples.java ├── Fruit.java ├── Histogram.java └── MakeDubMus.java ├── ch09 ├── Exercise.java ├── Format.java ├── Max.java ├── Recurse.java └── StringsThings.java ├── ch10 ├── PointRect.java ├── Pow.java └── Riddle.java ├── ch11 ├── Time.java └── TimeClient.java ├── ch12 ├── Card.java ├── CardTable.java ├── Search.java └── cardset-oxymoron │ ├── 01c.gif │ ├── 01d.gif │ ├── 01h.gif │ ├── 01s.gif │ ├── 02c.gif │ ├── 02d.gif │ ├── 02h.gif │ ├── 02s.gif │ ├── 03c.gif │ ├── 03d.gif │ ├── 03h.gif │ ├── 03s.gif │ ├── 04c.gif │ ├── 04d.gif │ ├── 04h.gif │ ├── 04s.gif │ ├── 05c.gif │ ├── 05d.gif │ ├── 05h.gif │ ├── 05s.gif │ ├── 06c.gif │ ├── 06d.gif │ ├── 06h.gif │ ├── 06s.gif │ ├── 07c.gif │ ├── 07d.gif │ ├── 07h.gif │ ├── 07s.gif │ ├── 08c.gif │ ├── 08d.gif │ ├── 08h.gif │ ├── 08s.gif │ ├── 09c.gif │ ├── 09d.gif │ ├── 09h.gif │ ├── 09s.gif │ ├── 10c.gif │ ├── 10d.gif │ ├── 10h.gif │ ├── 10s.gif │ ├── 11c.gif │ ├── 11d.gif │ ├── 11h.gif │ ├── 11s.gif │ ├── 12c.gif │ ├── 12d.gif │ ├── 12h.gif │ ├── 12s.gif │ ├── 13c.gif │ ├── 13d.gif │ ├── 13h.gif │ ├── 13s.gif │ ├── COPYRIGHT │ ├── back001.gif │ ├── back101.gif │ ├── back102.gif │ ├── back111.gif │ ├── back191.gif │ ├── back192.gif │ ├── bottom01.gif │ ├── bottom02.gif │ ├── bottom03.gif │ ├── bottom04.gif │ ├── bottom05.gif │ ├── bottom06.gif │ ├── bottom07.gif │ ├── config.txt │ ├── l01.gif │ ├── l02.gif │ ├── l03.gif │ ├── l04.gif │ ├── shade.gif │ ├── shadow00.gif │ ├── shadow01.gif │ ├── shadow02.gif │ ├── shadow03.gif │ ├── shadow04.gif │ ├── shadow05.gif │ ├── shadow06.gif │ ├── shadow07.gif │ ├── shadow08.gif │ ├── shadow09.gif │ ├── shadow10.gif │ ├── shadow11.gif │ ├── shadow12.gif │ └── shadow13.gif ├── ch13 ├── Card.java ├── Deck.java └── Test.java └── ch14 ├── Card.java ├── CardCollection.java ├── Deck.java ├── Eights.java ├── Hand.java ├── Player.java └── Test.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Allen Downey and Chris Mayfield 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ThinkJavaCode 2 | Supporting code for *Think Java* by Allen Downey and Chris Mayfield. 3 | 4 | This is a Git repository that contains the code examples from the book and starter code for some exercises. 5 | 6 | Git is a version control system that allows you to keep track of the files that make up a project. 7 | A collection of files under Git's control is called a repository. 8 | 9 | There are several ways you can work with the code: 10 | * You can edit and run the code on [Codiva online java IDE](https://www.codiva.io/tutorials/thinkjavacode). 11 | 12 | * You can create a copy of this repository on GitHub by pressing the "Fork" button in the upper right. 13 | If you don't already have a GitHub account, you'll need to create one. 14 | After forking, you'll have your own repository on GitHub that you can use to keep track of code you write. 15 | Then you can ``clone'' the repository, which downloads a copy of the files to your computer. 16 | 17 | * Alternatively, you could clone the repository without forking. 18 | 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. 19 | 20 | * 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). 21 | 22 | To clone a repository, you need a Git client installed on your computer. 23 | The URL of this repository is `https://github.com/AllenDowney/ThinkJavaCode.git`. 24 | If you use Git from the command line, you can clone it like this: 25 | 26 | git clone https://github.com/AllenDowney/ThinkJavaCode.git 27 | 28 | 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. 29 | 30 | All examples in this book were developed and tested using Java SE Development Kit 8. 31 | If you are using a more recent version, the examples in this book should still work. 32 | If you are using an older version, some of them may not. 33 | -------------------------------------------------------------------------------- /ap01/Series.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Example method from Chapter 6. 3 | */ 4 | public class Series { 5 | 6 | public static int fibonacci(int n) { 7 | if (n == 1 || n == 2) { 8 | return 1; 9 | } 10 | return fibonacci(n - 1) + fibonacci(n - 2); 11 | } 12 | 13 | public static void main(String[] args) { 14 | if (fibonacci(1) != 1) { 15 | System.err.println("fibonacci(1) is incorrect"); 16 | } 17 | if (fibonacci(2) != 1) { 18 | System.err.println("fibonacci(2) is incorrect"); 19 | } 20 | if (fibonacci(3) != 2) { 21 | System.err.println("fibonacci(3) is incorrect"); 22 | } 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /ap01/SeriesTest.java: -------------------------------------------------------------------------------- 1 | import junit.framework.TestCase; 2 | 3 | /** 4 | * Example JUnit test from Appendix A. 5 | */ 6 | public class SeriesTest extends TestCase { 7 | 8 | public void testFibonacci() { 9 | assertEquals(1, Series.fibonacci(1)); 10 | assertEquals(1, Series.fibonacci(2)); 11 | assertEquals(2, Series.fibonacci(3)); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /ap02/Drawing.java: -------------------------------------------------------------------------------- 1 | import java.awt.Canvas; 2 | import java.awt.Graphics; 3 | import javax.swing.JFrame; 4 | 5 | public class Drawing extends Canvas { 6 | 7 | public static void main(String[] args) { 8 | JFrame frame = new JFrame("My Drawing"); 9 | Canvas drawing = new Drawing(); 10 | drawing.setSize(400, 400); 11 | frame.add(drawing); 12 | frame.pack(); 13 | frame.setVisible(true); 14 | } 15 | 16 | public void paint(Graphics g) { 17 | g.fillOval(100, 100, 200, 200); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /ap02/Mickey.java: -------------------------------------------------------------------------------- 1 | import java.awt.Canvas; 2 | import java.awt.Color; 3 | import java.awt.Graphics; 4 | import java.awt.Rectangle; 5 | import javax.swing.JFrame; 6 | 7 | public class Mickey extends Canvas { 8 | 9 | public static void main(String[] args) { 10 | JFrame frame = new JFrame("Mickey Mouse"); 11 | Canvas canvas = new Mickey(); 12 | canvas.setSize(400, 400); 13 | canvas.setBackground(Color.white); 14 | frame.add(canvas); 15 | frame.pack(); 16 | frame.setVisible(true); 17 | } 18 | 19 | public void paint(Graphics g) { 20 | Rectangle bb = new Rectangle(100, 100, 200, 200); 21 | mickey(g, bb); 22 | } 23 | 24 | public void boxOval(Graphics g, Rectangle bb) { 25 | g.fillOval(bb.x, bb.y, bb.width, bb.height); 26 | } 27 | 28 | public void mickey(Graphics g, Rectangle bb) { 29 | boxOval(g, bb); 30 | 31 | int dx = bb.width / 2; 32 | int dy = bb.height / 2; 33 | Rectangle half = new Rectangle(bb.x, bb.y, dx, dy); 34 | 35 | half.translate(-dx / 2, -dy / 2); 36 | boxOval(g, half); 37 | 38 | half.translate(dx * 2, 0); 39 | boxOval(g, half); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /ap02/Moire.java: -------------------------------------------------------------------------------- 1 | import java.awt.Canvas; 2 | import java.awt.Color; 3 | import java.awt.Graphics; 4 | import javax.swing.JFrame; 5 | 6 | public class Moire extends Canvas { 7 | 8 | public static void main(String[] args) { 9 | JFrame frame = new JFrame("Moire Pattern"); 10 | Canvas canvas = new Moire(); 11 | canvas.setSize(400, 400); 12 | canvas.setBackground(Color.white); 13 | frame.add(canvas); 14 | frame.pack(); 15 | frame.setVisible(true); 16 | } 17 | 18 | public void paint(Graphics g) { 19 | int i = 90; 20 | while (i < getWidth()) { 21 | g.drawOval(0, 0, i, i); 22 | i = i + 3; 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /ch01/Goodbye.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Example program that demonstrates print vs println. 3 | */ 4 | public class Goodbye { 5 | 6 | /** 7 | * Prints a greeting. 8 | */ 9 | public static void main(String[] args) { 10 | System.out.print("Goodbye, "); // note the space 11 | System.out.println("cruel world"); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /ch01/Hello.java: -------------------------------------------------------------------------------- 1 | public class Hello { 2 | 3 | public static void main(String[] args) { 4 | // generate some simple output 5 | System.out.println("Hello, World!"); 6 | } 7 | 8 | } 9 | -------------------------------------------------------------------------------- /ch02/Variables.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Examples from Chapter 2. 3 | */ 4 | public class Variables { 5 | 6 | public static void main(String[] args) { 7 | 8 | String message; 9 | int x; 10 | 11 | String firstName; 12 | String lastName; 13 | int hour, minute; 14 | 15 | message = "Hello!"; // give message the value "Hello!" 16 | hour = 11; // assign the value 11 to hour 17 | minute = 59; // set minute to 59 18 | 19 | message = "123"; // legal 20 | // message = 123; // not legal 21 | 22 | String message2 = "Hello!"; 23 | int hour2 = 11; 24 | int minute2 = 59; 25 | 26 | int a = 5; 27 | int b = a; // a and b are now equal 28 | a = 3; // a and b are no longer equal 29 | 30 | String firstLine = "Hello, again!"; 31 | System.out.println(firstLine); 32 | 33 | System.out.print("The value of firstLine is "); 34 | System.out.println(firstLine); 35 | 36 | System.out.print("The current time is "); 37 | System.out.print(hour); 38 | System.out.print(":"); 39 | System.out.print(minute); 40 | System.out.println("."); 41 | 42 | System.out.print("Number of minutes since midnight: "); 43 | System.out.println(hour * 60 + minute); 44 | 45 | System.out.print("Fraction of the hour that has passed: "); 46 | System.out.println(minute / 60); 47 | 48 | System.out.print("Percent of the hour that has passed: "); 49 | System.out.println(minute * 100 / 60); 50 | 51 | double pi; 52 | pi = 3.14159; 53 | 54 | double minute3 = 59.0; 55 | System.out.print("Fraction of the hour that has passed: "); 56 | System.out.println(minute3 / 60.0); 57 | 58 | double y = 1.0 / 3.0; // correct 59 | 60 | System.out.println(0.1 * 10); 61 | System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1 62 | + 0.1 + 0.1 + 0.1 + 0.1 + 0.1); 63 | 64 | double balance = 123.45; // potential rounding error 65 | int balance2 = 12345; // total number of cents 66 | 67 | System.out.println(1 + 2 + "Hello"); 68 | // the output is 3Hello 69 | 70 | System.out.println("Hello" + 1 + 2); 71 | // the output is Hello12 72 | 73 | System.out.println(17 * 3); 74 | System.out.println(hour * 60 + minute); 75 | 76 | int percentage; 77 | percentage = (minute * 100) / 60; 78 | 79 | hour = minute + 1; // correct 80 | // minute + 1 = hour; // compiler error 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /ch03/Convert.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /** 4 | * Converts centimeters to feet and inches. 5 | */ 6 | public class Convert { 7 | 8 | public static void main(String[] args) { 9 | double cm; 10 | int feet, inches, remainder; 11 | final double CM_PER_INCH = 2.54; 12 | final int IN_PER_FOOT = 12; 13 | Scanner in = new Scanner(System.in); 14 | 15 | // prompt the user and get the value 16 | System.out.print("Exactly how many cm? "); 17 | cm = in.nextDouble(); 18 | 19 | // convert and output the result 20 | inches = (int) (cm / CM_PER_INCH); 21 | feet = inches / IN_PER_FOOT; 22 | remainder = inches % IN_PER_FOOT; 23 | System.out.printf("%.2f cm = %d ft, %d in\n", 24 | cm, feet, remainder); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /ch03/Echo.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Echo { 4 | 5 | public static void main(String[] args) { 6 | String line; 7 | Scanner in = new Scanner(System.in); 8 | 9 | System.out.print("Type something: "); 10 | line = in.nextLine(); 11 | System.out.println("You said: " + line); 12 | 13 | System.out.print("Type something else: "); 14 | line = in.nextLine(); 15 | System.out.println("You also said: " + line); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /ch03/GuessStarter.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | /** 4 | * Starter code for the "Guess My Number" exercise. 5 | */ 6 | public class GuessStarter { 7 | 8 | public static void main(String[] args) { 9 | // pick a random number 10 | Random random = new Random(); 11 | int number = random.nextInt(100) + 1; 12 | System.out.println(number); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /ch03/Input.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Examples from Chapter 3. 3 | */ 4 | public class Input { 5 | 6 | public static void main(String[] args) { 7 | System.out.println(System.out); 8 | 9 | System.out.print(4.0 / 3.0); 10 | System.out.printf("Four thirds = %.3f", 4.0 / 3.0); 11 | 12 | double pi = 3.14159; 13 | double x = (int) pi * 20.0; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /ch03/ScannerBug.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /** 4 | * Demonstrates a common problem using Scanner. 5 | */ 6 | public class ScannerBug { 7 | 8 | public static void main(String[] args) { 9 | String name; 10 | int age; 11 | Scanner in = new Scanner(System.in); 12 | 13 | System.out.print("What is your name? "); 14 | name = in.nextLine(); 15 | System.out.print("What is your age? "); 16 | age = in.nextInt(); 17 | System.out.printf("Hello %s, age %d\n", name, age); 18 | 19 | System.out.print("What is your age? "); 20 | age = in.nextInt(); 21 | System.out.print("What is your name? "); 22 | name = in.nextLine(); 23 | System.out.printf("Hello %s, age %d\n", name, age); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /ch04/Exercise.java: -------------------------------------------------------------------------------- 1 | public class Exercise { 2 | 3 | public static void zoop() { 4 | baffle(); 5 | System.out.print("You wugga "); 6 | baffle(); 7 | } 8 | 9 | public static void main(String[] args) { 10 | System.out.print("No, I "); 11 | zoop(); 12 | System.out.print("I "); 13 | baffle(); 14 | } 15 | 16 | public static void baffle() { 17 | System.out.print("wug"); 18 | ping(); 19 | } 20 | 21 | public static void ping() { 22 | System.out.println("."); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /ch04/Methods.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Examples from Chapter 4. 3 | */ 4 | public class Methods { 5 | 6 | public static void main(String[] args) { 7 | double root = Math.sqrt(17.0); 8 | double angle = 1.5; 9 | double height = Math.sin(angle); 10 | 11 | double degrees = 90; 12 | double angle2 = degrees / 180.0 * Math.PI; 13 | double radians = Math.toRadians(180.0); 14 | double degrees2 = Math.toDegrees(Math.PI); 15 | long x = Math.round(Math.PI * 20.0); 16 | 17 | double x2 = Math.cos(angle + Math.PI / 2.0); 18 | double x3 = Math.exp(Math.log(10.0)); 19 | double x4 = Math.pow(2.0, 10.0); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /ch04/NewLine.java: -------------------------------------------------------------------------------- 1 | public class NewLine { 2 | 3 | public static void newLine() { 4 | System.out.println(); 5 | } 6 | 7 | public static void threeLine() { 8 | newLine(); 9 | newLine(); 10 | newLine(); 11 | } 12 | 13 | public static void main(String[] args) { 14 | System.out.println("First line."); 15 | threeLine(); 16 | System.out.println("Second line."); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /ch04/PrintTime.java: -------------------------------------------------------------------------------- 1 | public class PrintTime { 2 | 3 | public static void printTime(int hour, int minute) { 4 | System.out.print(hour); 5 | System.out.print(":"); 6 | System.out.println(minute); 7 | } 8 | 9 | public static void main(String[] args) { 10 | int hour = 11; 11 | int minute = 59; 12 | printTime(hour, minute); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /ch04/PrintTwice.java: -------------------------------------------------------------------------------- 1 | public class PrintTwice { 2 | 3 | public static void printTwice(String s) { 4 | System.out.println(s); 5 | System.out.println(s); 6 | } 7 | 8 | public static void main(String[] args) { 9 | printTwice("Don't make me say this twice!"); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /ch05/Buzz.java: -------------------------------------------------------------------------------- 1 | public class Buzz { 2 | 3 | public static void baffle(String blimp) { 4 | System.out.println(blimp); 5 | zippo("ping", -5); 6 | } 7 | 8 | public static void zippo(String quince, int flag) { 9 | if (flag < 0) { 10 | System.out.println(quince + " zoop"); 11 | } else { 12 | System.out.println("ik"); 13 | baffle(quince); 14 | System.out.println("boo-wa-ha-ha"); 15 | } 16 | } 17 | 18 | public static void main(String[] args) { 19 | zippo("rattle", 13); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /ch05/Conditional.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Examples from Chapter 5. 3 | */ 4 | public class Conditional { 5 | 6 | public static void main(String[] args) { 7 | String fruit1 = "Apple"; 8 | String fruit2 = "Orange"; 9 | System.out.println(fruit1.equals(fruit2)); 10 | 11 | int x = 17; 12 | int n = 18; 13 | 14 | if (x > 0) { 15 | System.out.println("x is positive"); 16 | } 17 | 18 | if (x % 2 == 0) { 19 | System.out.println("x is even"); 20 | } else { 21 | System.out.println("x is odd"); 22 | } 23 | 24 | if (x > 0) { 25 | System.out.println("x is positive"); 26 | } else if (x < 0) { 27 | System.out.println("x is negative"); 28 | } else { 29 | System.out.println("x is zero"); 30 | } 31 | 32 | if (x == 0) { 33 | System.out.println("x is zero"); 34 | } else { 35 | if (x > 0) { 36 | System.out.println("x is positive"); 37 | } else { 38 | System.out.println("x is negative"); 39 | } 40 | } 41 | 42 | boolean evenFlag = (n % 2 == 0); // true if n is even 43 | boolean positiveFlag = (x > 0); // true if x is positive 44 | 45 | if (evenFlag) { 46 | System.out.println("n was even when I checked it"); 47 | } 48 | 49 | if (!evenFlag) { 50 | System.out.println("n was odd when I checked it"); 51 | } 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /ch05/Exercise.java: -------------------------------------------------------------------------------- 1 | public class Exercise { 2 | 3 | public static void zoop(String fred, int bob) { 4 | System.out.println(fred); 5 | if (bob == 5) { 6 | ping("not "); 7 | } else { 8 | System.out.println("!"); 9 | } 10 | } 11 | 12 | public static void main(String[] args) { 13 | int bizz = 5; 14 | int buzz = 2; 15 | zoop("just for", bizz); 16 | clink(2 * buzz); 17 | } 18 | 19 | public static void clink(int fork) { 20 | System.out.print("It's "); 21 | zoop("breakfast ", fork); 22 | } 23 | 24 | public static void ping(String strangStrung) { 25 | System.out.println("any " + strangStrung + "more "); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /ch05/Logarithm.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Logarithm { 4 | 5 | public static void main(String[] args) { 6 | System.out.println("printLogarithm"); 7 | printLogarithm(3.0); 8 | 9 | Scanner in = new Scanner(System.in); 10 | 11 | System.out.println("scandouble"); 12 | scanDouble(in); 13 | 14 | System.out.println("scandouble2"); 15 | scanDouble2(in); 16 | } 17 | 18 | public static void printLogarithm(double x) { 19 | if (x <= 0.0) { 20 | System.err.println("Error: x must be positive."); 21 | return; 22 | } 23 | double result = Math.log(x); 24 | System.out.println("The log of x is " + result); 25 | } 26 | 27 | public static void scanDouble(Scanner in) { 28 | System.out.print("Enter a number: "); 29 | double x = in.nextDouble(); 30 | printLogarithm(x); 31 | } 32 | 33 | public static void scanDouble2(Scanner in) { 34 | System.out.print("Enter a number: "); 35 | if (!in.hasNextDouble()) { 36 | String word = in.next(); 37 | System.err.println(word + " is not a number"); 38 | return; 39 | } 40 | double x = in.nextDouble(); 41 | printLogarithm(x); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /ch05/Recursion.java: -------------------------------------------------------------------------------- 1 | public class Recursion { 2 | 3 | public static void main(String[] args) { 4 | System.out.println("countdown"); 5 | countdown(3); 6 | 7 | System.out.println("countup"); 8 | countup(3); 9 | 10 | System.out.println("newLine"); 11 | newLine(); 12 | 13 | System.out.println("nLines"); 14 | nLines(3); 15 | 16 | System.out.println("threeLine"); 17 | threeLine(); 18 | 19 | System.out.println("displayBinary"); 20 | displayBinary(23); 21 | System.out.println(); 22 | } 23 | 24 | public static void countdown(int n) { 25 | if (n == 0) { 26 | System.out.println("Blastoff!"); 27 | } else { 28 | System.out.println(n); 29 | countdown(n - 1); 30 | } 31 | } 32 | 33 | public static void newLine() { 34 | System.out.println(); 35 | } 36 | 37 | public static void threeLine() { 38 | newLine(); 39 | newLine(); 40 | newLine(); 41 | } 42 | 43 | public static void nLines(int n) { 44 | if (n > 0) { 45 | System.out.println(); 46 | nLines(n - 1); 47 | } 48 | } 49 | 50 | public static void forever(String s) { 51 | System.out.println(s); 52 | forever(s); 53 | } 54 | 55 | public static void countup(int n) { 56 | if (n == 0) { 57 | System.out.println("Blastoff!"); 58 | } else { 59 | countup(n - 1); 60 | System.out.println(n); 61 | } 62 | } 63 | 64 | public static void displayBinary(int value) { 65 | if (value > 0) { 66 | displayBinary(value / 2); 67 | System.out.print(value % 2); 68 | } 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /ch06/Exercise.java: -------------------------------------------------------------------------------- 1 | public class Exercise { 2 | 3 | public static void main(String[] args) { 4 | boolean flag1 = isHoopy(202); 5 | boolean flag2 = isFrabjuous(202); 6 | System.out.println(flag1); 7 | System.out.println(flag2); 8 | if (flag1 && flag2) { 9 | System.out.println("ping!"); 10 | } 11 | if (flag1 || flag2) { 12 | System.out.println("pong!"); 13 | } 14 | } 15 | 16 | public static boolean isHoopy(int x) { 17 | boolean hoopyFlag; 18 | if (x % 2 == 0) { 19 | hoopyFlag = true; 20 | } else { 21 | hoopyFlag = false; 22 | } 23 | return hoopyFlag; 24 | } 25 | 26 | public static boolean isFrabjuous(int x) { 27 | boolean frabjuousFlag; 28 | if (x > 0) { 29 | frabjuousFlag = true; 30 | } else { 31 | frabjuousFlag = false; 32 | } 33 | return frabjuousFlag; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /ch06/Recursive.java: -------------------------------------------------------------------------------- 1 | public class Recursive { 2 | 3 | public static void main(String[] args) { 4 | System.out.println(prod(1, 4)); 5 | } 6 | 7 | public static int prod(int m, int n) { 8 | if (m == n) { 9 | return n; 10 | } else { 11 | int recurse = prod(m, n - 1); 12 | int result = n * recurse; 13 | return result; 14 | } 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /ch06/Series.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Examples from Chapter 6. 3 | */ 4 | public class Series { 5 | 6 | public static void countup(int n) { 7 | if (n == 0) { 8 | System.out.println("Blastoff!"); 9 | } else { 10 | countup(n - 1); 11 | System.out.println(n); 12 | } 13 | } 14 | 15 | public static double calculateArea(double radius) { 16 | double result = Math.PI * radius * radius; 17 | return result; 18 | } 19 | 20 | public static double calculateArea2(double radius) { 21 | return Math.PI * radius * radius; 22 | } 23 | 24 | public static double absoluteValue(double x) { 25 | if (x < 0) { 26 | return -x; 27 | } else { 28 | return x; 29 | } 30 | } 31 | 32 | public static double distance 33 | (double x1, double y1, double x2, double y2) { 34 | double dx = x2 - x1; 35 | double dy = y2 - y1; 36 | System.out.println("dx is " + dx); 37 | System.out.println("dy is " + dy); 38 | return 0.0; 39 | } 40 | 41 | public static double distance2 42 | (double x1, double y1, double x2, double y2) { 43 | double dx = x2 - x1; 44 | double dy = y2 - y1; 45 | double dsquared = dx * dx + dy * dy; 46 | System.out.println("dsquared is " + dsquared); 47 | return 0.0; 48 | } 49 | 50 | public static double distance3 51 | (double x1, double y1, double x2, double y2) { 52 | double dx = x2 - x1; 53 | double dy = y2 - y1; 54 | double dsquared = dx * dx + dy * dy; 55 | double result = Math.sqrt(dsquared); 56 | return result; 57 | } 58 | 59 | public static double circleArea 60 | (double xc, double yc, double xp, double yp) { 61 | double radius = distance(xc, yc, xp, yp); 62 | double area = calculateArea(radius); 63 | return area; 64 | } 65 | 66 | public static double calculateArea 67 | (double xc, double yc, double xp, double yp) { 68 | return calculateArea(distance(xc, yc, xp, yp)); 69 | } 70 | 71 | /** 72 | * Tests whether x is a single digit integer. 73 | * 74 | * @param x the integer to test 75 | * @return true if x has one digit, false otherwise 76 | */ 77 | public static boolean isSingleDigit(int x) { 78 | if (x > -10 && x < 10) { 79 | return true; 80 | } else { 81 | return false; 82 | } 83 | } 84 | 85 | public static boolean isSingleDigit2(int x) { 86 | return x > -10 && x < 10; 87 | } 88 | 89 | public static int factorial(int n) { 90 | if (n == 0) { 91 | return 1; 92 | } 93 | int recurse = factorial(n - 1); 94 | int result = n * recurse; 95 | return result; 96 | } 97 | 98 | public static int fibonacci(int n) { 99 | if (n == 1 || n == 2) { 100 | return 1; 101 | } 102 | return fibonacci(n - 1) + fibonacci(n - 2); 103 | } 104 | 105 | public static void main(String[] args) { 106 | countup(3); 107 | System.out.println("Have a nice day."); 108 | 109 | System.out.println("calculateArea"); 110 | System.out.println(calculateArea(3.0)); 111 | 112 | System.out.println("calculateArea2"); 113 | System.out.println(calculateArea2(3.0)); 114 | 115 | System.out.println("circleArea"); 116 | System.out.println(circleArea(1.0, 2.0, 4.0, 6.0)); 117 | 118 | System.out.println("calculateArea with 4 doubles"); 119 | System.out.println(calculateArea(1.0, 2.0, 4.0, 6.0)); 120 | 121 | System.out.println("absolute value"); 122 | System.out.println(absoluteValue(-2)); 123 | 124 | System.out.println("distance"); 125 | System.out.println(distance(1.0, 2.0, 4.0, 6.0)); 126 | 127 | System.out.println("distance2"); 128 | System.out.println(distance2(1.0, 2.0, 4.0, 6.0)); 129 | 130 | System.out.println("distance3"); 131 | System.out.println(distance3(1.0, 2.0, 4.0, 6.0)); 132 | 133 | System.out.println(isSingleDigit(2)); 134 | boolean bigFlag = !isSingleDigit2(17); 135 | 136 | int z = 9; 137 | if (isSingleDigit(z)) { 138 | System.out.println("z is small"); 139 | } else { 140 | System.out.println("z is big"); 141 | } 142 | 143 | System.out.println("factorial"); 144 | System.out.println(factorial(3)); 145 | 146 | System.out.println("fibonacci"); 147 | System.out.println(fibonacci(3)); 148 | } 149 | 150 | } 151 | -------------------------------------------------------------------------------- /ch07/Exercise.java: -------------------------------------------------------------------------------- 1 | public class Exercise { 2 | 3 | public static void main(String[] args) { 4 | loop(10); 5 | } 6 | 7 | public static void loop(int n) { 8 | int i = n; 9 | while (i > 1) { 10 | System.out.println(i); 11 | if (i % 2 == 0) { 12 | i = i / 2; 13 | } else { 14 | i = i + 1; 15 | } 16 | } 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /ch07/Loops.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Examples from Chapter 7. 3 | */ 4 | public class Loops { 5 | 6 | public static void countdown(int n) { 7 | while (n > 0) { 8 | System.out.println(n); 9 | n = n - 1; 10 | } 11 | System.out.println("Blastoff!"); 12 | } 13 | 14 | public static void sequence(int n) { 15 | while (n != 1) { 16 | System.out.println(n); 17 | if (n % 2 == 0) { // n is even 18 | n = n / 2; 19 | } else { // n is odd 20 | n = n * 3 + 1; 21 | } 22 | } 23 | } 24 | 25 | public static void main(String[] args) { 26 | System.out.println("countdown"); 27 | countdown(3); 28 | 29 | System.out.println("sequence"); 30 | sequence(10); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /ch07/Tables.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Generating tables; encapsulation and generalization. 3 | */ 4 | public class Tables { 5 | 6 | public static void example() { 7 | int i = 1; 8 | while (i < 10) { 9 | double x = i; 10 | System.out.println(x + " " + Math.log(x)); 11 | i = i + 1; 12 | } 13 | } 14 | 15 | public static void example2() { 16 | int i = 1; 17 | while (i < 10) { 18 | double x = i; 19 | System.out.println(x + " " + Math.log(x) / Math.log(2)); 20 | i = i + 1; 21 | } 22 | } 23 | 24 | public static void example3() { 25 | final double LOG2 = Math.log(2); 26 | int i = 1; 27 | while (i < 100) { 28 | double x = i; 29 | System.out.println(x + " " + Math.log(x) / LOG2); 30 | i = i * 2; 31 | } 32 | } 33 | 34 | public static void example4() { 35 | int i = 1; 36 | while (i <= 6) { 37 | System.out.printf("%4d", 2 * i); 38 | i = i + 1; 39 | } 40 | System.out.println(); 41 | } 42 | 43 | public static void printRow() { 44 | int i = 1; 45 | while (i <= 6) { 46 | System.out.printf("%4d", 2 * i); 47 | i = i + 1; 48 | } 49 | System.out.println(); 50 | } 51 | 52 | public static void printRow2(int n) { 53 | int i = 1; 54 | while (i <= 6) { 55 | System.out.printf("%4d", n * i); 56 | i = i + 1; 57 | } 58 | System.out.println(); 59 | } 60 | 61 | public static void example5() { 62 | int i = 1; 63 | while (i <= 6) { 64 | printRow2(i); 65 | i = i + 1; 66 | } 67 | } 68 | 69 | public static void printTable() { 70 | int i = 1; 71 | while (i <= 6) { 72 | printRow2(i); 73 | i = i + 1; 74 | } 75 | } 76 | 77 | public static void printTable2(int rows) { 78 | int i = 1; 79 | while (i <= rows) { 80 | printRow2(i); 81 | i = i + 1; 82 | } 83 | } 84 | 85 | public static void printRow3(int n, int cols) { 86 | int i = 1; 87 | while (i <= cols) { 88 | System.out.printf("%4d", n * i); 89 | i = i + 1; 90 | } 91 | System.out.println(); 92 | } 93 | 94 | public static void printTable3(int rows) { 95 | int i = 1; 96 | while (i <= rows) { 97 | printRow3(i, rows); 98 | i = i + 1; 99 | } 100 | } 101 | 102 | public static void printTable4(int rows) { 103 | for (int i = 1; i <= rows; i = i + 1) { 104 | printRow3(i, rows); 105 | } 106 | } 107 | 108 | public static void printRow4(int n, int cols) { 109 | int i; 110 | for (i = 1; i <= cols; i = i + 1) { 111 | System.out.printf("%4d", n * i); 112 | } 113 | System.out.println(i); 114 | } 115 | 116 | public static void main(String[] args) { 117 | System.out.println("example"); 118 | example(); 119 | 120 | System.out.println("example2"); 121 | example2(); 122 | 123 | System.out.println("example3"); 124 | example3(); 125 | 126 | System.out.println("example4"); 127 | example4(); 128 | 129 | System.out.println("example5"); 130 | example5(); 131 | 132 | System.out.println("printRow"); 133 | printRow(); 134 | 135 | System.out.println("printRow2"); 136 | printRow2(6); 137 | 138 | System.out.println("printTable"); 139 | printTable(); 140 | 141 | System.out.println("printTable2"); 142 | printTable2(6); 143 | 144 | System.out.println("printRow3"); 145 | printRow3(6, 6); 146 | 147 | System.out.println("printTable3"); 148 | printTable3(6); 149 | 150 | System.out.println("printRow4"); 151 | printRow4(6, 6); 152 | 153 | System.out.println("printTable4"); 154 | printTable4(6); 155 | } 156 | 157 | } 158 | -------------------------------------------------------------------------------- /ch07/Validate.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /** 4 | * Do-while, break, and continue. 5 | */ 6 | public class Validate { 7 | 8 | public static double scanDouble() { 9 | Scanner in = new Scanner(System.in); 10 | boolean okay; 11 | do { 12 | System.out.print("Enter a number: "); 13 | if (in.hasNextDouble()) { 14 | okay = true; 15 | } else { 16 | okay = false; 17 | String word = in.next(); 18 | System.err.println(word + " is not a number"); 19 | } 20 | } while (!okay); 21 | double x = in.nextDouble(); 22 | return x; 23 | } 24 | 25 | public static double scanDouble2() { 26 | Scanner in = new Scanner(System.in); 27 | while (true) { 28 | System.out.print("Enter a number: "); 29 | if (in.hasNextDouble()) { 30 | break; 31 | } 32 | String word = in.next(); 33 | System.err.println(word + " is not a number"); 34 | } 35 | double x = in.nextDouble(); 36 | return x; 37 | } 38 | 39 | public static double addNumbers() { 40 | Scanner in = new Scanner(System.in); 41 | int x = -1; 42 | int sum = 0; 43 | while (x != 0) { 44 | x = in.nextInt(); 45 | if (x <= 0) { 46 | continue; 47 | } 48 | System.out.println("Adding " + x); 49 | sum += x; 50 | } 51 | return sum; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /ch08/ArrayExamples.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * Demonstrates uses of arrays. 5 | */ 6 | public class ArrayExamples { 7 | 8 | /** 9 | * Example code from Chapter 8. 10 | */ 11 | public static void main(String[] args) { 12 | int size = 10; 13 | int[] counts = new int[4]; 14 | double[] values = new double[size]; 15 | 16 | counts[0] = 7; 17 | counts[1] = counts[0] * 2; 18 | counts[2]++; 19 | counts[3] -= 60; 20 | 21 | // traversal with a while loop 22 | int j = 0; 23 | while (j < 4) { 24 | System.out.println(counts[j]); 25 | j++; 26 | } 27 | 28 | // traversal with a for loop 29 | for (int i = 0; i < 4; i++) { 30 | System.out.println(counts[i]); 31 | } 32 | 33 | int[] array = {1, 2, 3, 4}; 34 | printArray(array); 35 | 36 | // printing an array as an object 37 | System.out.println(array); 38 | 39 | // printing with Arrays class 40 | System.out.println(Arrays.toString(array)); 41 | 42 | // copying an array 43 | double[] a = {1.0, 2.0, 3.0}; 44 | double[] b = new double[a.length]; 45 | for (int i = 0; i < a.length; i++) { 46 | b[i] = a[i]; 47 | } 48 | 49 | // copying with Arrays class 50 | double[] c = Arrays.copyOf(a, a.length); 51 | 52 | // traversal 53 | for (int i = 0; i < a.length; i++) { 54 | a[i] = Math.pow(a[i], 2.0); 55 | } 56 | 57 | // search 58 | int index = search(a, 2.0); 59 | System.out.println("index = " + index); 60 | 61 | // reduce 62 | double total = sum(a); 63 | System.out.println("total = " + total); 64 | } 65 | 66 | /** 67 | * Prints the elements of an array. 68 | */ 69 | public static void printArray(int[] array) { 70 | System.out.print("{" + array[0]); 71 | for (int i = 1; i < array.length; i++) { 72 | System.out.print(", " + array[i]); 73 | } 74 | System.out.println("}"); 75 | } 76 | 77 | /** 78 | * Returns the index of the target in the array, or -1 if not found. 79 | */ 80 | public static int search(double[] a, double target) { 81 | for (int i = 0; i < a.length; i++) { 82 | if (a[i] == target) { 83 | return i; 84 | } 85 | } 86 | return -1; 87 | } 88 | 89 | /** 90 | * Returns the total of the elements in an array. 91 | */ 92 | public static double sum(double[] a) { 93 | double total = 0.0; 94 | for (int i = 0; i < a.length; i++) { 95 | total += a[i]; 96 | } 97 | return total; 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /ch08/Fruit.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Fruit exercise. 3 | */ 4 | public class Fruit { 5 | 6 | public static int banana(int[] a) { 7 | int kiwi = 1; 8 | int i = 0; 9 | while (i < a.length) { 10 | kiwi = kiwi * a[i]; 11 | i++; 12 | } 13 | return kiwi; 14 | } 15 | 16 | public static int grapefruit(int[] a, int grape) { 17 | for (int i = 0; i < a.length; i++) { 18 | if (a[i] == grape) { 19 | return i; 20 | } 21 | } 22 | return -1; 23 | } 24 | 25 | public static int pineapple(int[] a, int apple) { 26 | int pear = 0; 27 | for (int pine: a) { 28 | if (pine == apple) { 29 | pear++; 30 | } 31 | } 32 | return pear; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /ch08/Histogram.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | /** 4 | * Example code related to histograms. 5 | */ 6 | public class Histogram { 7 | 8 | /** 9 | * Returns an array of random integers. 10 | */ 11 | public static int[] randomArray(int size) { 12 | Random random = new Random(); 13 | int[] a = new int[size]; 14 | for (int i = 0; i < a.length; i++) { 15 | a[i] = random.nextInt(100); 16 | } 17 | return a; 18 | } 19 | 20 | /** 21 | * Computes the number of array elements in [low, high). 22 | */ 23 | public static int inRange(int[] a, int low, int high) { 24 | int count = 0; 25 | for (int i = 0; i < a.length; i++) { 26 | if (a[i] >= low && a[i] < high) { 27 | count++; 28 | } 29 | } 30 | return count; 31 | } 32 | 33 | public static void main(String[] args) { 34 | int numValues = 8; 35 | int[] array = randomArray(numValues); 36 | ArrayExamples.printArray(array); 37 | 38 | int[] scores = randomArray(30); 39 | int a = inRange(scores, 90, 100); 40 | int b = inRange(scores, 80, 90); 41 | int c = inRange(scores, 70, 80); 42 | int d = inRange(scores, 60, 70); 43 | int f = inRange(scores, 0, 60); 44 | 45 | // making a histogram 46 | int[] counts = new int[100]; 47 | for (int i = 0; i < scores.length; i++) { 48 | int index = scores[i]; 49 | counts[index]++; 50 | } 51 | 52 | // histogram with enhanced for loop 53 | counts = new int[100]; 54 | for (int score : scores) { 55 | counts[score]++; 56 | } 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /ch08/MakeDubMus.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Stack diagram exercise. 3 | */ 4 | public class MakeDubMus { 5 | 6 | public static int[] make(int n) { 7 | int[] a = new int[n]; 8 | for (int i = 0; i < n; i++) { 9 | a[i] = i + 1; 10 | } 11 | return a; 12 | } 13 | 14 | public static void dub(int[] jub) { 15 | for (int i = 0; i < jub.length; i++) { 16 | jub[i] *= 2; 17 | } 18 | } 19 | 20 | public static int mus(int[] zoo) { 21 | int fus = 0; 22 | for (int i = 0; i < zoo.length; i++) { 23 | fus += zoo[i]; 24 | } 25 | return fus; 26 | } 27 | 28 | public static void main(String[] args) { 29 | int[] bob = make(5); 30 | dub(bob); 31 | System.out.println(mus(bob)); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /ch09/Exercise.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Exercise on encapsulation and generalization. 3 | */ 4 | public class Exercise { 5 | 6 | public static void main(String[] args) { 7 | String s = "((3 + 7) * 2)"; 8 | int count = 0; 9 | 10 | for (int i = 0; i < s.length(); i++) { 11 | char c = s.charAt(i); 12 | if (c == '(') { 13 | count++; 14 | } else if (c == ')') { 15 | count--; 16 | } 17 | } 18 | 19 | System.out.println(count); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /ch09/Format.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Example using the String.format method. 3 | */ 4 | public class Format { 5 | 6 | /** 7 | * Returns a time string in 12-hour format. 8 | * 9 | * @param hour between 0 and 23 10 | * @param minute between 0 and 59 11 | */ 12 | public static String timeString(int hour, int minute) { 13 | String ampm; 14 | if (hour < 12) { 15 | ampm = "AM"; 16 | if (hour == 0) { 17 | hour = 12; // midnight 18 | } 19 | } else { 20 | ampm = "PM"; 21 | hour = hour - 12; 22 | } 23 | return String.format("%02d:%02d %s", hour, minute, ampm); 24 | } 25 | 26 | public static void main(String[] args) { 27 | System.out.println(timeString(0, 0)); 28 | System.out.println(timeString(7, 30)); 29 | System.out.println(timeString(12, 5)); 30 | System.out.println(timeString(23, 59)); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /ch09/Max.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | /** 4 | * Demonstrates command-line arguments. 5 | */ 6 | public class Max { 7 | 8 | /** 9 | * Converts args to integers and prints the max. 10 | */ 11 | public static void main(String[] args) { 12 | System.out.println(Arrays.toString(args)); 13 | 14 | int max = Integer.MIN_VALUE; 15 | for (String arg : args) { 16 | int value = Integer.parseInt(arg); 17 | if (value > max) { 18 | max = value; 19 | } 20 | } 21 | System.out.println("The max is " + max); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /ch09/Recurse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Recursion exercise. 3 | */ 4 | public class Recurse { 5 | 6 | /** 7 | * Returns the first character of the given String. 8 | */ 9 | public static char first(String s) { 10 | return s.charAt(0); 11 | } 12 | 13 | /** 14 | * Returns all but the first letter of the given String. 15 | */ 16 | public static String rest(String s) { 17 | return s.substring(1); 18 | } 19 | 20 | /** 21 | * Returns all but the first and last letter of the String. 22 | */ 23 | public static String middle(String s) { 24 | return s.substring(1, s.length() - 1); 25 | } 26 | 27 | /** 28 | * Returns the length of the given String. 29 | */ 30 | public static int length(String s) { 31 | return s.length(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /ch09/StringsThings.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstates uses of Strings. 3 | */ 4 | public class StringsThings { 5 | 6 | public static void main(String[] args) { 7 | 8 | // Characters 9 | 10 | String fruit = "banana"; 11 | char letter0 = fruit.charAt(0); 12 | 13 | if (letter0 == 'a') { 14 | System.out.println('?'); 15 | } 16 | 17 | System.out.print("Roman alphabet: "); 18 | for (char c = 'A'; c <= 'Z'; c++) { 19 | System.out.print(c); 20 | } 21 | System.out.println(); 22 | 23 | System.out.print("Greek alphabet: "); 24 | for (int i = 913; i <= 937; i++) { 25 | System.out.print((char) i); 26 | } 27 | System.out.println(); 28 | 29 | // Strings are immutable 30 | 31 | String name = "Alan Turing"; 32 | String upperName = name.toUpperCase(); 33 | 34 | String text = "Computer Science is fun!"; 35 | text = text.replace("Computer Science", "CS"); 36 | 37 | // String traversal 38 | 39 | for (int i = 0; i < fruit.length(); i++) { 40 | char letter = fruit.charAt(i); 41 | System.out.println(letter); 42 | } 43 | 44 | for (char letter : fruit.toCharArray()) { 45 | System.out.println(letter); 46 | } 47 | 48 | int length = fruit.length(); 49 | char last = fruit.charAt(length - 1); // correct 50 | 51 | System.out.println(reverse(fruit)); 52 | 53 | // Substrings 54 | 55 | System.out.println(fruit.substring(0)); 56 | System.out.println(fruit.substring(2)); 57 | System.out.println(fruit.substring(6)); 58 | 59 | System.out.println(fruit.substring(0, 3)); 60 | System.out.println(fruit.substring(2, 5)); 61 | System.out.println(fruit.substring(6, 6)); 62 | 63 | // The indexOf method 64 | 65 | int index = fruit.indexOf('a'); 66 | int index2 = fruit.indexOf('a', 2); 67 | 68 | // String comparison 69 | 70 | String name1 = "Alan Turing"; 71 | String name2 = "Ada Lovelace"; 72 | if (name1.equals(name2)) { 73 | System.out.println("The names are the same."); 74 | } 75 | 76 | int diff = name1.compareTo(name2); 77 | if (diff == 0) { 78 | System.out.println("The names are the same."); 79 | } else if (diff < 0) { 80 | System.out.println("name1 comes before name2."); 81 | } else if (diff > 0) { 82 | System.out.println("name2 comes before name1."); 83 | } 84 | 85 | // Wrapper classes 86 | 87 | String str = "12345"; 88 | int num = Integer.parseInt(str); 89 | 90 | num = 12345; 91 | str = Integer.toString(num); 92 | } 93 | 94 | /** 95 | * Reverses a string, returns a new String. 96 | */ 97 | public static String reverse(String s) { 98 | String r = ""; 99 | for (int i = s.length() - 1; i >= 0; i--) { 100 | r = r + s.charAt(i); 101 | } 102 | return r; 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /ch10/PointRect.java: -------------------------------------------------------------------------------- 1 | import java.awt.Point; 2 | import java.awt.Rectangle; 3 | 4 | /** 5 | * Demonstates use of Point and Rectangle classes. 6 | */ 7 | public class PointRect { 8 | 9 | public static void main(String[] args) { 10 | Point blank; 11 | blank = new Point(3, 4); 12 | System.out.println(blank); 13 | 14 | int x = blank.x; 15 | System.out.println(blank.x + ", " + blank.y); 16 | int sum = blank.x * blank.x + blank.y * blank.y; 17 | 18 | Rectangle box = new Rectangle(0, 0, 100, 200); 19 | moveRect(box, 50, 100); 20 | System.out.println(box); 21 | box.translate(50, 100); 22 | 23 | Rectangle box1 = new Rectangle(0, 0, 100, 200); 24 | Rectangle box2 = box1; 25 | 26 | System.out.println(box2.width); 27 | box1.grow(50, 50); 28 | System.out.println(box2.width); 29 | } 30 | 31 | /** 32 | * Prints the attributes of a Point object. 33 | */ 34 | public static void printPoint(Point p) { 35 | System.out.println("(" + p.x + ", " + p.y + ")"); 36 | } 37 | 38 | /** 39 | * Computes the distance between two points. 40 | */ 41 | public static double distance(Point p1, Point p2) { 42 | int dx = p2.x - p1.x; 43 | int dy = p2.y - p1.y; 44 | return Math.sqrt(dx * dx + dy * dy); 45 | } 46 | 47 | /** 48 | * Finds the center of a Rectangle and returns a new Point. 49 | */ 50 | public static Point findCenter(Rectangle box) { 51 | int x = box.x + box.width / 2; 52 | int y = box.y + box.height / 2; 53 | return new Point(x, y); 54 | } 55 | 56 | /** 57 | * Moves a Rectangle by modifying the x and y attributes. 58 | */ 59 | public static void moveRect(Rectangle box, int dx, int dy) { 60 | box.x = box.x + dx; 61 | box.y = box.y + dy; 62 | } 63 | 64 | /** 65 | * Exercise on returning objects. 66 | */ 67 | public static void exercise2() { 68 | Point blank = new Point(5, 8); 69 | 70 | Rectangle rect = new Rectangle(0, 2, 4, 4); 71 | Point center = findCenter(rect); 72 | 73 | double dist = distance(center, blank); 74 | System.out.println(dist); 75 | } 76 | 77 | /** 78 | * Exercise on aliasing. 79 | */ 80 | public static void exercise3() { 81 | Rectangle box1 = new Rectangle(2, 4, 7, 9); 82 | Point p1 = findCenter(box1); 83 | printPoint(p1); 84 | 85 | box1.grow(1, 1); 86 | Point p2 = findCenter(box1); 87 | printPoint(p2); 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /ch10/Pow.java: -------------------------------------------------------------------------------- 1 | /** 2 | * BigInteger exercise. 3 | */ 4 | public class Pow { 5 | 6 | /** 7 | * Integer exponentiation. 8 | */ 9 | public static int pow(int x, int n) { 10 | if (n == 0) return 1; 11 | 12 | // find x to the n/2 recursively 13 | int t = pow(x, n / 2); 14 | 15 | // if n is even, the result is t squared 16 | // if n is odd, the result is t squared times x 17 | if (n % 2 == 0) { 18 | return t * t; 19 | } else { 20 | return t * t * x; 21 | } 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /ch10/Riddle.java: -------------------------------------------------------------------------------- 1 | import java.awt.Point; 2 | 3 | /** 4 | * Exercise on passing objects as parameters. 5 | */ 6 | public class Riddle { 7 | 8 | public static int riddle(int x, Point p) { 9 | x = x + 7; 10 | return x + p.x + p.y; 11 | } 12 | 13 | public static void main(String[] args) { 14 | int x = 5; 15 | Point blank = new Point(1, 2); 16 | 17 | System.out.println(riddle(x, blank)); 18 | System.out.println(x); 19 | System.out.println(blank.x); 20 | System.out.println(blank.y); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /ch11/Time.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Represents a time of day. 3 | */ 4 | public class Time { 5 | 6 | private int hour; 7 | private int minute; 8 | private double second; 9 | 10 | /** 11 | * Construct a Time object with default values. 12 | */ 13 | public Time() { 14 | this.hour = 0; 15 | this.minute = 0; 16 | this.second = 0.0; 17 | } 18 | 19 | /** 20 | * Construct a Time object with given values. 21 | */ 22 | public Time(int hour, int minute, double second) { 23 | this.hour = hour; 24 | this.minute = minute; 25 | this.second = second; 26 | } 27 | 28 | /** 29 | * Prints the time in a simple format. 30 | */ 31 | public static void printTime(Time t) { 32 | System.out.print(t.hour); 33 | System.out.print(":"); 34 | System.out.println(t.minute); 35 | System.out.print(":"); 36 | System.out.println(t.second); 37 | } 38 | 39 | /** 40 | * Returns a String representation of the time. 41 | */ 42 | public String toString() { 43 | return String.format("%02d:%02d:%04.1f\n", 44 | this.hour, this.minute, this.second); 45 | } 46 | 47 | /** 48 | * Tests whether two times are equivalent. 49 | */ 50 | public boolean equals(Time that) { 51 | return this.hour == that.hour 52 | && this.minute == that.minute 53 | && this.second == that.second; 54 | } 55 | 56 | /** 57 | * Adds two Times and returns a new Time object (static method). 58 | */ 59 | public static Time add(Time t1, Time t2) { 60 | Time sum = new Time(); 61 | sum.hour = t1.hour + t2.hour; 62 | sum.minute = t1.minute + t2.minute; 63 | sum.second = t1.second + t2.second; 64 | return sum; 65 | } 66 | 67 | /** 68 | * Adds two Times and returns a new Time object (instance method). 69 | */ 70 | public Time add(Time t2) { 71 | Time sum = new Time(); 72 | sum.hour = this.hour + t2.hour; 73 | sum.minute = this.minute + t2.minute; 74 | sum.second = this.second + t2.second; 75 | 76 | if (sum.second >= 60.0) { 77 | sum.second -= 60.0; 78 | sum.minute += 1; 79 | } 80 | if (sum.minute >= 60) { 81 | sum.minute -= 60; 82 | sum.hour += 1; 83 | } 84 | return sum; 85 | } 86 | 87 | /** 88 | * Adds the given number of seconds to this object (modifier). 89 | */ 90 | public void increment(double seconds) { 91 | this.second += seconds; 92 | while (this.second >= 60.0) { 93 | this.second -= 60.0; 94 | this.minute += 1; 95 | } 96 | while (this.minute >= 60) { 97 | this.minute -= 60; 98 | this.hour += 1; 99 | } 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /ch11/TimeClient.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Class that uses Time objects. 3 | */ 4 | public class TimeClient { 5 | 6 | public static void main(String[] args) { 7 | Time time = new Time(11, 59, 59.9); 8 | System.out.println(time); 9 | 10 | // cannot access private variables from another class 11 | // System.out.println(time.hour); 12 | 13 | String s = time.toString(); 14 | System.out.println(s); 15 | 16 | Time time1 = new Time(9, 30, 0.0); 17 | Time time2 = time1; 18 | Time time3 = new Time(9, 30, 0.0); 19 | 20 | System.out.println(time1 == time2); 21 | System.out.println(time1 == time3); 22 | System.out.println(time1.equals(time2)); 23 | System.out.println(time1.equals(time3)); 24 | 25 | Time startTime = new Time(18, 50, 0.0); 26 | Time runningTime = new Time(2, 16, 0.0); 27 | Time endTime = Time.add(startTime, runningTime); 28 | 29 | // using the instance method 30 | endTime = startTime.add(runningTime); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /ch12/Card.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A standard playing card. 3 | */ 4 | public class Card { 5 | 6 | public static final String[] RANKS = { 7 | null, "Ace", "2", "3", "4", "5", "6", "7", 8 | "8", "9", "10", "Jack", "Queen", "King"}; 9 | 10 | public static final String[] SUITS = { 11 | "Clubs", "Diamonds", "Hearts", "Spades"}; 12 | 13 | private final int rank; 14 | 15 | private final int suit; 16 | 17 | /** 18 | * Constructs a card of the given rank and suit. 19 | */ 20 | public Card(int rank, int suit) { 21 | this.rank = rank; 22 | this.suit = suit; 23 | } 24 | 25 | /** 26 | * Returns a negative integer if this card comes before 27 | * the given card, zero if the two cards are equal, or 28 | * a positive integer if this card comes after the card. 29 | */ 30 | public int compareTo(Card that) { 31 | if (this.suit < that.suit) { 32 | return -1; 33 | } 34 | if (this.suit > that.suit) { 35 | return 1; 36 | } 37 | if (this.rank < that.rank) { 38 | return -1; 39 | } 40 | if (this.rank > that.rank) { 41 | return 1; 42 | } 43 | return 0; 44 | } 45 | 46 | /** 47 | * Returns true if the given card has the same 48 | * rank AND same suit; otherwise returns false. 49 | */ 50 | public boolean equals(Card that) { 51 | return this.rank == that.rank 52 | && this.suit == that.suit; 53 | } 54 | 55 | /** 56 | * Gets the card's rank. 57 | */ 58 | public int getRank() { 59 | return this.rank; 60 | } 61 | 62 | /** 63 | * Gets the card's suit. 64 | */ 65 | public int getSuit() { 66 | return this.suit; 67 | } 68 | 69 | /** 70 | * Returns the card's index in a sorted deck of 52 cards. 71 | */ 72 | public int position() { 73 | return this.suit * 13 + this.rank - 1; 74 | } 75 | 76 | /** 77 | * Returns a string representation of the card. 78 | */ 79 | public String toString() { 80 | return RANKS[this.rank] + " of " + SUITS[this.suit]; 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /ch12/CardTable.java: -------------------------------------------------------------------------------- 1 | import java.awt.Canvas; 2 | import java.awt.Color; 3 | import java.awt.Graphics; 4 | import java.awt.Image; 5 | 6 | import javax.swing.ImageIcon; 7 | import javax.swing.JFrame; 8 | 9 | public class CardTable extends Canvas { 10 | 11 | private Image[][] images; 12 | private int cardWidth, cardHeight; 13 | 14 | /** 15 | * Creates a CardTable. 16 | * cardset is the name of the folder that contains the card images. 17 | */ 18 | public CardTable(String cardset) { 19 | setBackground(new Color(0x088A4B)); 20 | 21 | // create a 2-D array of card images 22 | images = new Image[14][4]; 23 | String suits = "cdhs"; 24 | 25 | for (int suit = 0; suit <= 3; suit++) { 26 | char c = suits.charAt(suit); 27 | 28 | for (int rank = 1; rank <= 13; rank++) { 29 | String s = String.format("%s/%02d%c.gif", 30 | cardset, rank, c); 31 | images[rank][suit] = new ImageIcon(s).getImage(); 32 | } 33 | } 34 | 35 | // get the width and height of the cards and set the size of 36 | // the frame accordingly 37 | cardWidth = images[1][1].getWidth(null); 38 | cardHeight = images[1][1].getHeight(null); 39 | 40 | // set the size temporarily to get the insets 41 | setTableSize(14, 4); 42 | } 43 | 44 | /** 45 | * Sets the table size. 46 | * x and y are in units of card width/height. 47 | */ 48 | public void setTableSize(double x, double y) { 49 | setSize((int) (x * cardWidth), 50 | (int) (y * cardHeight)); 51 | } 52 | 53 | /** 54 | * Draws a card at the given coordinates. 55 | * x and y are in units of card width/height. 56 | */ 57 | public void drawCard(Graphics g, int rank, int suit, double x, double y) { 58 | Image image = images[rank][suit]; 59 | g.drawImage(image, 60 | (int) (x * cardWidth), 61 | (int) (y * cardHeight), 62 | null); 63 | } 64 | 65 | /** 66 | * Special method invoked when the Frame needs to be drawn. 67 | */ 68 | public void paint(Graphics g) { 69 | for (int rank = 1; rank <= 13; rank++) { 70 | for (int suit = 0; suit <= 3; suit++) { 71 | double x = rank - 1 + suit / 5.0; 72 | double y = suit / 2.0; 73 | drawCard(g, rank, suit, x, y); 74 | } 75 | } 76 | } 77 | 78 | public static void main(String[] args) { 79 | // make the frame 80 | JFrame frame = new JFrame("Card Table"); 81 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 82 | 83 | // add the CardTable 84 | String cardset = "cardset-oxymoron"; 85 | Canvas canvas = new CardTable(cardset); 86 | frame.getContentPane().add(canvas); 87 | 88 | // show the frame 89 | frame.pack(); 90 | frame.setVisible(true); 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /ch12/Search.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Search algorithms for arrays of cards. 3 | */ 4 | public class Search { 5 | 6 | /** 7 | * Make an array of 52 cards. 8 | */ 9 | public static Card[] makeDeck() { 10 | Card[] cards = new Card[52]; 11 | int index = 0; 12 | for (int suit = 0; suit <= 3; suit++) { 13 | for (int rank = 1; rank <= 13; rank++) { 14 | cards[index] = new Card(rank, suit); 15 | index++; 16 | } 17 | } 18 | return cards; 19 | } 20 | 21 | /** 22 | * Displays the given deck of cards. 23 | */ 24 | public static void printDeck(Card[] cards) { 25 | for (int i = 0; i < cards.length; i++) { 26 | System.out.println(cards[i]); 27 | } 28 | } 29 | 30 | /** 31 | * Sequential search. 32 | */ 33 | public static int search(Card[] cards, Card target) { 34 | for (int i = 0; i < cards.length; i++) { 35 | if (cards[i].equals(target)) { 36 | return i; 37 | } 38 | } 39 | return -1; 40 | } 41 | 42 | /** 43 | * Binary search (iterative version). 44 | */ 45 | public static int binarySearch(Card[] cards, Card target) { 46 | int low = 0; 47 | int high = cards.length - 1; 48 | while (low <= high) { 49 | System.out.println(low + ", " + high); 50 | 51 | int mid = (low + high) / 2; // step 1 52 | int comp = cards[mid].compareTo(target); 53 | 54 | if (comp == 0) { // step 2 55 | return mid; 56 | } else if (comp < 0) { // step 3 57 | low = mid + 1; 58 | } else { // step 4 59 | high = mid - 1; 60 | } 61 | } 62 | return -1; 63 | } 64 | 65 | /** 66 | * Binary search (recursive version). 67 | */ 68 | public static int binarySearch(Card[] cards, Card target, 69 | int low, int high) { 70 | System.out.println(low + ", " + high); 71 | 72 | if (high < low) { 73 | return -1; 74 | } 75 | int mid = (low + high) / 2; // step 1 76 | int comp = cards[mid].compareTo(target); 77 | 78 | if (comp == 0) { // step 2 79 | return mid; 80 | } else if (comp < 0) { // step 3 81 | return binarySearch(cards, target, mid + 1, high); 82 | } else { // step 4 83 | return binarySearch(cards, target, low, mid - 1); 84 | } 85 | } 86 | 87 | /** 88 | * Demonstrates how to call the search methods. 89 | */ 90 | public static void main(String[] args) { 91 | Card[] cards = makeDeck(); 92 | Card jack = new Card(11, 0); 93 | Card fake = new Card(15, 1); 94 | 95 | System.out.println("Sequential search"); 96 | System.out.println(search(cards, jack)); 97 | System.out.println(); 98 | 99 | System.out.println("Binary search"); 100 | System.out.println(binarySearch(cards, jack)); 101 | System.out.println(); 102 | 103 | System.out.println("Failed binary search"); 104 | System.out.println(binarySearch(cards, fake)); 105 | System.out.println(); 106 | 107 | System.out.println("Recursive binary search"); 108 | System.out.println(binarySearch(cards, jack, 0, 51)); 109 | System.out.println(); 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/01c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/01c.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/01d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/01d.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/01h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/01h.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/01s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/01s.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/02c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/02c.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/02d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/02d.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/02h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/02h.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/02s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/02s.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/03c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/03c.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/03d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/03d.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/03h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/03h.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/03s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/03s.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/04c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/04c.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/04d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/04d.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/04h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/04h.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/04s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/04s.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/05c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/05c.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/05d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/05d.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/05h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/05h.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/05s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/05s.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/06c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/06c.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/06d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/06d.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/06h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/06h.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/06s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/06s.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/07c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/07c.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/07d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/07d.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/07h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/07h.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/07s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/07s.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/08c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/08c.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/08d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/08d.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/08h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/08h.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/08s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/08s.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/09c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/09c.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/09d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/09d.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/09h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/09h.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/09s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/09s.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/10c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/10c.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/10d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/10d.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/10h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/10h.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/10s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/10s.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/11c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/11c.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/11d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/11d.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/11h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/11h.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/11s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/11s.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/12c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/12c.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/12d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/12d.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/12h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/12h.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/12s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/12s.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/13c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/13c.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/13d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/13d.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/13h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/13h.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/13s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/13s.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/COPYRIGHT: -------------------------------------------------------------------------------- 1 | This PySol cardset was adapted from cards-2.0.tar.gz. 2 | http://www.waste.org/~oxymoron/cards/ 3 | 4 | Copyright (C) 1998 Oliver Xymoron 5 | Copyright (C) 1999 Markus F.X.J. Oberhumer 6 | 7 | This cardset is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU General Public License 9 | as published by the Free Software Foundation; either version 2 of 10 | the License, or (at your option) any later version. 11 | -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/back001.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/back001.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/back101.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/back101.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/back102.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/back102.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/back111.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/back111.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/back191.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/back191.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/back192.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/back192.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/bottom01.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/bottom01.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/bottom02.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/bottom02.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/bottom03.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/bottom03.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/bottom04.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/bottom04.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/bottom05.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/bottom05.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/bottom06.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/bottom06.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/bottom07.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/bottom07.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/config.txt: -------------------------------------------------------------------------------- 1 | PySol solitaire cardset;4;.gif;1;52;0 2 | 1000_oxymoron_073097;Oxymoron 3 | 73 97 8 4 | 18 18 7 7 5 | back001.gif 6 | back001.gif;back101.gif;back102.gif;back111.gif;back191.gif;back192.gif 7 | -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/l01.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/l01.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/l02.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/l02.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/l03.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/l03.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/l04.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/l04.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/shade.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/shade.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/shadow00.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/shadow00.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/shadow01.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/shadow01.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/shadow02.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/shadow02.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/shadow03.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/shadow03.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/shadow04.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/shadow04.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/shadow05.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/shadow05.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/shadow06.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/shadow06.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/shadow07.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/shadow07.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/shadow08.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/shadow08.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/shadow09.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/shadow09.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/shadow10.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/shadow10.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/shadow11.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/shadow11.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/shadow12.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/shadow12.gif -------------------------------------------------------------------------------- /ch12/cardset-oxymoron/shadow13.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AllenDowney/ThinkJavaCode/ce03ee90086e66dfd3141813f85f6b034bcb695c/ch12/cardset-oxymoron/shadow13.gif -------------------------------------------------------------------------------- /ch13/Card.java: -------------------------------------------------------------------------------- 1 | ../ch12/Card.java -------------------------------------------------------------------------------- /ch13/Deck.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Random; 3 | 4 | /** 5 | * A deck of playing cards (of fixed size). 6 | */ 7 | public class Deck { 8 | 9 | private Card[] cards; 10 | 11 | /** 12 | * Constructs a standard deck of 52 cards. 13 | */ 14 | public Deck() { 15 | this.cards = new Card[52]; 16 | int index = 0; 17 | for (int suit = 0; suit <= 3; suit++) { 18 | for (int rank = 1; rank <= 13; rank++) { 19 | this.cards[index] = new Card(rank, suit); 20 | index++; 21 | } 22 | } 23 | } 24 | 25 | /** 26 | * Constructs a deck of n cards (null). 27 | */ 28 | public Deck(int n) { 29 | this.cards = new Card[n]; 30 | } 31 | 32 | /** 33 | * Gets the internal cards array. 34 | */ 35 | public Card[] getCards() { 36 | return this.cards; 37 | } 38 | 39 | /** 40 | * Displays each of the cards in the deck. 41 | */ 42 | public void print() { 43 | for (int i = 0; i < this.cards.length; i++) { 44 | System.out.println(this.cards[i]); 45 | } 46 | } 47 | 48 | /** 49 | * Returns a string representation of the deck. 50 | */ 51 | public String toString() { 52 | return Arrays.toString(this.cards); 53 | } 54 | 55 | /** 56 | * Chooses a random number between low and high, including both. 57 | */ 58 | public int randomInt(int low, int high) { 59 | return 0; 60 | } 61 | 62 | /** 63 | * Swaps the cards at indexes i and j. 64 | */ 65 | public void swapCards(int i, int j) { 66 | } 67 | 68 | /** 69 | * Randomly permutes the array of cards. 70 | */ 71 | public void shuffle() { 72 | } 73 | 74 | /** 75 | * Finds the index of the lowest card 76 | * between low and high inclusive. 77 | */ 78 | public int indexLowest(int low, int high) { 79 | return 0; 80 | } 81 | 82 | /** 83 | * Sorts the cards (in place) using selection sort. 84 | */ 85 | public void selectionSort() { 86 | } 87 | 88 | /** 89 | * Returns a subset of the cards in the deck. 90 | */ 91 | public Deck subdeck(int low, int high) { 92 | Deck sub = new Deck(high - low + 1); 93 | for (int i = 0; i < sub.cards.length; i++) { 94 | sub.cards[i] = this.cards[low + i]; 95 | } 96 | return sub; 97 | } 98 | 99 | /** 100 | * Combines two previously sorted subdecks. 101 | */ 102 | public static Deck merge(Deck d1, Deck d2) { 103 | return null; 104 | } 105 | 106 | /** 107 | * Returns a sorted copy of the deck using merge sort. 108 | */ 109 | public Deck mergeSort() { 110 | return this; 111 | } 112 | 113 | /** 114 | * Reorders the cards (in place) using insertion sort. 115 | */ 116 | public void insertionSort() { 117 | } 118 | 119 | } 120 | -------------------------------------------------------------------------------- /ch13/Test.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Test sorting algorithms for decks of cards. 3 | */ 4 | public class Test { 5 | 6 | /** 7 | * Checks that the deck is sorted. 8 | */ 9 | public static void checkSorted(Deck deck) { 10 | Card[] cards = deck.getCards(); 11 | for (int i = 0; i < cards.length - 1; i++) { 12 | if (cards[i].compareTo(cards[i + 1]) >= 0) { 13 | System.out.println("Card #" + i + " not sorted!"); 14 | } 15 | } 16 | } 17 | 18 | /** 19 | * Demonstrates how to call the sorting methods. 20 | */ 21 | public static void main(String[] args) { 22 | Deck deck; 23 | 24 | System.out.println("Testing selection..."); 25 | deck = new Deck(); 26 | deck.shuffle(); 27 | deck.selectionSort(); 28 | checkSorted(deck); 29 | 30 | System.out.println("Testing mergesort..."); 31 | deck = new Deck(); 32 | deck.shuffle(); 33 | deck = deck.mergeSort(); 34 | checkSorted(deck); 35 | 36 | System.out.println("Testing insertion..."); 37 | deck = new Deck(); 38 | deck.shuffle(); 39 | deck.insertionSort(); 40 | checkSorted(deck); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /ch14/Card.java: -------------------------------------------------------------------------------- 1 | ../ch12/Card.java -------------------------------------------------------------------------------- /ch14/CardCollection.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Random; 3 | 4 | /** 5 | * A collection of playing cards. 6 | */ 7 | public class CardCollection { 8 | 9 | private String label; 10 | private ArrayList cards; 11 | 12 | /** 13 | * Constructs an empty collection. 14 | */ 15 | public CardCollection(String label) { 16 | this.label = label; 17 | this.cards = new ArrayList(); 18 | } 19 | 20 | /** 21 | * Returns the label of the card collection. 22 | */ 23 | public String getLabel() { 24 | return label; 25 | } 26 | 27 | /** 28 | * Adds the given card to the collection. 29 | */ 30 | public void addCard(Card card) { 31 | cards.add(card); 32 | } 33 | 34 | /** 35 | * Removes and returns the card with the given index. 36 | */ 37 | public Card popCard(int i) { 38 | return cards.remove(i); 39 | } 40 | 41 | /** 42 | * Removes and returns the last card. 43 | */ 44 | public Card popCard() { 45 | int i = size() - 1; 46 | return popCard(i); 47 | } 48 | 49 | /** 50 | * Returns the number of cards. 51 | */ 52 | public int size() { 53 | return cards.size(); 54 | } 55 | 56 | /** 57 | * True if the collection is empty, false otherwise. 58 | */ 59 | public boolean empty() { 60 | return cards.size() == 0; 61 | } 62 | 63 | /** 64 | * Moves n cards from this collection to the given collection. 65 | */ 66 | public void deal(CardCollection that, int n) { 67 | for (int i = 0; i < n; i++) { 68 | Card card = popCard(); 69 | that.addCard(card); 70 | } 71 | } 72 | 73 | /** 74 | * Moves all remaining cards to the given collection. 75 | */ 76 | public void dealAll(CardCollection that) { 77 | int n = size(); 78 | deal(that, n); 79 | } 80 | 81 | /** 82 | * Returns the card with the given index. 83 | */ 84 | public Card getCard(int i) { 85 | return cards.get(i); 86 | } 87 | 88 | /** 89 | * Returns the last card. 90 | */ 91 | public Card last() { 92 | int i = size() - 1; 93 | return cards.get(i); 94 | } 95 | 96 | /** 97 | * Swaps the cards at indexes i and j. 98 | */ 99 | public void swapCards(int i, int j) { 100 | Card temp = cards.get(i); 101 | cards.set(i, cards.get(j)); 102 | cards.set(j, temp); 103 | } 104 | 105 | /** 106 | * Randomly permute the cards. 107 | */ 108 | public void shuffle() { 109 | Random random = new Random(); 110 | for (int i = size() - 1; i > 0; i--) { 111 | int j = random.nextInt(i); 112 | swapCards(i, j); 113 | } 114 | } 115 | 116 | /** 117 | * Returns a string representation of the card collection. 118 | */ 119 | public String toString() { 120 | return label + ": " + cards.toString(); 121 | } 122 | 123 | } 124 | -------------------------------------------------------------------------------- /ch14/Deck.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A deck of playing cards. 3 | */ 4 | public class Deck extends CardCollection { 5 | 6 | /** 7 | * Constructs a standard deck of 52 cards. 8 | */ 9 | public Deck(String label) { 10 | super(label); 11 | 12 | for (int suit = 0; suit <= 3; suit++) { 13 | for (int rank = 1; rank <= 13; rank++) { 14 | addCard(new Card(rank, suit)); 15 | } 16 | } 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /ch14/Eights.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /** 4 | * Simulates a game of Crazy Eights. See 5 | * https://en.wikipedia.org/wiki/Crazy_Eights 6 | * for basic play and scoring rules. 7 | */ 8 | public class Eights { 9 | 10 | private Player one; 11 | private Player two; 12 | private Hand drawPile; 13 | private Hand discardPile; 14 | private Scanner in; 15 | 16 | /** 17 | * Initializes the state of the game. 18 | */ 19 | public Eights() { 20 | Deck deck = new Deck("Deck"); 21 | deck.shuffle(); 22 | 23 | // deal cards to each player 24 | int handSize = 5; 25 | one = new Player("Allen"); 26 | deck.deal(one.getHand(), handSize); 27 | 28 | two = new Player("Chris"); 29 | deck.deal(two.getHand(), handSize); 30 | 31 | // turn one card face up 32 | discardPile = new Hand("Discards"); 33 | deck.deal(discardPile, 1); 34 | 35 | // put the rest of the deck face down 36 | drawPile = new Hand("Draw pile"); 37 | deck.dealAll(drawPile); 38 | 39 | // create the scanner we'll use to wait for the user 40 | in = new Scanner(System.in); 41 | } 42 | 43 | /** 44 | * Returns true if either hand is empty. 45 | */ 46 | public boolean isDone() { 47 | return one.getHand().empty() || two.getHand().empty(); 48 | } 49 | 50 | /** 51 | * Moves cards from the discard pile to the draw pile and shuffles. 52 | */ 53 | public void reshuffle() { 54 | // save the top card 55 | Card prev = discardPile.popCard(); 56 | 57 | // move the rest of the cards 58 | discardPile.dealAll(drawPile); 59 | 60 | // put the top card back 61 | discardPile.addCard(prev); 62 | 63 | // shuffle the draw pile 64 | drawPile.shuffle(); 65 | } 66 | 67 | /** 68 | * Returns a card from the draw pile. 69 | */ 70 | public Card draw() { 71 | if (drawPile.empty()) { 72 | reshuffle(); 73 | } 74 | return drawPile.popCard(); 75 | } 76 | 77 | /** 78 | * Switches players. 79 | */ 80 | public Player nextPlayer(Player current) { 81 | if (current == one) { 82 | return two; 83 | } else { 84 | return one; 85 | } 86 | } 87 | 88 | /** 89 | * Displays the state of the game. 90 | */ 91 | public void displayState() { 92 | one.display(); 93 | two.display(); 94 | discardPile.display(); 95 | System.out.print("Draw pile: "); 96 | System.out.println(drawPile.size() + " cards"); 97 | } 98 | 99 | /** 100 | * Waits for the user to press enter. 101 | */ 102 | public void waitForUser() { 103 | in.nextLine(); 104 | } 105 | 106 | /** 107 | * One player takes a turn. 108 | */ 109 | public void takeTurn(Player player) { 110 | Card prev = discardPile.last(); 111 | Card next = player.play(this, prev); 112 | discardPile.addCard(next); 113 | 114 | System.out.println(player.getName() + " plays " + next); 115 | System.out.println(); 116 | } 117 | 118 | /** 119 | * Plays the game. 120 | */ 121 | public void playGame() { 122 | Player player = one; 123 | 124 | // keep playing until there's a winner 125 | while (!isDone()) { 126 | displayState(); 127 | waitForUser(); 128 | takeTurn(player); 129 | player = nextPlayer(player); 130 | } 131 | 132 | // display the final score 133 | one.displayScore(); 134 | two.displayScore(); 135 | } 136 | 137 | /** 138 | * Creates the game and runs it. 139 | */ 140 | public static void main(String[] args) { 141 | Eights game = new Eights(); 142 | game.playGame(); 143 | } 144 | 145 | } 146 | -------------------------------------------------------------------------------- /ch14/Hand.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A hand of playing cards. 3 | */ 4 | public class Hand extends CardCollection { 5 | 6 | /** 7 | * Constructs an empty hand. 8 | */ 9 | public Hand(String label) { 10 | super(label); 11 | } 12 | 13 | /** 14 | * Prints the label and cards. 15 | */ 16 | public void display() { 17 | System.out.println(getLabel() + ": "); 18 | for (int i = 0; i < size(); i++) { 19 | System.out.println(getCard(i)); 20 | } 21 | System.out.println(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /ch14/Player.java: -------------------------------------------------------------------------------- 1 | /** 2 | * A player in a game of crazy eights. 3 | */ 4 | public class Player { 5 | 6 | private String name; 7 | private Hand hand; 8 | 9 | /** 10 | * Constructs a player with an empty hand. 11 | */ 12 | public Player(String name) { 13 | this.name = name; 14 | this.hand = new Hand(name); 15 | } 16 | 17 | /** 18 | * Gets the player's name. 19 | */ 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | /** 25 | * Gets the player's hand. 26 | */ 27 | public Hand getHand() { 28 | return hand; 29 | } 30 | 31 | /** 32 | * Removes and returns a legal card from the player's hand. 33 | */ 34 | public Card play(Eights eights, Card prev) { 35 | Card card = searchForMatch(prev); 36 | if (card == null) { 37 | card = drawForMatch(eights, prev); 38 | } 39 | return card; 40 | } 41 | 42 | /** 43 | * Searches the player's hand for a matching card. 44 | */ 45 | public Card searchForMatch(Card prev) { 46 | for (int i = 0; i < hand.size(); i++) { 47 | Card card = hand.getCard(i); 48 | if (cardMatches(card, prev)) { 49 | return hand.popCard(i); 50 | } 51 | } 52 | return null; 53 | } 54 | 55 | /** 56 | * Draws cards until a match is found. 57 | */ 58 | public Card drawForMatch(Eights eights, Card prev) { 59 | while (true) { 60 | Card card = eights.draw(); 61 | System.out.println(name + " draws " + card); 62 | if (cardMatches(card, prev)) { 63 | return card; 64 | } 65 | hand.addCard(card); 66 | } 67 | } 68 | 69 | /** 70 | * Checks whether two cards match. 71 | */ 72 | public static boolean cardMatches(Card card1, Card card2) { 73 | if (card1.getSuit() == card2.getSuit()) { 74 | return true; 75 | } 76 | if (card1.getRank() == card2.getRank()) { 77 | return true; 78 | } 79 | if (card1.getRank() == 8) { 80 | return true; 81 | } 82 | return false; 83 | } 84 | 85 | /** 86 | * Calculates the player's score (penalty points). 87 | */ 88 | public int score() { 89 | int sum = 0; 90 | for (int i = 0; i < hand.size(); i++) { 91 | Card card = hand.getCard(i); 92 | int rank = card.getRank(); 93 | if (rank == 8) { 94 | sum -= 20; 95 | } else if (rank > 10) { 96 | sum -= 10; 97 | } else { 98 | sum -= rank; 99 | } 100 | } 101 | return sum; 102 | } 103 | 104 | /** 105 | * Displays the player's hand. 106 | */ 107 | public void display() { 108 | hand.display(); 109 | } 110 | 111 | /** 112 | * Displays the player's name and score. 113 | */ 114 | public void displayScore() { 115 | System.out.println(name + " has " + score() + " points"); 116 | } 117 | 118 | } 119 | -------------------------------------------------------------------------------- /ch14/Test.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Test code for Deck and Hand. 3 | */ 4 | public class Test { 5 | 6 | public static void main(String[] args) { 7 | Deck deck = new Deck("Deck"); 8 | deck.shuffle(); 9 | 10 | Hand hand = new Hand("Hand"); 11 | deck.deal(hand, 5); 12 | hand.display(); 13 | 14 | Hand drawPile = new Hand("Draw Pile"); 15 | deck.dealAll(drawPile); 16 | System.out.printf("Draw Pile has %d cards.\n", 17 | drawPile.size()); 18 | } 19 | 20 | } 21 | --------------------------------------------------------------------------------