├── .vscode └── .browse.VC.db ├── Chapter14 ├── UML.graffle ├── GameCenter.java ├── EightsDeck.java ├── EightsCard.java ├── Player.java ├── Card.java ├── EightsPlayer.java ├── EightsGenius.java ├── EightsCardCollection.java ├── CardCollection.java ├── Eights.java └── UML.svg ├── Chapter12 ├── 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 │ ├── l01.gif │ ├── l02.gif │ ├── l03.gif │ ├── l04.gif │ ├── shade.gif │ ├── 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 │ ├── 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 │ ├── config.txt │ └── COPYRIGHT ├── CardTable.java └── Card.java ├── Chapter6 ├── 6.1.java ├── 6.7.java ├── 6.3.java ├── 6.2.java ├── Multadd.java ├── 6.8.java ├── 6.6.java ├── 6.9.java └── 6.5.java ├── .gitignore ├── Chapter5 ├── 5.1.java ├── 5.3.java ├── 5.4.java ├── Buzz.java ├── 5.5.java ├── 5.2.java └── NumberGuessGame.java ├── Chapter4 ├── 4.2.java ├── 4.1.java └── Date.java ├── Chapter7 ├── 7.4.java ├── 7.3.java ├── 7.6.java ├── 7.1.java ├── 7.2.java └── 7.5.java ├── Chapter8 ├── 8.6.java ├── 8.8.java ├── 8.5.java ├── 8.1.java ├── 8.4.java ├── 8.7.java ├── 8.3.java └── 8.2.java ├── README.md ├── Chapter9 ├── 9.2.java ├── 9.5.java ├── 9.3.java ├── 9.6.java ├── 9.8.java ├── 9.7.java ├── Test.java └── Recurse.java ├── Chapter3 ├── Temperature.java ├── TimeConverter.java └── NumberGuessGame.java ├── Chapter2 ├── Date.java └── Time.java ├── Chapter11 ├── Date.java ├── Time.java ├── Tile.java ├── Rectangle.java.h └── Rational.java ├── Chapter10 ├── 10.5.java ├── 10.1.java ├── Big.java ├── 10.2.java └── 10.3.java ├── AppendixB ├── JapaneseFlag.java ├── MickeyMouse.java └── Moire.java ├── Chapter13 ├── Test.java ├── Card.java └── Deck.java └── LICENSE /.vscode/.browse.VC.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/.vscode/.browse.VC.db -------------------------------------------------------------------------------- /Chapter14/UML.graffle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter14/UML.graffle -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/01c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/01c.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/01d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/01d.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/01h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/01h.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/01s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/01s.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/02c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/02c.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/02d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/02d.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/02h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/02h.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/02s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/02s.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/03c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/03c.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/03d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/03d.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/03h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/03h.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/03s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/03s.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/04c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/04c.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/04d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/04d.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/04h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/04h.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/04s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/04s.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/05c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/05c.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/05d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/05d.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/05h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/05h.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/05s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/05s.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/06c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/06c.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/06d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/06d.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/06h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/06h.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/06s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/06s.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/07c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/07c.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/07d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/07d.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/07h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/07h.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/07s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/07s.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/08c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/08c.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/08d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/08d.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/08h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/08h.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/08s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/08s.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/09c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/09c.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/09d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/09d.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/09h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/09h.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/09s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/09s.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/10c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/10c.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/10d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/10d.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/10h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/10h.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/10s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/10s.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/11c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/11c.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/11d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/11d.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/11h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/11h.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/11s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/11s.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/12c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/12c.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/12d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/12d.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/12h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/12h.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/12s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/12s.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/13c.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/13c.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/13d.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/13d.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/13h.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/13h.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/13s.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/13s.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/l01.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/l01.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/l02.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/l02.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/l03.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/l03.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/l04.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/l04.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/shade.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/shade.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/back001.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/back001.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/back101.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/back101.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/back102.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/back102.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/back111.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/back111.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/back191.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/back191.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/back192.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/back192.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/bottom01.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/bottom01.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/bottom02.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/bottom02.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/bottom03.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/bottom03.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/bottom04.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/bottom04.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/bottom05.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/bottom05.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/bottom06.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/bottom06.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/bottom07.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/bottom07.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/shadow00.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/shadow00.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/shadow01.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/shadow01.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/shadow02.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/shadow02.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/shadow03.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/shadow03.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/shadow04.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/shadow04.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/shadow05.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/shadow05.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/shadow06.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/shadow06.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/shadow07.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/shadow07.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/shadow08.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/shadow08.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/shadow09.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/shadow09.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/shadow10.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/shadow10.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/shadow11.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/shadow11.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/shadow12.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/shadow12.gif -------------------------------------------------------------------------------- /Chapter12/cardset-oxymoron/shadow13.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ApolloZhu/Think-Java-Exercises/HEAD/Chapter12/cardset-oxymoron/shadow13.gif -------------------------------------------------------------------------------- /Chapter12/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 | -------------------------------------------------------------------------------- /Chapter6/6.1.java: -------------------------------------------------------------------------------- 1 | // 2 | // 6.1.java 3 | // Chapter6 4 | // 5 | // Created by Apollo Zhu on 7/28/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | /* 10 | 1. Nothign happens 11 | 2. not a statement 12 | */ -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /Chapter5/5.1.java: -------------------------------------------------------------------------------- 1 | // 2 | // 5.1.java 3 | // Chapter5 4 | // 5 | // Created by Apollo Zhu on 7/26/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | if (x > 0 && x < 10) { 10 | System.out.println("positive single digit number."); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter4/4.2.java: -------------------------------------------------------------------------------- 1 | // 2 | // 4.2.java 3 | // Chapter4 4 | // 5 | // Created by Apollo Zhu on 7/23/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public static void zool(int arg1, String arg2, String arg3){ 10 | // Your code goes here 11 | } 12 | 13 | zool(11, "GoGo", "Fang Cao"); -------------------------------------------------------------------------------- /Chapter7/7.4.java: -------------------------------------------------------------------------------- 1 | // 2 | // 7.4.java 3 | // Chapter7 4 | // 5 | // Created by Apollo Zhu on 7/29/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public static int factorial(int n){ 10 | int result = 1; 11 | for (int i=1;i<=n;i++){ 12 | result *=i; 13 | } 14 | return result; 15 | } -------------------------------------------------------------------------------- /Chapter7/7.3.java: -------------------------------------------------------------------------------- 1 | // 2 | // 7.3.java 3 | // Chapter7 4 | // 5 | // Created by Apollo Zhu on 7/29/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public static double power(double x, int n){ 10 | double result = 1; 11 | for (int i=0;i 0) { 11 | System.out.println(); 12 | nLines(n - 1); 13 | } 14 | } 15 | 16 | /* 17 | main | | 18 | nLines |[4]| 19 | nLines |[3]| 20 | nLines |[2]| 21 | nLines |[1]| 22 | nLines |[0]| 23 | */ -------------------------------------------------------------------------------- /Chapter6/6.7.java: -------------------------------------------------------------------------------- 1 | // 2 | // 6.7.java 3 | // Chapter6 4 | // 5 | // Created by Apollo Zhu on 7/28/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | /** 10 | @param n positive odd integer 11 | @return sum of odd integers form 1 to n 12 | @error StackoverflowError if n is not a positive odd integer 13 | */ 14 | public static int oddSum(int n){ 15 | return ((n==1)?(1):(n*oddSum(n-2))); 16 | } -------------------------------------------------------------------------------- /Chapter7/7.6.java: -------------------------------------------------------------------------------- 1 | // 2 | // 7.6.java 3 | // Chapter7 4 | // 5 | // Created by Apollo Zhu on 7/29/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public static double gauss(double x, int n){ 10 | double result = 1; 11 | int q = 1; 12 | double p = 1; 13 | int pre = 1; 14 | for (int i=1;i 2) && (Math.pow(a,n) + Math.pow(b,n) == Math.pow(c,n))){ 11 | System.out.println("Holy smokes, Fermat was wrong!"); 12 | } else { 13 | System.out.println("No, that doesn’t work."); 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter6/6.3.java: -------------------------------------------------------------------------------- 1 | // 2 | // 6.3.java 3 | // Chapter6 4 | // 5 | // Created by Apollo Zhu on 7/28/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | /** 10 | determine if given three interger is possible to be the three sides of a triangle 11 | @return true if can form a triangle 12 | */ 13 | public static boolean isTriangle(int s1, int s2, int s3){ 14 | return (s1 > 0 && s2 > 0 && s3 > 0 && s1 + s2 > s3 && s1 + s3 > s2 && s2 + s3 > s1); 15 | } -------------------------------------------------------------------------------- /Chapter6/6.2.java: -------------------------------------------------------------------------------- 1 | // 2 | // 6.2.java 3 | // Chapter6 4 | // 5 | // Created by Apollo Zhu on 7/28/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | /** 10 | A method to test if the first argument is divisible by the second argument 11 | @param n the integer to be divided 12 | @param m the integer to divide 13 | @ return true if m is not `0` and n is divisible by m 14 | */ 15 | public static boolean isDivisible(int n, int m){ 16 | return (m != 0 && n % m == 0); 17 | } -------------------------------------------------------------------------------- /Chapter12/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 | -------------------------------------------------------------------------------- /Chapter7/7.1.java: -------------------------------------------------------------------------------- 1 | // 2 | // 7.1.java 3 | // Chapter7 4 | // 5 | // Created by Apollo Zhu on 7/29/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | loop(10); 10 | public static void loop(int n){ 11 | int i = n; 12 | while (i > 1){ 13 | System.out.println(i); 14 | if (i % 2 == 0){ 15 | i = i/2; 16 | } else { 17 | i = i + 1; 18 | } 19 | } 20 | } 21 | 22 | /* 23 | 1. n: constantly 10 24 | i: 10 -> 5 -> 6 -> 3 -> 4 -> 2 -> 1 25 | 2. "10\n5\n6\n3\n4\n2\n" 26 | 3. ... 27 | */ -------------------------------------------------------------------------------- /Chapter7/7.2.java: -------------------------------------------------------------------------------- 1 | // 2 | // 7.2.java 3 | // Chapter7 4 | // 5 | // Created by Apollo Zhu on 7/29/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public static double squareRoot(double a){ 10 | double oldValue = a/2; 11 | double newValue = a/2; 12 | double interval = 0; 13 | do{ 14 | oldValue = newValue; 15 | newValue = (oldValue + a/oldValue)/2; 16 | interval = Math.abs(newValue - oldValue); 17 | System.out.println(interval); 18 | }while(interval > 0.001); 19 | return newValue; 20 | } -------------------------------------------------------------------------------- /Chapter9/9.2.java: -------------------------------------------------------------------------------- 1 | // 2 | // 9.2.java 3 | // Chapter9 4 | // 5 | // Created by Apollo Zhu on 8/11/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public static int[] letterHist(String str){ 10 | char[] characters = str.toUpperCase().toCharArray(); 11 | int[] result = new int[27]; 12 | for (char letter:characters){ 13 | if (letter >= 65 && letter <= 90){ 14 | result[letter-65]+=1; 15 | } 16 | else if (letter != 32){ 17 | result[27]+=1; 18 | } 19 | } 20 | return result; 21 | } -------------------------------------------------------------------------------- /Chapter3/Temperature.java: -------------------------------------------------------------------------------- 1 | // 2 | // Temperature.java 3 | // Chapter3 4 | // 5 | // Created by Apollo Zhu on 7/21/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | import java.util.Scanner; 10 | 11 | public class Temperature{ 12 | public static void main(String args[]){ 13 | Scanner console = new Scanner(System.in); 14 | System.out.print("Input temperatur in Celsius: "); 15 | double temperature = console.nextDouble(); 16 | double temperatureInF = temperature * 9 / 5 + 32; 17 | System.out.printf("%.1f C = %.1f F", temperature, temperatureInF); 18 | } 19 | } -------------------------------------------------------------------------------- /Chapter8/8.8.java: -------------------------------------------------------------------------------- 1 | // 2 | // 8.8.java 3 | // Chapter8 4 | // 5 | // Created by Apollo Zhu on 8/11/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public static int maxInRange(int[] integers, int lowIndex, int highIndex){ 10 | if (lowIndex == highIndex){ 11 | return integers[lowIndex]; 12 | } 13 | int mid = (lowIndex + highIndex) / 2; 14 | return Math.max(maxInRange(integers,lowIndex,mid),maxInRange(integers,(mid + 1),highIndex)); 15 | } 16 | 17 | public static int max(int[] integers){ 18 | return maxInRange(integers, 0, integers.length - 1); 19 | } -------------------------------------------------------------------------------- /Chapter9/9.5.java: -------------------------------------------------------------------------------- 1 | // 2 | // 9.5.java 3 | // Chapter9 4 | // 5 | // Created by Apollo Zhu on 8/11/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public static boolean isAbecedarian(String str){ 10 | String converted = str.toUpperCase(); 11 | for (int i = 0;i= 65 && c <= 90){ 14 | if (c > converted.charAt(i+1)){ 15 | return false; 16 | } 17 | } 18 | else { 19 | return false; 20 | } 21 | } 22 | return true; 23 | } -------------------------------------------------------------------------------- /Chapter9/9.3.java: -------------------------------------------------------------------------------- 1 | // 2 | // 9.3.java 3 | // Chapter9 4 | // 5 | // Created by Apollo Zhu on 8/11/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public static int diff(String str, char open, char close){ 10 | int count = 0; 11 | for (int i = 0; i players = new ArrayList(); 16 | players.add(new EightsPlayer("Random")); 17 | players.add(new EightsGenius("Genius")); 18 | players.add(new EightsPlayer("Random")); 19 | players.add(new EightsPlayer("Random")); 20 | Eights.test(players, count); 21 | } 22 | } -------------------------------------------------------------------------------- /Chapter6/Multadd.java: -------------------------------------------------------------------------------- 1 | // 2 | // Multadd.java 3 | // Chapter6 4 | // 5 | // Created by Apollo Zhu on 7/28/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public class Multadd{ 10 | public static void main(String args[]){ 11 | System.out.println(multadd(2.0,3.0,4.0)); 12 | System.out.println(multadd(0.5,Math.cos(Math.PI/4),Math.sin(Math.PI/4))); 13 | System.out.println(Math.log(10)+multadd(2,Math.log(2),Math.log(5))); 14 | } 15 | public static double multadd(double a, double b, double c){ 16 | return a*b+c; 17 | } 18 | public static double expSum(double x){ 19 | return (multadd(x,Math.exp(-x),Math.sqrt(1-Math.exp(-x)))); 20 | } 21 | } -------------------------------------------------------------------------------- /Chapter14/EightsDeck.java: -------------------------------------------------------------------------------- 1 | // 2 | // EightsDeck.java 3 | // Chapter14 4 | // 5 | // Created by Apollo Zhu on 8/20/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | /** 10 | * A deck of playing cards. 11 | */ 12 | public class EightsDeck extends EightsCardCollection { 13 | 14 | public EightsDeck(String label) { 15 | super(label); 16 | initialize(); 17 | } 18 | 19 | /** 20 | * Constructs a standard deck of 52 cards. 21 | */ 22 | private void initialize(){ 23 | for (int suit = 0; suit <= 3; suit++) { 24 | for (int rank = 1; rank <= 13; rank++) { 25 | append(new EightsCard(rank, suit)); 26 | } 27 | } 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Chapter3/TimeConverter.java: -------------------------------------------------------------------------------- 1 | // 2 | // TimeConverter.java 3 | // ProjectName 4 | // 5 | // Created by Apollo Zhu on 7/21/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | import java.util.Scanner; 10 | 11 | public class TimeConverter { 12 | public static void main(String args[]){ 13 | Scanner console = new Scanner(System.in); 14 | System.out.print("Input the time in seconds: "); 15 | int temp = console.nextInt(); 16 | int hour = temp / 3600; 17 | int seconds = temp % 3600; 18 | int minute = seconds / 60; 19 | seconds = seconds % 60; 20 | System.out.printf("%d second(s) = %d hour(s), %d minute(s), and %d second(s)", temp, hour, minute, seconds); 21 | } 22 | } -------------------------------------------------------------------------------- /Chapter6/6.8.java: -------------------------------------------------------------------------------- 1 | // 2 | // 6.8.java 3 | // Chapter6 4 | // 5 | // Created by Apollo Zhu on 7/28/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | /** 10 | Evaluate Ackermann function with two non-negative integers 11 | @return the value of the Ackermann function, or 233 if illegal paramter# 12 | @error StackOverflow if m is larger than 3 13 | */ 14 | public static int ack(int m, int n){ 15 | if (m==0){ 16 | return n+1; 17 | } 18 | else if (m > 0 && n == 0) { 19 | return ack(m-1,1); 20 | } 21 | else if (m > 0 && n > 0){ 22 | return ack(m-1,ack(m,n-1)); 23 | } 24 | else { 25 | return 233; 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /Chapter8/8.4.java: -------------------------------------------------------------------------------- 1 | // 2 | // 8.4.java 3 | // Chapter8 4 | // 5 | // Created by Apollo Zhu on 8/11/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public static int indexOfMax(int[] numbers){ 10 | int index = 0; 11 | for (int i=1;i numbers[index]){ 13 | index = i; 14 | } 15 | } 16 | return index; 17 | } 18 | 19 | public static int indexOfMaxWrittenInEnhancedForLoop(int[] numbers){ 20 | int index = 0; 21 | int i = 0; 22 | for (int number:numbers){ 23 | if (numbers[i] > numbers[index]){ 24 | index = i; 25 | } 26 | i++; 27 | } 28 | return index; 29 | } -------------------------------------------------------------------------------- /AppendixB/JapaneseFlag.java: -------------------------------------------------------------------------------- 1 | // 2 | // JapaneseFlag.java 3 | // AppendixB 4 | // 5 | // Created by Apollo Zhu on 8/14/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | import java.awt.*; 10 | import javax.swing.*; 11 | 12 | public class JapaneseFlag extends Canvas{ 13 | public static void main(String[] args){ 14 | JFrame frame = new JFrame("Window Title"); 15 | Canvas canvas = new JapaneseFlag(); 16 | canvas.setSize(800, 400); 17 | canvas.setBackground(Color.white); 18 | frame.add(canvas); 19 | frame.pack(); 20 | frame.setVisible(true); 21 | } 22 | public void paint(Graphics g){ 23 | // Drawing codes 24 | g.setColor(Color.red); 25 | g.fillOval(250,50,300,300); 26 | } 27 | } -------------------------------------------------------------------------------- /Chapter10/10.1.java: -------------------------------------------------------------------------------- 1 | // 2 | // 10.1.java 3 | // Chapter10 4 | // 5 | // Created by Apollo Zhu on 8/12/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | /* 10 | int x = 5; 11 | Point blank = new Point(1, 2); 12 | System.out.println(riddle(x, blank)); 13 | System.out.println(x); 14 | System.out.println(blank.x); 15 | System.out.println(blank.y); 16 | */ 17 | public static int riddle(int x, Point p){ 18 | x = x+7; 19 | //p.x += 2; 20 | return x+p.x+p.y; 21 | } 22 | 23 | /* 24 | 1. main.x [5] 25 | main.blank[] -> Point[x[1] y[2]] 26 | riddle.x [5] ^ 27 | riddle [x[5] p[]--------|] 28 | riddle [x[12] p[]-------|] 29 | 2. output: 15\n5\n1\n2\n 30 | 3. mutable 31 | */ -------------------------------------------------------------------------------- /Chapter10/Big.java: -------------------------------------------------------------------------------- 1 | // 2 | // Big.java 3 | // Chapter10 4 | // 5 | // Created by Apollo Zhu on 8/12/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | import java.math.BigInteger; 10 | 11 | public class Big{ 12 | public static void main(String args[]){ 13 | BigInteger integer = BigInteger.valueOf(2147483647); 14 | System.out.println(integer.add(BigInteger.ONE).pow(10)); 15 | for (int i=0;i<30;i++){ 16 | System.out.println(i+": "+factorial(i)); 17 | } 18 | } 19 | public static BigInteger factorial(int number){ 20 | BigInteger result = BigInteger.valueOf(1); 21 | for (int i = number;i>0;i--){ 22 | result = result.multiply(BigInteger.valueOf(i)); 23 | } 24 | return result; 25 | } 26 | // Imutable; According to documentation...; No Value to change 27 | } -------------------------------------------------------------------------------- /Chapter5/Buzz.java: -------------------------------------------------------------------------------- 1 | // 2 | // 5.6.java 3 | // ProjectName 4 | // 5 | // Created by Apollo Zhu on m/dd/yy. 6 | // Copyright © 2015-yyyy WWITDC. All rights reserved. 7 | // 8 | 9 | public class Buzz { 10 | public static void baffle(String blimp) { //8 11 | System.out.println(blimp); //9 12 | zippo("ping", -5); //10 13 | } 14 | public static void zippo(String quince, int flag) { //3, 11 15 | if (flag < 0) { //4, 12 16 | System.out.println(quince + " zoop"); //13 17 | } 18 | else { //5 19 | System.out.println("ik"); //6 20 | baffle(quince); //7 21 | System.out.println("boo-wa-ha-ha"); //14 22 | } 23 | } 24 | public static void main(String[] args) { //1 25 | zippo("rattle", 13); //2 26 | } 27 | } 28 | 29 | /* 30 | 3. "rattle" 31 | 4. "ik\nrattle\nping zoop\nboo-wa-ha-ha\n" 32 | */ -------------------------------------------------------------------------------- /Chapter6/6.6.java: -------------------------------------------------------------------------------- 1 | // 2 | // 6.6.java 3 | // Chapter6 4 | // 5 | // Created by Apollo Zhu on 7/28/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | prod(1,4); 10 | 11 | /** 12 | returns so-called interval factorial value 13 | @param m an interger 14 | @param n an integer not less than m 15 | @error StackOverflowError if m > n 16 | @return m*(m+1)*...*n 17 | */ 18 | public static int prod(int m, int n) { 19 | /*if (m == n) { 20 | return n; 21 | } else { 22 | int recurse = prod(m, n - 1); 23 | int result = n * recurse; 24 | return result; 25 | }*/ 26 | return ((m==n)?(n):(n*prod(m, n - 1))); 27 | } 28 | 29 | /* 30 | 1. main|| 31 | prod|m[1] n[4] recurse[6] result[24]| -> 24 32 | prod|m[1] n[3] recurse[2] result[6]| 33 | prod|m[1] n[2] recurse[1] result[2]| 34 | prod|m[1] n[1]| -> 1 35 | */ 36 | -------------------------------------------------------------------------------- /Chapter9/9.6.java: -------------------------------------------------------------------------------- 1 | // 2 | // 9.6.java 3 | // Chapter9 4 | // 5 | // Created by Apollo Zhu on 8/11/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | // 9.2 10 | public static int[] letterHist(String str){ 11 | char[] characters = str.toUpperCase().toCharArray(); 12 | int[] result = new int[27]; 13 | for (char letter:characters){ 14 | if (letter >= 65 && letter <= 90){ 15 | result[letter-65]+=1; 16 | } 17 | else if (letter != 32){ 18 | result[27]+=1; 19 | } 20 | } 21 | return result; 22 | } 23 | 24 | public static boolean isDoubloon(String str){ 25 | if (str.length() == 0) { 26 | return false;} 27 | for (int count:letterHist(str)){ 28 | if (count != 0 && count != 2){ 29 | return false; 30 | } 31 | } 32 | return true; 33 | } -------------------------------------------------------------------------------- /Chapter4/4.1.java: -------------------------------------------------------------------------------- 1 | // 2 | // 4.1 3 | // Chpater4 4 | // 5 | // Created by Apollo Zhu on 7/23/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public static void zoop() { 10 | baffle(); 11 | System.out.print("You wugga "); 12 | baffle(); 13 | } 14 | 15 | public static void main(String[] args) { 16 | System.out.print("No, I "); 17 | zoop(); 18 | System.out.print("I "); 19 | baffle(); 20 | } 21 | 22 | public static void baffle() { 23 | System.out.print("wug"); 24 | ping(); 25 | } 26 | 27 | public static void ping() { 28 | System.out.println("."); 29 | } 30 | 31 | /* 32 | 1. No, I wug.You wugga wug.I wug.\n 33 | 16 .232811 .2328182328 34 | 35 | 2. | main [ ] | at: 17 36 | | zoop [ ] | at: 10 37 | | baffle [ ] | at: 24 38 | | ping [ ] | at: 27 39 | 40 | 3. baffle -> ping -> baffle -> ping ... (Non-escape) -------------------------------------------------------------------------------- /Chapter4/Date.java: -------------------------------------------------------------------------------- 1 | // 2 | // Date.java 3 | // ThinkJava-Chapter2 4 | // 5 | // Created by Apollo Zhu on 7/23/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public class Date{ 10 | 11 | public static void printAmerican(String day, int date, String month, int year){ 12 | System.out.println(day + ", " + month + " " + date + ", " + year); 13 | } 14 | 15 | public static void printEuropean(String day, int date, String month, int year){ 16 | System.out.println(day + " " + date + " " + month + " " + year); 17 | } 18 | 19 | public static void main(String args[]){ 20 | 21 | String day = "Saturday", month = "July"; 22 | int date = 23, year = 2016; 23 | System.out.println("American format:"); 24 | printAmerican(day, date, month, year); 25 | System.out.println("European format:"); 26 | printEuropean(day, date, month, year); 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /Chapter5/5.5.java: -------------------------------------------------------------------------------- 1 | // 2 | // 5.5.java 3 | // Chapter5 4 | // 5 | // Created by Apollo Zhu on 7/26/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public static void lyricsFor(int bearNumber){ 10 | if (bearNumber > 0){ 11 | System.out.println(bearNumber + " bottles of beer on the wall,"); 12 | System.out.println(bearNumber + " bottles of beer,"); 13 | System.out.println("ya' take one down, ya' pass it around,"); 14 | System.out.println(bearNumber-- + "bottles of beer on the wall."); 15 | lyricsFor(bearNumber); 16 | } 17 | else { 18 | System.out.println("No bottles of beer on the wall,"); 19 | System.out.println("no bottles of beer,"); 20 | System.out.println("ya' can't take one down, ya' can't pass it around,"); 21 | System.out.println("'cause there are no more bottles of beer on the wall!"); 22 | } 23 | } -------------------------------------------------------------------------------- /Chapter9/9.8.java: -------------------------------------------------------------------------------- 1 | // 2 | // 9.8.java 3 | // Cahpter9 4 | // 5 | // Created by Apollo Zhu on 8/11/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public static int[] letterHist(String str){ 10 | char[] characters = str.toUpperCase().toCharArray(); 11 | int[] result = new int[27]; 12 | for (char letter:characters){ 13 | if (letter >= 65 && letter <= 90){ 14 | result[letter-65]+=1; 15 | } 16 | else if (letter != 32){ 17 | result[27]+=1; 18 | } 19 | } 20 | return result; 21 | } 22 | 23 | public static boolean canSpell(String word, String libchar){ 24 | int[] wordRequirment = letterHist(word); 25 | int[] libCapacity = letterHist(libchar); 26 | for (int i=0;i<26;i++){ 27 | if (wordRequirment[i] > libCapacity[i]){ 28 | return false; 29 | } 30 | } 31 | return true; 32 | } -------------------------------------------------------------------------------- /Chapter3/NumberGuessGame.java: -------------------------------------------------------------------------------- 1 | // 2 | // NumberGuessGame.java 3 | // Chapter3 4 | // 5 | // Created by Apollo Zhu on 7/21/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | import java.util.Scanner; 10 | import java.util.Random; 11 | 12 | public class NumberGuessGame{ 13 | public static void main(String args[]){ 14 | Scanner console = new Scanner(System.in); 15 | System.out.println("I'm thinking of a number between 1 and 100"); 16 | System.out.println("(including both). Can you guess what it is?"); 17 | System.out.print("Type a number: "); 18 | 19 | int guess = console.nextInt(); 20 | Random random = new Random(); 21 | int answer = random.nextInt(100) + 1; 22 | 23 | System.out.printf("Your guess is: %d\n", guess); 24 | System.out.printf("The number I was thinking of is: %d\n", answer); 25 | System.out.printf("You were off by: %d\n", Math.abs(guess - answer)); 26 | } 27 | } -------------------------------------------------------------------------------- /Chapter6/6.9.java: -------------------------------------------------------------------------------- 1 | // 2 | // 6.9.java 3 | // Chapter6 4 | // 5 | // Created by Apollo Zhu on 7/28/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | /** 10 | A recursive method to evaluate power 11 | @param x base 12 | @param n power which is a positive integer 13 | @return 233 if illegal, otherwise x^n 14 | */ 15 | public static double power(double x, int n){ 16 | if (x == 0){ 17 | if (n == 0){ 18 | return 233.3; 19 | } 20 | else { 21 | return 0; 22 | } 23 | } 24 | else { 25 | if (n == 1){ 26 | return x; 27 | } 28 | else if (n == 2){ 29 | return x*x; 30 | } 31 | else { 32 | if (n % 2 == 0){ 33 | return power(power(x,n/2),2); 34 | } 35 | else { 36 | return x*power(x,n-1); 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Chapter11/Time.java: -------------------------------------------------------------------------------- 1 | // 2 | // Time.java 3 | // Chapter11 4 | // 5 | // Created by Apollo Zhu on 8/13/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public class Time{ 10 | 11 | public static void main(String[] args){ 12 | Time time = new Time(); 13 | time.increment(3600*8+6*60+5); 14 | System.out.println(time); 15 | } 16 | 17 | public int hour; 18 | public int minute; 19 | public double second; 20 | public Time(int hour, int minute, double second){ 21 | this.hour = hour; 22 | this.minute = minute; 23 | this.second = second; 24 | } 25 | public Time(){ 26 | this(0,0,0); 27 | } 28 | public String toString(){ 29 | return this.hour+":"+this.minute+":"+this.second; 30 | } 31 | public void increment(double seconds) { 32 | this.second += seconds; 33 | this.minute += this.second / 60; 34 | this.second %= 60; 35 | this.hour += this.minute / 60; 36 | this.minute %= 60; 37 | } 38 | } -------------------------------------------------------------------------------- /Chapter9/9.7.java: -------------------------------------------------------------------------------- 1 | // 2 | // 9.7.java 3 | // Chapter9 4 | // 5 | // Created by Apollo Zhu on 8/11/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | // 9.2 10 | public static int[] letterHist(String str){ 11 | char[] characters = str.toUpperCase().toCharArray(); 12 | int[] result = new int[27]; 13 | for (char letter:characters){ 14 | if (letter >= 65 && letter <= 90){ 15 | result[letter-65]+=1; 16 | } 17 | else if (letter != 32){ 18 | result[27]+=1; 19 | } 20 | } 21 | return result; 22 | } 23 | 24 | public static boolean isAnagram(String str1, String str2){ 25 | int[] result1 = letterHist(str1); 26 | int[] result2 = letterHist(str2); 27 | if (result1[26] != 0 || result2[26] != 0) { 28 | return false;} 29 | for (int i=0;i<26;i++){ 30 | if (result1[i] != result2[i]){ 31 | return false; 32 | } 33 | } 34 | return true; 35 | } -------------------------------------------------------------------------------- /Chapter8/8.7.java: -------------------------------------------------------------------------------- 1 | // 2 | // 8.7.java 3 | // Chapter8 4 | // 5 | // Created by Apollo Zhu on 8/11/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public static boolean isPrime(int n){ 10 | if (n < 2){ 11 | return false; 12 | } 13 | else if (n > 2){ 14 | if (n % 2 == 0){ 15 | return false; 16 | } 17 | for (int i = 3;i<=(int)Math.sqrt(n);i+=2){ 18 | if (n % i !=0){ 19 | return false; 20 | } 21 | } 22 | 23 | } 24 | return true; 25 | } 26 | 27 | public static boolean arePrimeFactors(int n, int[] arr){ 28 | int result = 1; 29 | for (int element:arr){ 30 | if (isPrime(element)){ 31 | result *= element; 32 | } 33 | else { 34 | return false; 35 | } 36 | } 37 | if (result == n){ 38 | return true; 39 | } 40 | else { 41 | return false; 42 | } 43 | } -------------------------------------------------------------------------------- /Chapter7/7.5.java: -------------------------------------------------------------------------------- 1 | // 2 | // 7.5.java 3 | // Chapter7 4 | // 5 | // Created by Apollo Zhu on 7/29/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | for (double i=0.1; i<=100; i*=10){ 10 | check(i); // 1+14 -> 1+15 -> 5+1 -> ? 11 | } 12 | // for (double i=-0.1; i>=-100; i*=10){ 13 | // check(i); // 1+15 -> 1+15 -> ? -> ? 14 | // } 15 | 16 | public static double myexp(double x, int n){ 17 | double result = 1; 18 | double p = 1; 19 | double q = 1; 20 | for (int i=1;i 0.0000000000000001); 35 | System.out.println(x+"\t"+myexp(x, i)+"\t"+Math.exp(x)); 36 | } -------------------------------------------------------------------------------- /Chapter8/8.3.java: -------------------------------------------------------------------------------- 1 | // 2 | // 8.3.java 3 | // Cahpter8 4 | // 5 | // Created by Apollo Zhu on 8/11/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | int[] bob = make(5); 10 | dub(bob); 11 | mus(bob) 12 | 13 | public static int[] make(int n){ 14 | int[] a = new int[n]; 15 | for (int i = 0;i make |n [5] a[]| -> [1|2|3|4|5] 39 | bob [] -> dub |jub [bob[]]| -> [2|4|6|8|10] 40 | mus |zoo [bob[]] fus [30]| 41 | 42 | output: 30 43 | */ -------------------------------------------------------------------------------- /Chapter10/10.2.java: -------------------------------------------------------------------------------- 1 | // 2 | // 10.2.java 3 | // Chapter10 4 | // 5 | // Created by Apollo Zhu on 8/12/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | /* 10 | Point blank = new Point(5,8); 11 | Rectangle rect = new Rectangle(0,2,4,4); 12 | Point center = findCenter(rect); 13 | double dist = distance(center, blank); 14 | System.out.println(dist); 15 | */ 16 | 17 | public static double distance(Point p1, Point p2){ 18 | int dx = p2.x - p1.x; 19 | int dy = p2.y - p1.y; 20 | return Math.sqrt(dx*dx+dy*dy); 21 | } 22 | 23 | public static Point findCenter(Rectangle box){ 24 | int x = box.x + box.width / 2; 25 | int y = box.y + box.height / 2; 26 | return new Point(x, y); 27 | } 28 | /* 29 | 1. main.blank[] -> Point[x[5] y[8]] 30 | main.rect[] -> Rect [x[0] y[2] w[4] h[4]] 31 | main.center[] -> findCenter[rect[]-^ x[1] y[3]] -> Point[x[1] y[3]] 32 | main.dist[distance[p1[]->center p2[]->blank dx[4] dy[3]] 33 | 2. output: 5.0 34 | */ -------------------------------------------------------------------------------- /Chapter14/EightsCard.java: -------------------------------------------------------------------------------- 1 | // 2 | // EightsCard.java 3 | // Chapter14 4 | // 5 | // Created by Apollo Zhu on 8/20/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public class EightsCard extends Card{ 10 | 11 | public EightsCard(int rank, int suit) { 12 | super(rank, suit); 13 | } 14 | 15 | /** 16 | * Returns `true` if matches `that` with rule of crazy eights 17 | * Otherwise `false` 18 | */ 19 | public boolean matches(EightsCard that){ 20 | if (getSuit() == that.getSuit()) { 21 | return true; 22 | } 23 | if (getRank() == that.getRank()) { 24 | return true; 25 | } 26 | if (getRank() == 8) { 27 | return true; 28 | } 29 | return false; 30 | } 31 | 32 | /** 33 | * Returns the score of the card 34 | */ 35 | public int score(){ 36 | int rank = getRank(); 37 | if (rank == 8) { 38 | return -20; 39 | } 40 | else if (rank > 10) { 41 | return -10; 42 | } 43 | else { 44 | return -rank; 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /Chapter10/10.3.java: -------------------------------------------------------------------------------- 1 | // 2 | // 10.3.java 3 | // Chapter10 4 | // 5 | // Created by Apollo Zhu on 8/12/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | Rectangle box1 = new Rectangle(2,4,7,9); 10 | Point p1 = findCenter(box1); 11 | printPoint(p1); 12 | box1.grow(1, 1); 13 | Point p2 = findCenter(box1); 14 | printPoint(p2); 15 | 16 | /* 17 | p2.x += 1; 18 | printPoint(p1); 19 | printPoint(p2); 20 | */ 21 | 22 | public static void printPoint(Point p){ 23 | System.out.println("("+p.x+", "+p.y+")"); 24 | } 25 | 26 | public static Point findCenter(Rectangle box){ 27 | int x = box.x + box.width / 2; 28 | int y = box.y + box.height / 2; 29 | return new Point(x,y); 30 | } 31 | 32 | /* 33 | 1. box1[] -> Rectangle[x[2] y[4] w[7] h[9]] 34 | p1[] -> findCenter[box[]->box1 x[5] y[8]] -> Point[x[5] y[8]] 35 | box1[] -> Rectangle[x[0] y[2] w[9] h[11]] 36 | p2[] -> findCenter[box[]->box1 x[5] y[8]] -> Point[x[5] y[8]] 37 | 2. output (5, 8)\n(5, 8)\n 38 | 3. Not. `new` creates a new object 39 | */ -------------------------------------------------------------------------------- /Chapter6/6.5.java: -------------------------------------------------------------------------------- 1 | // 2 | // 6.5.java 3 | // Chapter6 4 | // 5 | // Created by Apollo Zhu on 7/28/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public static void main(String[] args) { 10 | boolean flag1 = isHoopy(202); 11 | //true 12 | boolean flag2 = isFrabjuous(202); 13 | //true 14 | System.out.println(flag1); 15 | //true\n 16 | System.out.println(flag2); //true\n 17 | if (flag1 && flag2) { 18 | System.out.println("ping!"); //ping!\n 19 | } 20 | if (flag1 || flag2) { 21 | 22 | System.out.println("pong!"); //pong!\n 23 | } 24 | } 25 | public static boolean isHoopy(int x) { 26 | // AZMark: Int.isEven 27 | boolean hoopyFlag; 28 | if (x % 2 == 0) { 29 | hoopyFlag = true; 30 | } else { 31 | hoopyFlag = false; 32 | } 33 | return hoopyFlag; 34 | } 35 | public static boolean isFrabjuous(int x) { 36 | // AZMark: Int.isPositive 37 | boolean frabjuousFlag; 38 | if (x > 0) { 39 | frabjuousFlag = true; 40 | } else { 41 | frabjuousFlag = false; 42 | } 43 | return frabjuousFlag; 44 | } 45 | 46 | /* 47 | true\ntrue\nping!\npong!\n 48 | */ -------------------------------------------------------------------------------- /Chapter5/5.2.java: -------------------------------------------------------------------------------- 1 | // 2 | // 5.2.java 3 | // Chapter5 4 | // 5 | // Created by Apollo Zhu on 7/26/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public static void zoop(String fred, int bob) { 10 | System.out.println(fred); 11 | if (bob == 5) { 12 | ping("not "); 13 | } else { 14 | System.out.println("!"); 15 | } 16 | } 17 | public static void main(String[] args) { 18 | int bizz = 5; 19 | int buzz = 2; 20 | zoop("just for", bizz); 21 | clink(2 * buzz); 22 | } 23 | public static void clink(int fork) { 24 | System.out.print("It's "); 25 | zoop("breakfast ", fork) ; 26 | } 27 | public static void ping(String strangStrung) { 28 | System.out.println("any " + strangStrung + "more "); 29 | } 30 | 31 | /* 32 | 1. Error: Unexpected found EOF while processing stack diagram: 33 | main | | 34 | zoop |[just for][5] | 35 | ping |[not ] | 36 | clink |[4] | 37 | zoop |[breakfast ][4]| 38 | 39 | 2. "just for\nany not more \nIt's breakfast \n!" 40 | 41 | just for 42 | any not more 43 | It's breakfast 44 | ! 45 | 46 | */ -------------------------------------------------------------------------------- /Chapter13/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 | //deck.print(); 29 | checkSorted(deck); 30 | 31 | System.out.println("Testing mergesort..."); 32 | deck = new Deck(); 33 | deck.shuffle(); 34 | deck = deck.mergeSort(); 35 | checkSorted(deck); 36 | 37 | System.out.println("Testing insertion..."); 38 | deck = new Deck(); 39 | deck.shuffle(); 40 | deck.insertionSort(); 41 | checkSorted(deck); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Chapter9/Test.java: -------------------------------------------------------------------------------- 1 | // 2 | // Test.java /*9.1*/ 3 | // Chapter9 4 | // 5 | // Created by Apollo Zhu on 8/11/16. 6 | // Copyright © 2015-yyyy WWITDC. All rights reserved. 7 | // 8 | 9 | public class Test{ 10 | public static void main(String args[]){ 11 | /* 12 | System.out.println("1"+"2"); 13 | 14 | // Unavoidable: () 15 | // arbitrary: [] 16 | // Problematic: {} 17 | bool char int double str 18 | bool {0} (0) [0] (0) (cat) 19 | char (0) {add add add} (cat) 20 | int [0] {add} (add add cat) 21 | double (0) {add} (add add cat) 22 | str (cat cat cat cat cat) 23 | */ 24 | 25 | /* 26 | char x = 127; 27 | x=x+1; 28 | throw -> possible lossy conversion from int to char 29 | x -> 65 -> 66 -> Character(66) // might exceed ASCII 30 | */ 31 | 32 | /* 33 | boolean temp = "" + true; 34 | make the other type a String 35 | */ 36 | 37 | // boolean< answer ? "high" : "low")); 39 | System.out.println("Try again\n"); 40 | guess(answer); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /Chapter8/8.2.java: -------------------------------------------------------------------------------- 1 | // 2 | // 8.2.java 3 | // Chapter8 4 | // 5 | // Created by Apollo Zhu on 8/10/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | /** 10 | * Calculates the result of multiplying all elements in `a` 11 | * reduce(1) {$0 * $1} 12 | */ 13 | public static int banana(int[] a){ 14 | // Storing result of calculation 15 | int kiwi = 1; 16 | // Storing index to travserse `a` 17 | int i = 0; 18 | while (i < a.length){ 19 | kiwi = kiwi * a[i]; 20 | i++; 21 | } 22 | return kiwi; 23 | } 24 | 25 | /** 26 | * Finds the index of first `grape` in `a` 27 | * indexOf(`grape`) 28 | * @return Index of `grape`; -1 if not found 29 | */ 30 | public static int grapefruit(int[]a, int grape){ 31 | for (int i=0;i= 3; 36 | Rectangle half = new Rectangle(bb.x, bb.y, dx, dy); 37 | half.translate(-dx / 2, -dy / 2); 38 | boxOval(g, half); 39 | if (needsSubEar){ 40 | mickey(g,half); 41 | } 42 | half.translate(dx * 2, 0); 43 | boxOval(g, half); 44 | if (needsSubEar){ 45 | mickey(g,half); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /Chapter9/Recurse.java: -------------------------------------------------------------------------------- 1 | // 2 | // Recurse.java 3 | // Chapter9 4 | // 5 | // Created by Apollo Zhu on 8/11/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public class Recurse{ 10 | public static void main(String args[]){ 11 | System.out.println(isPalindrome("otto")); 12 | } 13 | 14 | public static void printString(String str){ 15 | if (length(str) != 0){ 16 | System.out.println(first(str)); 17 | printString(rest(str)); 18 | } 19 | } 20 | 21 | public static void printBackward(String str){ 22 | if (length(str) != 0){ 23 | printBackward(rest(str)); 24 | System.out.println(first(str)); 25 | } 26 | } 27 | 28 | public static String reverseString(String str){ 29 | if (length(str) != 0){ 30 | return reverseString(rest(str)) + first(str); 31 | } 32 | return ""; 33 | } 34 | 35 | public static boolean isPalindrome(String str){ 36 | int length = length(str); 37 | if (length == 1){ 38 | return true; 39 | } 40 | else if (length == 2){ 41 | return first(str) == first(rest(str)); 42 | } 43 | else { 44 | return first(str) == first(reverseString(rest(str))) && isPalindrome(middle(str)); 45 | } 46 | } 47 | // MARK: Supporting 48 | 49 | /** 50 | * Returns the first character of the given String. 51 | */ 52 | public static char first(String s){ 53 | return s.charAt(0); 54 | } 55 | 56 | /** 57 | * Returns all but the first letter of the given String. 58 | */ 59 | public static String rest(String s) { 60 | return s.substring(1); 61 | } 62 | 63 | /** 64 | * Returns all but the first and last letter of the String. 65 | */ 66 | public static String middle(String s) { 67 | return s.substring(1, s.length() - 1); 68 | } 69 | 70 | /** 71 | * Returns the length of the given String. 72 | */ 73 | public static int length(String s) { 74 | return s.length(); 75 | } 76 | } -------------------------------------------------------------------------------- /Chapter11/Rectangle.java.h: -------------------------------------------------------------------------------- 1 | // 2 | // Rectangle.java.h 3 | // Chapter11 4 | // 5 | // Created by Apollo Zhu on 8/13/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public class Rectangle extends Rectangle2D implements Shape, java.io.Serializable{ 10 | 11 | //?_? 12 | private static native void initIDs(); 13 | 14 | // Pure 15 | public double getX() 16 | public double getY() 17 | public double getWidth() 18 | public double getHeight() 19 | public Rectangle getBounds() 20 | public Rectangle2D getBounds2D() 21 | private static int clip(double v, boolean doceil) 22 | public Point getLocation() 23 | public Dimension getSize() 24 | public boolean contains(Point p) 25 | public boolean contains(int x, int y) 26 | public boolean contains(Rectangle r) 27 | public boolean contains(int X, int Y, int W, int H) 28 | @Deprecated public boolean inside(int X, int Y) 29 | public boolean intersects(Rectangle r) 30 | public Rectangle intersection(Rectangle r) 31 | public Rectangle union(Rectangle r) 32 | public boolean isEmpty() 33 | public int outcode(double x, double y) 34 | public Rectangle2D createIntersection(Rectangle2D r) 35 | public Rectangle2D createUnion(Rectangle2D r) 36 | public boolean equals(Object obj) 37 | public String toString() 38 | 39 | // Modifier 40 | public void setBounds(Rectangle r) 41 | public void setBounds(int x, int y, int width, int height) 42 | public void setRect(double x, double y, double width, double height) 43 | @Deprecated public void reshape(int x, int y, int width, int height) 44 | public void setLocation(Point p) 45 | public void setLocation(int x, int y) 46 | @Deprecated public void move(int x, int y) 47 | public void translate(int dx, int dy) 48 | public void setSize(Dimension d) 49 | public void setSize(int width, int height) 50 | @Deprecated public void resize(int width, int height) 51 | public void add(int newx, int newy) 52 | public void add(Point pt) 53 | public void add(Rectangle r) 54 | public void grow(int h, int v) 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Chapter14/Card.java: -------------------------------------------------------------------------------- 1 | // 2 | // Card.java 3 | // Chapter14 4 | // 5 | // Created by Allen Downey and Chris Mayfield 6 | // Modified by Apollo Zhu on 8/20/16. 7 | // 8 | 9 | /** 10 | * A standard playing card. 11 | */ 12 | public class Card { 13 | 14 | public static final String[] RANKS = { 15 | null, "Ace", "2", "3", "4", "5", "6", "7", 16 | "8", "9", "10", "Jack", "Queen", "King"}; 17 | 18 | public static final String[] SUITS = { 19 | "Clubs", "Diamonds", "Hearts", "Spades"}; 20 | 21 | private final int rank; 22 | private final int suit; 23 | 24 | /** 25 | * Constructs a card of the given rank and suit. 26 | */ 27 | public Card(int rank, int suit) { 28 | this.rank = rank; 29 | this.suit = suit; 30 | } 31 | 32 | /** 33 | * Returns a negative integer if this card comes before 34 | * the given card, zero if the two cards are equal, or 35 | * a positive integer if this card comes after the card. 36 | */ 37 | public int compareTo(Card that) { 38 | if (suit < that.suit) { 39 | return -1; 40 | } 41 | if (suit > that.suit) { 42 | return 1; 43 | } 44 | if (rank < that.rank) { 45 | return -1; 46 | } 47 | if (rank > that.rank) { 48 | return 1; 49 | } 50 | return 0; 51 | } 52 | 53 | /** 54 | * Returns true if the given card has the same 55 | * rank AND same suit; otherwise returns false. 56 | */ 57 | public boolean equals(Card that) { 58 | return rank == that.rank 59 | && suit == that.suit; 60 | } 61 | 62 | /** 63 | * Gets the card's rank. 64 | */ 65 | public int getRank() { 66 | return rank; 67 | } 68 | 69 | /** 70 | * Gets the card's suit. 71 | */ 72 | public int getSuit() { 73 | return suit; 74 | } 75 | 76 | /** 77 | * Returns the score of the card 78 | */ 79 | public int score(){ 80 | return rank; 81 | } 82 | 83 | /** 84 | * Returns a string representation of the card. 85 | */ 86 | public String toString() { 87 | return RANKS[rank] + " of " + SUITS[suit]; 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /Chapter13/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 | -------------------------------------------------------------------------------- /Chapter14/EightsPlayer.java: -------------------------------------------------------------------------------- 1 | // 2 | // EightsPlayer.java 3 | // Chapter14 4 | // 5 | // Created by Apollo Zhu on 8/20/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public class EightsPlayer extends Player{ 10 | 11 | private EightsCardCollection eightsHand; 12 | 13 | public EightsPlayer(String name){ 14 | super(name); 15 | eightsHand = new EightsCardCollection(name); 16 | } 17 | 18 | /** 19 | * Gets the player's hand. 20 | */ 21 | public EightsCardCollection getHand(){ 22 | return eightsHand; 23 | } 24 | 25 | /** 26 | * Calculates the player's score (penalty points). 27 | */ 28 | public int score() { 29 | int sum = 0; 30 | for (int i = 0; i < getHand().size(); i++) { 31 | sum += getHand().getCardAt(i).score(); 32 | } 33 | return sum; 34 | } 35 | 36 | /** 37 | * Displays the player's hand. 38 | */ 39 | public void display() { 40 | getHand().display(); 41 | } 42 | 43 | /** 44 | * Displays the player's name and score. 45 | */ 46 | public void displayScore() { 47 | System.out.println(getName() + " has " + score() + " points"); 48 | } 49 | 50 | /** 51 | * Removes and returns a legal card from the player's hand. 52 | */ 53 | public EightsCard play(Eights eights, EightsCard prev) { 54 | EightsCard card = searchForMatch(prev); 55 | if (card == null) { 56 | card = drawForMatch(eights, prev); 57 | } 58 | return card; 59 | } 60 | 61 | /** 62 | * Searches the player's hand for a matching card. 63 | */ 64 | public EightsCard searchForMatch(EightsCard prev) { 65 | for (int i = 0; i < getHand().size(); i++) { 66 | EightsCard card = getHand().getCardAt(i); 67 | if (card.matches(prev)) { 68 | return getHand().removeAt(i); 69 | } 70 | } 71 | return null; 72 | } 73 | 74 | /** 75 | * Draws cards until a match is found. 76 | */ 77 | public EightsCard drawForMatch(Eights eights, EightsCard prev) { 78 | while (true) { 79 | EightsCard card = eights.draw(); 80 | System.out.println(getName() + " draws " + card); 81 | if (card.matches(prev)) { 82 | return card; 83 | } 84 | getHand().append(card); 85 | } 86 | } 87 | } -------------------------------------------------------------------------------- /AppendixB/Moire.java: -------------------------------------------------------------------------------- 1 | import java.awt.Canvas; 2 | import java.awt.Color; 3 | import java.awt.Graphics; 4 | import java.awt.*; 5 | import javax.swing.JFrame; 6 | 7 | public class Moire extends Canvas { 8 | 9 | public static void main(String[] args) { 10 | JFrame frame = new JFrame("Moire Pattern"); 11 | Canvas canvas = new Moire(); 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 | circularAtOrigin(g, 90, 3); 21 | circularAtCenter(g, 90, 3); 22 | radial(g, 0, 0.5); 23 | } 24 | 25 | public static double cot(double radian){ 26 | return 1 / Math.tan(radian); 27 | } 28 | 29 | public void circularAtOrigin(Graphics g, int start, int interval){ 30 | int i = start; 31 | while (i < Math.min(getWidth(), getHeight())) { 32 | g.drawOval (0, 0, i, i); 33 | i += interval; 34 | } 35 | } 36 | 37 | public void circularAtCenter(Graphics g, int start, int interval){ 38 | int i = start; 39 | while (i < Math.min(getWidth(), getHeight())) { 40 | int originX = (getWidth() - i) / 2; 41 | int originY = (getHeight() - i) / 2; 42 | g.drawOval (originX, originY, i, i); 43 | i += interval; 44 | } 45 | } 46 | 47 | public void radial(Graphics g, double startAngle, double interval){ 48 | double theta = startAngle; 49 | while (theta <= 180){ 50 | if (theta > 45 && theta < 135){ 51 | int x1 = (int)((1 - cot(Math.toRadians(theta))) * getWidth() / 2); 52 | int y1 = getHeight(); 53 | int x2 = (int)((1 + cot(Math.toRadians(theta))) * getWidth() / 2); 54 | int y2 = 0; 55 | g.drawLine(x1,y1,x2,y2); 56 | } 57 | else { 58 | int x1 = 0; 59 | int y1 = (int)((1 + Math.tan(Math.toRadians(theta))) * getHeight() / 2); 60 | int x2 = getWidth(); 61 | int y2 = (int)((1 - Math.tan(Math.toRadians(theta))) * getHeight() / 2); 62 | g.drawLine(x1,y1,x2,y2); 63 | 64 | } 65 | theta += interval; 66 | } 67 | } 68 | 69 | } 70 | 71 | /* 72 | 1. several circles that in squares all started at (0, 0) 73 | min width to 90 and max to 399 74 | */ 75 | -------------------------------------------------------------------------------- /Chapter14/EightsGenius.java: -------------------------------------------------------------------------------- 1 | // 2 | // EightsGenius.java 3 | // Chapter14 4 | // 5 | // Created by Apollo Zhu on 8/19/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | import java.util.ArrayList; 10 | 11 | public class EightsGenius extends EightsPlayer{ 12 | 13 | public EightsGenius(String name){ 14 | super(name); 15 | } 16 | /** 17 | * Evaluate value of certain card under certain condition 18 | */ 19 | private static int eval(EightsCard card, int total, boolean vary){ 20 | return eval(card.getRank(), total, vary); 21 | } 22 | 23 | /** 24 | * Evaluate value of certain card under certain condition 25 | */ 26 | private static int eval(int rank, int total, boolean vary){ 27 | if (rank > 10){ 28 | return 10; 29 | } 30 | if (rank == 8){ 31 | if (total < 5 && !vary){ 32 | return 12; 33 | } 34 | return 0; 35 | } 36 | return rank; 37 | } 38 | 39 | /** 40 | * Searches the player's hand for the best matching card. 41 | */ 42 | public EightsCard searchForMatch(EightsCard prev) { 43 | int[] suitStat = new int[4]; 44 | int[] rankStat = new int[14]; 45 | int index = -1; 46 | EightsCardCollection pile = getHand(); 47 | ArrayList possible = new ArrayList(); 48 | for (int i = 0; i < pile.size(); i++) { 49 | EightsCard card = pile.getCardAt(i); 50 | int suit = card.getSuit(); 51 | int rank = card.getRank(); 52 | suitStat[suit] += 1; 53 | rankStat[rank] += 1; 54 | if (card.matches(prev)){ 55 | possible.add(i); 56 | if (possible.size() == 1){ index = i; } 57 | } 58 | } 59 | int total = possible.size(); 60 | if (total == 0){ 61 | return null; 62 | } 63 | if (total > 1){ 64 | // Evalute cards 65 | int max = -1; 66 | boolean vary = true; 67 | for (int i=0;i<4;i++){ 68 | if (suitStat[i] == 0){ 69 | vary = false; 70 | break; 71 | } 72 | } 73 | for (int i=0;i max) { index = position; max = value; } 82 | } 83 | } 84 | return pile.removeAt(index); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Chapter12/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 | -------------------------------------------------------------------------------- /Chapter14/EightsCardCollection.java: -------------------------------------------------------------------------------- 1 | // 2 | // EightsCardCollection.java 3 | // Chapter14 4 | // 5 | // Created by Apollo Zhu on 8/20/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | import java.util.ArrayList; 10 | 11 | public class EightsCardCollection extends CardCollection{ 12 | 13 | private ArrayList eightCards; 14 | 15 | /** 16 | * Constructs an empty collection. 17 | */ 18 | public EightsCardCollection(String label) { 19 | super(label); 20 | this.eightCards = new ArrayList(); 21 | } 22 | 23 | /** 24 | * Returns the card with the given index. 25 | */ 26 | public EightsCard getCardAt(int i) { 27 | return eightCards.get(i); 28 | } 29 | 30 | /** 31 | * Returns the last card. 32 | */ 33 | public EightsCard last() { 34 | int i = size() - 1; 35 | return getCardAt(i); 36 | } 37 | 38 | /** 39 | * Adds the given card to the collection. 40 | */ 41 | public void append(EightsCard card) { 42 | eightCards.add(card); 43 | } 44 | 45 | /** 46 | * Removes and returns the card with the given index. 47 | */ 48 | public EightsCard removeAt(int i) { 49 | return eightCards.remove(i); 50 | } 51 | 52 | /** 53 | * Removes and returns the last card. 54 | */ 55 | public EightsCard removeLast() { 56 | int i = size() - 1; 57 | return removeAt(i); 58 | } 59 | 60 | /** 61 | * Returns the number of cards. 62 | */ 63 | public int size() { 64 | return eightCards.size(); 65 | } 66 | 67 | /** 68 | * Moves n cards from this collection to the given collection. 69 | */ 70 | public void deal(EightsCardCollection that, int n) { 71 | for (int i = 0; i < n; i++) { 72 | EightsCard card = removeLast(); 73 | that.append(card); 74 | } 75 | } 76 | 77 | /** 78 | * Moves all remaining cards to the given collection. 79 | */ 80 | public void dealAll(EightsCardCollection that) { 81 | int n = size(); 82 | deal(that, n); 83 | } 84 | 85 | /** 86 | * Swaps the cards at indexes i and j. 87 | */ 88 | public void swapAt(int i, int j) { 89 | append(eightCards.get(i)); 90 | eightCards.set(i, getCardAt(j)); 91 | eightCards.set(j, removeLast()); 92 | } 93 | 94 | /** 95 | * Returns a string representation of the card collection. 96 | */ 97 | public String toString() { 98 | return getLabel() + ": " + eightCards.toString(); 99 | } 100 | 101 | /** 102 | * Prints the label and cards. 103 | */ 104 | public void display() { 105 | System.out.println(getLabel() + ": "); 106 | for (Card card: eightCards){ 107 | System.out.println(card); 108 | } 109 | System.out.println(); 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /Chapter11/Rational.java: -------------------------------------------------------------------------------- 1 | // 2 | // Rational.java 3 | // Chapter11 4 | // 5 | // Created by Apollo Zhu on 8/13/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public class Rational{ 10 | public static void main(String args[]){ 11 | Rational temp1 = new Rational(); 12 | temp1.setNumerator(17); 13 | temp1.setDenominator(-12); 14 | temp1.printRational(); 15 | Rational temp2 = new Rational(temp1.getDenominator(),15); 16 | temp2.negate(); 17 | temp2.invert(); 18 | System.out.println(temp2+" "+temp2.add(temp1)+" "+temp2.toDouble()); 19 | } 20 | private int numerator; 21 | private int denominator; 22 | public void setNumerator(int numerator){ 23 | this.numerator = numerator; 24 | this.correct(); 25 | } 26 | public int getNumerator(){ 27 | return this.numerator; 28 | } 29 | public void setDenominator(int denominator){ 30 | this.denominator = denominator; 31 | this.correct(); 32 | } 33 | public int getDenominator(){ 34 | return this.denominator; 35 | } 36 | public Rational(){ 37 | this(0,1); 38 | } 39 | public Rational(Rational that){ 40 | this(that.numerator,that.denominator); 41 | } 42 | public Rational(int numerator, int denominator){ 43 | this.set(numerator,denominator); 44 | } 45 | public void set(Rational that){ 46 | this.set(that.numerator, that.denominator); 47 | } 48 | public void set(int numerator, int denominator){ 49 | this.numerator = numerator; 50 | this.denominator = denominator; 51 | this.correct(); 52 | } 53 | public void printRational(){ 54 | System.out.println(this.numerator+"/"+this.denominator); 55 | } 56 | public String toString(){ 57 | return this.numerator+"/"+this.denominator; 58 | } 59 | public void negate(){ 60 | this.numerator = -this.numerator; 61 | } 62 | public void invert(){ 63 | this.numerator+=this.denominator; 64 | this.denominator=this.numerator-this.denominator; 65 | this.numerator=this.numerator-this.denominator; 66 | this.correct(); 67 | } 68 | public Double toDouble(){ 69 | return (double)this.numerator / this.denominator; 70 | } 71 | public Rational reduce(){ 72 | int gcd = gcd(); 73 | return new Rational(this.numerator / gcd, this.denominator / gcd); 74 | } 75 | public int gcd(){ 76 | return gcd(this.numerator, this.denominator); 77 | } 78 | private int gcd(int m,int n){ 79 | if (m % n == 0) { 80 | return n; 81 | } 82 | else { 83 | return gcd(n,m % n); 84 | } 85 | } 86 | public Rational add(Rational that){ 87 | int newNumerator = this.numerator * that.denominator + that.numerator * this.denominator; 88 | int newDenominator = this.denominator * that.denominator; 89 | Rational result = new Rational(newNumerator, newDenominator).reduce(); 90 | result.correct(); 91 | this.set(result); 92 | return result; 93 | } 94 | private void correct(){ 95 | if (this.denominator < 0){ 96 | this.negate(); 97 | this.denominator = -this.denominator; 98 | } 99 | } 100 | } -------------------------------------------------------------------------------- /Chapter12/Card.java: -------------------------------------------------------------------------------- 1 | // 2 | // Card.java 3 | // Chapter11 4 | // 5 | // Created by Apollo Zhu on 8/14/16. 6 | // Copyright © 2015-2016 WWITDC. All rights reserved. 7 | // 8 | 9 | public class Card{ 10 | public static void main(String args[]){ 11 | // 12 | 13 | } 14 | 15 | public static Card[] makeDeck(){ 16 | Card[] cards = new Card[52]; 17 | for (int suit = 0;suit<=3;suit++){ 18 | for (int rank = 1;rank<=13;rank++){ 19 | cards[suit * 3 + rank] = new Card(rank, suit); 20 | } 21 | } 22 | return cards; 23 | } 24 | 25 | public int compareTo(Card that){ 26 | if (this.suit < that.suit){ 27 | return -1; 28 | } 29 | if (this.suit > that.suit){ 30 | return 1; 31 | } 32 | if (this.rank < that.rank){ 33 | if (this.rank == 1) { 34 | return 1; 35 | } 36 | else { 37 | return -1; 38 | } 39 | } 40 | if (this.rank > that.rank){ 41 | if (that.rank == 1) { 42 | return -1; 43 | } 44 | else { 45 | return 1; 46 | } 47 | } 48 | return 0; 49 | } 50 | 51 | public static int[] suitHist(Card[] cards){ 52 | int[] result = new int[4]; 53 | for (Card card:cards){ 54 | result[card.getSuit()] += 1; 55 | } 56 | return result; 57 | } 58 | public static boolean hasFlush(Card[] cards){ 59 | for (int count:suitHist(cards)){ 60 | if (count >= 5){ 61 | return true; 62 | } 63 | } 64 | return false; 65 | } 66 | 67 | // Mark: Supporting 68 | private final int rank; 69 | private final int suit; 70 | public static final String[] RANKS = { 71 | null, "Ace", "2", "3", "4", "5", "6", "7", 72 | "8", "9", "10", "Jack", "Queen", "King" 73 | }; 74 | public static final String[] SUITS = { 75 | "Clubs", "Diamonds", "Hearts", "Spades" 76 | }; 77 | public Card(int rank, int suit){ 78 | this.rank = rank; 79 | this.suit = suit; 80 | } 81 | public String toString(){ 82 | return RANKS[this.rank] + " of " + SUITS[this.suit]; 83 | } 84 | public boolean equals(Card that){ 85 | return this.rank == that.rank && this.suit == that.suit; 86 | } 87 | 88 | public int getRank(){ 89 | return this.rank; 90 | } 91 | public int getSuit(){ 92 | return this.suit; 93 | } 94 | public static int searchUnsorted(Card[] cards, Card target){ 95 | for (int i = 0; i < cards.length; i++) { 96 | if (cards[i].equals(target)) { 97 | return i; 98 | } 99 | } 100 | return -1; 101 | } 102 | public static int searchSorted(Card[] cards, Card target, int low, int high) { 103 | if (high < low) { 104 | return -1;} 105 | int mid = (low + high) / 2; 106 | int comp = cards[mid].compareTo(target); 107 | if (comp == 0) { 108 | return mid; 109 | } 110 | else if (comp < 0) { 111 | return searchSorted(cards, target, mid + 1, high); 112 | } 113 | else { 114 | return searchSorted(cards, target, low, mid - 1); 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /Chapter14/CardCollection.java: -------------------------------------------------------------------------------- 1 | // 2 | // CardCollection.java 3 | // Chapter14 4 | // 5 | // Created by Allen Downey and Chris Mayfield 6 | // Modified by Apollo Zhu on 8/20/16. 7 | // 8 | 9 | import java.util.ArrayList; 10 | import java.util.Random; 11 | 12 | /** 13 | * A collection of playing cards. 14 | */ 15 | public class CardCollection { 16 | 17 | private String label; 18 | private ArrayList cards; 19 | 20 | /** 21 | * Constructs an empty collection. 22 | */ 23 | public CardCollection(String label) { 24 | this.label = label; 25 | this.cards = new ArrayList(); 26 | } 27 | 28 | /** 29 | * Returns the label of the card collection. 30 | */ 31 | public String getLabel() { 32 | return label; 33 | } 34 | 35 | /** 36 | * Returns the card with the given index. 37 | */ 38 | public Card getCardAt(int i) { 39 | return cards.get(i); 40 | } 41 | 42 | /** 43 | * Returns the last card. 44 | */ 45 | public Card last() { 46 | int i = size() - 1; 47 | return getCardAt(i); 48 | } 49 | 50 | /** 51 | * Adds the given card to the collection. 52 | */ 53 | public void append(Card card) { 54 | cards.add(card); 55 | } 56 | 57 | /** 58 | * Removes and returns the card with the given index. 59 | */ 60 | public Card removeAt(int i) { 61 | return cards.remove(i); 62 | } 63 | 64 | /** 65 | * Removes and returns the last card. 66 | */ 67 | public Card removeLast() { 68 | int i = size() - 1; 69 | return removeAt(i); 70 | } 71 | 72 | /** 73 | * Returns the number of cards. 74 | */ 75 | public int size() { 76 | return cards.size(); 77 | } 78 | 79 | /** 80 | * True if the collection is empty, false otherwise. 81 | */ 82 | public boolean isEmpty() { 83 | return size() == 0; 84 | } 85 | 86 | /** 87 | * Moves n cards from this collection to the given collection. 88 | */ 89 | public void deal(CardCollection that, int n) { 90 | for (int i = 0; i < n; i++) { 91 | Card card = removeLast(); 92 | that.append(card); 93 | } 94 | } 95 | 96 | /** 97 | * Moves all remaining cards to the given collection. 98 | */ 99 | public void dealAll(CardCollection that) { 100 | int n = size(); 101 | deal(that, n); 102 | } 103 | 104 | /** 105 | * Swaps the cards at indexes i and j. 106 | */ 107 | public void swapAt(int i, int j) { 108 | append(cards.get(i)); 109 | cards.set(i, cards.get(j)); 110 | cards.set(j, removeLast()); 111 | } 112 | 113 | /** 114 | * Randomly permute the cards. 115 | */ 116 | public void shuffle() { 117 | Random random = new Random(); 118 | for (int i = size() - 1; i > 0; i--) { 119 | int j = random.nextInt(i); 120 | swapAt(i, j); 121 | } 122 | } 123 | 124 | /** 125 | * Returns a string representation of the card collection. 126 | */ 127 | public String toString() { 128 | return label + ": " + cards.toString(); 129 | } 130 | 131 | /** 132 | * Prints the label and cards. 133 | */ 134 | public void display() { 135 | System.out.println(label + ": "); 136 | for (Card card: cards){ 137 | System.out.println(card); 138 | } 139 | System.out.println(); 140 | } 141 | 142 | } 143 | -------------------------------------------------------------------------------- /Chapter13/Deck.java: -------------------------------------------------------------------------------- 1 | // 2 | // Card.java 3 | // Chapter13 4 | // 5 | // Created by Allen Downey and Chris Mayfield 6 | // Modified by Apollo Zhu on 8/19/16. 7 | // 8 | 9 | import java.util.Arrays; 10 | import java.util.Random; 11 | 12 | /** 13 | * A deck of playing cards (of fixed size). 14 | */ 15 | public class Deck { 16 | 17 | private static final Random random = new Random(); 18 | 19 | /** 20 | * Chooses a random number between low and high, including both. 21 | */ 22 | public static int randomInt(int low, int high){ 23 | return random.nextInt(Math.max(high,low) - Math.min(high,low) + 1) + low; 24 | } 25 | 26 | /** 27 | * Swaps the cards at indexes i and j. 28 | */ 29 | public void swapCards(int i, int j) { 30 | // System.out.println("Swapping "+cards[i]+" and "+cards[j]); 31 | Card temp = cards[i]; 32 | cards[i] = cards[j]; 33 | cards[j] = temp; 34 | } 35 | 36 | /** 37 | * Randomly permutes the array of cards. 38 | */ 39 | public void shuffle() { 40 | for (int i=0;i= c2.length || i < c1.length && c1[i].compareTo(c2[j]) <= 0){ 88 | sorted[k] = c1[i]; 89 | i++; 90 | } 91 | else { 92 | sorted[k] = c2[j]; 93 | j++; 94 | } 95 | } 96 | } 97 | Deck deck = new Deck(l1+l2); 98 | deck.cards = sorted; 99 | return deck; 100 | } 101 | 102 | /** 103 | * Returns a sorted copy of the deck using merge sort. 104 | */ 105 | public Deck mergeSort() { 106 | int len = cards.length; 107 | if (len == 0 || len == 1){ 108 | return this; 109 | } 110 | else { 111 | int mid = len / 2; 112 | Deck d1 = subdeck(0, mid - 1).mergeSort(); 113 | Deck d2 = subdeck(mid, len -1).mergeSort(); 114 | return merge(d1, d2); 115 | } 116 | } 117 | 118 | /** 119 | * Reorders the cards (in place) using insertion sort. 120 | */ 121 | public void insertionSort() { 122 | for (int i=1;i0;j--){ 124 | if (cards[j-1].compareTo(cards[j]) == 1){ 125 | swapCards(j-1,j); 126 | } 127 | else { 128 | break; 129 | } 130 | } 131 | } 132 | } 133 | 134 | /** 135 | * Returns a string representation of the deck. 136 | */ 137 | public String toString() { 138 | StringBuilder result = new StringBuilder(); 139 | for (int i = 0; i < this.cards.length; i++) { 140 | result.append(cards[i]); 141 | result.append('\n'); 142 | } 143 | return result.toString(); 144 | 145 | } 146 | 147 | // ------------------------------------------------ 148 | // MARK: Supporting 149 | 150 | private Card[] cards; 151 | 152 | /** 153 | * Constructs a standard deck of 52 cards. 154 | */ 155 | public Deck() { 156 | this.cards = new Card[52]; 157 | int index = 0; 158 | for (int suit = 0; suit <= 3; suit++) { 159 | for (int rank = 1; rank <= 13; rank++) { 160 | this.cards[index] = new Card(rank, suit); 161 | index++; 162 | } 163 | } 164 | } 165 | 166 | /** 167 | * Constructs a deck of n cards (null). 168 | */ 169 | public Deck(int n) { 170 | this.cards = new Card[n]; 171 | } 172 | 173 | /** 174 | * Gets the internal cards array. 175 | */ 176 | public Card[] getCards() { 177 | return this.cards; 178 | } 179 | 180 | /** 181 | * Displays each of the cards in the deck. 182 | */ 183 | public void print() { 184 | for (int i = 0; i < this.cards.length; i++) { 185 | System.out.println(this.cards[i]); 186 | } 187 | } 188 | 189 | /** 190 | * Returns a subset of the cards in the deck. 191 | */ 192 | public Deck subdeck(int low, int high) { 193 | Deck sub = new Deck(high - low + 1); 194 | for (int i = 0; i < sub.cards.length; i++) { 195 | sub.cards[i] = this.cards[low + i]; 196 | } 197 | return sub; 198 | } 199 | 200 | } 201 | -------------------------------------------------------------------------------- /Chapter14/Eights.java: -------------------------------------------------------------------------------- 1 | // 2 | // Eights.java 3 | // Chapter14 4 | // 5 | // Created by Allen Downey and Chris Mayfield 6 | // Modified by Apollo Zhu on 8/20/16. 7 | // 8 | 9 | import java.util.Scanner; 10 | import java.util.ArrayList; 11 | import java.util.Random; 12 | 13 | /** 14 | * Simulates a game of Crazy Eights. See 15 | * https://en.wikipedia.org/wiki/Crazy_Eights 16 | * for basic play and scoring rules. 17 | */ 18 | public class Eights { 19 | 20 | /** 21 | * Test with `players` for `count` times 22 | */ 23 | public static void test(ArrayList players, int count){ 24 | int total = players.size(); 25 | int[] scores = new int[total]; 26 | int[] board = new int[total]; 27 | for (int round=0;round players; 43 | private EightsCardCollection drawPile; 44 | private EightsCardCollection discardPile; 45 | private int current; 46 | private int winner; 47 | 48 | /** 49 | * Initializes the state of the game. 50 | */ 51 | public Eights(ArrayList players) { 52 | EightsDeck deck = new EightsDeck("Deck"); 53 | deck.shuffle(); 54 | 55 | // deal cards to each player 56 | int handSize = 5; 57 | this.players = players; 58 | for (int i=0;i 2 | 3 | 4 | Produced by OmniGraffle 7.2.1 5 | 2016-12-01 04:46:56 +0000 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | Canvas 1 47 | 48 | 49 | Layer 1 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | Card 69 | 70 | 71 | 72 | 73 | + RANKS: fi 74 | nal String[] 75 | + SUITS: fi 76 | nal String[] 77 | - rank: fi 78 | nal int 79 | - suit: fi 80 | nal int 81 | 82 | 83 | 84 | 85 | + Card(rank: int, suit: int) 86 | + compareT 87 | o(that: Card): int 88 | + equals(that: Card): int 89 | + getRank(): int 90 | + getSuit(): int 91 | + score(): int 92 | + toString(): String 93 | 94 | 95 | 96 | 97 | Eights Card 98 | 99 | 100 | 101 | 102 | 103 | 104 | + EightsCard(rank: int, suit: int) 105 | + matches(that: EightCard): boolean 106 | + score(): int 107 | 108 | 109 | 110 | 111 | 112 | Card Collection 113 | 114 | 115 | 116 | 117 | - label: String 118 | - cards: 119 | ArrayList<Card> 120 | 121 | 122 | 123 | 124 | + CardCollection(label: String) 125 | + getLabel(): String 126 | + getCardAt(i: int): Card 127 | + last(): Card 128 | + append(card: Card) 129 | + removeAt(i: int): Card 130 | + removeLast(): Card 131 | + size(): int 132 | + isEmpty(): boolean 133 | + deal(that: CardCollection, n: int) 134 | + dealAll(that: CardCollection) 135 | + swapAt(int i, int j) 136 | + shuffl 137 | e() 138 | + toString(): String 139 | + display() 140 | 141 | 142 | 143 | 144 | 145 | ArrayList<Card> 146 | 147 | 148 | 149 | 150 | 151 | Eights Card Collection 152 | 153 | 154 | 155 | 156 | - eightCards: 157 | ArrayList<EightsCard> 158 | 159 | 160 | 161 | 162 | + EightsCardCollection(label: String) 163 | + getCardAt(int i): EightsCard 164 | + last(): EightsCard 165 | + append(card: EightsCard) 166 | + removeAt(int i): EightsCard 167 | + removeLast(): EightsCard 168 | + size(): int 169 | + deal(that: EightsCardCollection, n: int) 170 | + dealAll(that: EightsCardCollection) 171 | + swapAt(int i, int j) 172 | + toString(): String 173 | + display() 174 | 175 | 176 | 177 | 178 | 179 | ArrayList<EightsCard> 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | Eights Deck 188 | 189 | 190 | 191 | 192 | 193 | 194 | + EightsDeck(label: String) 195 | - initialize() 196 | 197 | 198 | 199 | 200 | 201 | Player 202 | 203 | 204 | 205 | 206 | - name: String 207 | - hand: CardCollection 208 | 209 | 210 | 211 | 212 | + Player(name: String) 213 | + getName(): String 214 | + getHand(): CardCollection 215 | + score(): int 216 | + display() 217 | + displayScore() 218 | 219 | 220 | 221 | 222 | Eights Player 223 | 224 | 225 | 226 | 227 | - eightsHand: EightsCardCollection 228 | 229 | 230 | 231 | 232 | + EightsPlayer(name: String) 233 | + getHand(): EightsCardCollection 234 | + score(): int 235 | + display() 236 | + displayScore() 237 | + play(eights: Eights, prev: EightsCard): EightsCard 238 | + searchForMatch(prev: EightsCard): EightsCard 239 | + drawForMatch(eights: Eights, prev: EightsCard) 240 | 241 | 242 | 243 | 244 | 245 | Eights Genius 246 | 247 | 248 | 249 | 250 | 251 | 252 | + EightsGenius(name: String) 253 | - eval(card: EightsCard, total: int, vary: boolean): int 254 | - eval(rank: int, total: int, vary: boolean) 255 | + searchForMatch(prev: EightsCard): EightsCard 256 | 257 | 258 | 259 | 260 | 261 | Eights 262 | 263 | 264 | 265 | 266 | + debug: boolean = false 267 | - in: Scanner 268 | - random: Random 269 | - players: 270 | ArrayList<EightsPlayer> 271 | - drawPile: EightsCardCollection 272 | - discardPile: EightsCardCollection 273 | - current: int 274 | - winner: int 275 | 276 | 277 | 278 | 279 | + test(players: 280 | ArrayList<EightsPlayer>, count: int) 281 | + Eights(players: 282 | ArrayList<EightsPlayer>) 283 | + play(): int[] 284 | + isDone(): boolean 285 | + displayState() 286 | + waitForUser() 287 | + takeT 288 | urn(player: EightsPlayer) 289 | + getCurrent(): int 290 | + getW 291 | inner(): int 292 | + increaseIndex() 293 | + nextPlayer(): EightsPlayer 294 | + previousIndex(): int 295 | + draw(): EightsCard 296 | + reshuffl 297 | e() 298 | 299 | 300 | 301 | 302 | 303 | ArrayList<Integer> 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | Scanner 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | Random 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | ArrayList<EightsPlayer> 329 | 330 | 331 | 332 | 333 | 334 | 335 | Game Center 336 | 337 | 338 | 339 | 340 | 341 | 342 | + main(args: String[]) 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------