├── .gitignore
├── Chapter01
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── headfirstjava
│ └── chapterone
│ ├── BeerSong.java
│ ├── DooBee.java
│ ├── Exercise1bA.java
│ ├── Exercise1bB.java
│ ├── Exercise1bC.java
│ ├── IfTest.java
│ ├── IfTest2.java
│ ├── Loopy.java
│ ├── MyFirstApp.java
│ ├── PhraseOMatic.java
│ ├── PoolPuzzleOne.java
│ └── Shuffle1.java
├── Chapter02
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── headfirstjava
│ └── chaptertwo
│ ├── dog
│ ├── Dog.java
│ └── DogTestDrive.java
│ ├── drumkit
│ ├── DrumKit.java
│ └── DrumKitTestDrive.java
│ ├── dvdplayer
│ ├── DVDPlayer.java
│ └── DVDPlayerTestDrive.java
│ ├── echo
│ ├── Echo.java
│ └── EchoTestDrive.java
│ ├── guessgame
│ ├── GameLauncher.java
│ ├── GuessGame.java
│ └── Player.java
│ ├── movie
│ ├── Movie.java
│ └── MovieTestDrive.java
│ └── tapedeck
│ ├── TapeDeck.java
│ └── TapeDeckTestDrive.java
├── Chapter03
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── headfirstjava
│ └── chapterthree
│ ├── books
│ ├── Books.java
│ └── BooksTestDrive.java
│ ├── dog
│ └── Dog.java
│ ├── heapquiz
│ └── HeapQuiz.java
│ ├── hobbits
│ └── Hobbits.java
│ ├── testarrays
│ └── TestArrays.java
│ └── triangle
│ └── Triangle.java
├── Chapter04
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── headfirstjava
│ └── chapterfour
│ ├── clock
│ ├── Clock.java
│ └── ClockTestDrive.java
│ ├── dog
│ ├── Dog.java
│ └── DogTestDrive.java
│ ├── electricguitar
│ └── ElectricGuitar.java
│ ├── gooddog
│ ├── GoodDog.java
│ ├── GoodDogArrayTestDrive.java
│ └── GoodDogTestDrive.java
│ ├── improveddog
│ ├── ImprovedDog.java
│ └── ImprovedDogTestDrive.java
│ ├── mixfour
│ └── MixFour.java
│ ├── poordog
│ ├── PoorDog.java
│ └── PoorDogTestDrive.java
│ ├── puzzlefour
│ ├── PuzzleFour.java
│ └── PuzzleFourTestDrive.java
│ └── xcopy
│ └── XCopy.java
├── Chapter05
├── pom.xml
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── headfirstjava
│ │ └── chapterfive
│ │ ├── mixforfive
│ │ └── MixForFive.java
│ │ ├── multifor
│ │ └── MultiFor.java
│ │ ├── output
│ │ └── Output.java
│ │ └── simpledotcomgame
│ │ ├── GameHelper.java
│ │ ├── SimpleDotCom.java
│ │ ├── SimpleDotComGame.java
│ │ └── SimpleDotComTestDrive.java
│ └── test
│ └── java
│ └── com
│ └── headfirstjava
│ └── chapterfive
│ └── simpledotcomgame
│ └── SimpleDotComTest.java
├── Chapter06
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── headfirstjava
│ └── chaptersix
│ ├── arraylistmagnet
│ └── ArrayListMagnet.java
│ └── dotcomgame
│ ├── DotCom.java
│ ├── DotComBust.java
│ └── GameHelper.java
├── Chapter07
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── headfirstjava
│ └── chapterseven
│ ├── boats
│ ├── Boat.java
│ ├── RowBoat.java
│ ├── SailBoat.java
│ └── TestBoats.java
│ ├── mixedmessages
│ ├── A.java
│ ├── B.java
│ ├── C.java
│ └── MixedMessages.java
│ └── monsters
│ ├── Dragon.java
│ ├── Monster.java
│ ├── MonsterTestDrive.java
│ └── Vampire.java
├── Chapter08
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── headfirstjava
│ └── chaptereight
│ ├── animals
│ ├── Animal.java
│ ├── AnimalTestDrive.java
│ ├── Cat.java
│ ├── Dog.java
│ ├── MyAnimalList.java
│ └── MyDogList.java
│ └── poolpuzzle
│ ├── Acts.java
│ ├── Clowns.java
│ ├── Nose.java
│ ├── Of76.java
│ └── Picasso.java
├── Chapter09
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── headfirstjava
│ └── chapternine
│ ├── duck
│ ├── Duck.java
│ └── MakeDuck.java
│ ├── hippo
│ ├── Animal.java
│ ├── Hippo.java
│ └── MakeHippo.java
│ └── radiator
│ ├── RetentionBot.java
│ ├── SimUnit.java
│ ├── TestLifeSupportSim.java
│ ├── V2Radiator.java
│ └── V3Radiator.java
├── Chapter10
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── headfirstjava
│ └── chapterten
│ ├── calendar
│ └── CalendarTest.java
│ ├── dateformatting
│ └── TestDateFormatting.java
│ ├── lunarcodemagnets
│ └── FullMoons.java
│ ├── math
│ └── MathTestDrive.java
│ ├── numberformatting
│ └── TestNumberFormatting.java
│ ├── player
│ ├── Player.java
│ └── PlayerTestDrive.java
│ ├── staticsuper
│ ├── StaticSuper.java
│ └── StaticTests.java
│ ├── stringconversion
│ └── StringConversion.java
│ └── willitcompile
│ └── TestBox.java
├── Chapter11
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── headfirstjava
│ └── chaptereleven
│ ├── myex
│ ├── ExTestDrive.java
│ └── MyEx.java
│ ├── soundplayerapp
│ ├── MiniMiniMusicApp.java
│ └── MiniMusicCmdLine.java
│ ├── testexceptions
│ ├── ScaryException.java
│ └── TestExceptions.java
│ └── testmusic
│ └── MusicTest.java
├── Chapter14
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── headfirstjava
│ └── chapterfourteen
│ ├── dungeongame
│ ├── DungeonGame.java
│ └── DungeonTest.java
│ ├── gamesavertest
│ ├── GameCharacter.java
│ └── GameSaverTest.java
│ └── quizcard
│ ├── QuizCard.java
│ ├── QuizCardBuilder.java
│ └── QuizCardReader.java
├── Chapter16
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── headfirstjava
│ └── chaptersixteen
│ └── jukebox
│ ├── JukeBox1.java
│ ├── JukeBox3.java
│ ├── JukeBox5.java
│ ├── JukeBox6.java
│ ├── JukeBox8.java
│ ├── Song.java
│ ├── SongBad.java
│ └── SongList.txt
├── README.md
└── pom.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | *.idea
--------------------------------------------------------------------------------
/Chapter01/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.headfirstjava
8 | Chapter01
9 | 1.0-SNAPSHOT
10 | jar
11 |
12 |
13 |
14 |
15 | maven-compiler-plugin
16 | 3.1
17 |
18 | 1.7
19 | 1.7
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Chapter01/src/main/java/com/headfirstjava/chapterone/BeerSong.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterone;
2 |
3 | public class BeerSong {
4 |
5 | public static void main(String[] args) {
6 |
7 | int numberOfBeers = 99;
8 | String word = "bottles";
9 |
10 | while (numberOfBeers > 0) {
11 |
12 | if (numberOfBeers < 2) {
13 | word = "bottle";
14 | }
15 |
16 | System.out.println(numberOfBeers + " " + word + " of beer on the wall!");
17 | System.out.println(numberOfBeers + " " + word + " of beer!");
18 | System.out.println("Take one down!");
19 | System.out.println("Pass it around!");
20 | numberOfBeers = numberOfBeers - 1;
21 |
22 | if (numberOfBeers < 1) {
23 | System.out.println("No more bottles of beer on the wall!");
24 | }
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/Chapter01/src/main/java/com/headfirstjava/chapterone/DooBee.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterone;
2 |
3 | public class DooBee {
4 |
5 | public static void main(String[] args) {
6 |
7 | int x = 1;
8 |
9 | while (x < 3) {
10 | System.out.print("Doo");
11 | System.out.print("Bee");
12 | x = x + 1;
13 | }
14 |
15 | if (x == 3) {
16 | System.out.print("Do");
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/Chapter01/src/main/java/com/headfirstjava/chapterone/Exercise1bA.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterone;
2 |
3 | public class Exercise1bA {
4 |
5 | public static void main(String[] args) {
6 |
7 | int x = 1;
8 |
9 | while (x < 10) {
10 | if (x > 3) {
11 | System.out.println("Big x!");
12 | }
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Chapter01/src/main/java/com/headfirstjava/chapterone/Exercise1bB.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterone;
2 |
3 | public class Exercise1bB {
4 |
5 | public static void main(String[] args) {
6 |
7 | int x = 5;
8 |
9 | while (x > 1) {
10 | x = x - 1;
11 |
12 | if (x < 3) {
13 | System.out.println("Small x!");
14 | }
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Chapter01/src/main/java/com/headfirstjava/chapterone/Exercise1bC.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterone;
2 |
3 | public class Exercise1bC {
4 |
5 | public static void main(String[] args) {
6 |
7 | int x = 5;
8 |
9 | while (x > 1) {
10 | x = x - 1;
11 |
12 | if (x < 3) {
13 | System.out.println("Small x!");
14 | }
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Chapter01/src/main/java/com/headfirstjava/chapterone/IfTest.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterone;
2 |
3 | public class IfTest {
4 |
5 | public static void main(String[] args) {
6 |
7 | int x = 3;
8 |
9 | if (x == 3) {
10 | System.out.println("x must be 3!");
11 | }
12 |
13 | System.out.println("This runs no matter what!");
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Chapter01/src/main/java/com/headfirstjava/chapterone/IfTest2.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterone;
2 |
3 | public class IfTest2 {
4 |
5 | public static void main(String[] args) {
6 |
7 | int x = 2;
8 |
9 | if (x == 3) {
10 | System.out.println("x must be 3!");
11 | } else {
12 | System.out.println("x is NOT 3!");
13 | }
14 |
15 | System.out.println("This runs no matter what!");
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Chapter01/src/main/java/com/headfirstjava/chapterone/Loopy.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterone;
2 |
3 | public class Loopy {
4 |
5 | public static void main(String[] args) {
6 |
7 | int x = 1;
8 | System.out.println("Before the Loop!");
9 |
10 | while (x < 4) {
11 | System.out.println("In the Loop...");
12 | System.out.println("Value of x is " + x + "!");
13 | x = x + 1;
14 | }
15 |
16 | System.out.println("This is after the loop!");
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/Chapter01/src/main/java/com/headfirstjava/chapterone/MyFirstApp.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterone;
2 |
3 | public class MyFirstApp {
4 |
5 | public static void main(String[] args) {
6 |
7 | System.out.println("I Rule!");
8 | System.out.println("The World...");
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/Chapter01/src/main/java/com/headfirstjava/chapterone/PhraseOMatic.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterone;
2 |
3 | public class PhraseOMatic {
4 |
5 | public static void main(String[] args) {
6 |
7 | String[] wordListOne = {"24/7", "multiTier", "30,OOO foot", "B-to-B", "win-win", "frontend", "web-based",
8 | "pervasive", "smart", "six-sigma", "critical-path", "dynamic"};
9 |
10 | String[] wordListTwo = {"empowered", "sticky", "value-added.", "oriented", "centric", "distributed",
11 | "clustered", "branded", "outside-the-box", "positioned", "networked", "focused",
12 | "leveraged", "aligned", "targeted", "shared", "cooperative", "accelerated"};
13 |
14 | String[] wordListThree = {"process", "tipping-point", "solution", "architecture", "core competency", "strategy",
15 | "mindshare", "portal", "space", "vision", "paradigm", "mission"};
16 |
17 | int oneLength = wordListOne.length;
18 | int twoLength = wordListTwo.length;
19 | int threeLength = wordListThree.length;
20 |
21 | int rand1 = (int) (Math.random() * oneLength);
22 | int rand2 = (int) (Math.random() * twoLength);
23 | int rand3 = (int) (Math.random() * threeLength);
24 |
25 | String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3];
26 |
27 | System.out.println("What we need is a... " + phrase + "!");
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Chapter01/src/main/java/com/headfirstjava/chapterone/PoolPuzzleOne.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterone;
2 |
3 | public class PoolPuzzleOne {
4 |
5 | public static void main(String[] args) {
6 |
7 | int x = 0;
8 |
9 | while (x < 4) {
10 | System.out.print("A");
11 |
12 | if (x < 1) {
13 | System.out.print(" ");
14 | }
15 |
16 | System.out.print("n");
17 |
18 | if (x > 1) {
19 | System.out.print(" oyster!");
20 | x = x + 2;
21 | }
22 |
23 | if (x == 1) {
24 | System.out.print("noys");
25 | }
26 |
27 | if (x < 1) {
28 | System.out.print("oise");
29 | }
30 |
31 | System.out.println("");
32 | x = x + 1;
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Chapter01/src/main/java/com/headfirstjava/chapterone/Shuffle1.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterone;
2 |
3 | public class Shuffle1 {
4 |
5 | public static void main(String[] args) {
6 |
7 | int x = 3;
8 |
9 | while (x > 0) {
10 | if (x > 2) {
11 | System.out.print("A");
12 | }
13 |
14 | if (x == 2) {
15 | System.out.print("B C");
16 | }
17 |
18 | x = x - 1;
19 | System.out.print("-");
20 |
21 | if (x == 1) {
22 | System.out.print("D");
23 | x = x - 1;
24 | }
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/Chapter02/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.headfirstjava
8 | Chapter02
9 | 1.0-SNAPSHOT
10 | jar
11 |
12 |
13 |
14 |
15 | maven-compiler-plugin
16 | 3.1
17 |
18 | 1.7
19 | 1.7
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Chapter02/src/main/java/com/headfirstjava/chaptertwo/dog/Dog.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptertwo.dog;
2 |
3 | public class Dog {
4 |
5 | int size;
6 | String breed;
7 | String name;
8 |
9 | void bark() {
10 | System.out.println("Ruff Ruff!");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/Chapter02/src/main/java/com/headfirstjava/chaptertwo/dog/DogTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptertwo.dog;
2 |
3 | public class DogTestDrive {
4 |
5 | public static void main(String[] args) {
6 |
7 | Dog dog = new Dog();
8 | dog.size = 40;
9 | dog.bark();
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Chapter02/src/main/java/com/headfirstjava/chaptertwo/drumkit/DrumKit.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptertwo.drumkit;
2 |
3 | public class DrumKit {
4 |
5 | boolean topHat = true;
6 | boolean snare = true;
7 |
8 | void playSnare() {
9 | System.out.println("Bang bang ba-bang!");
10 | }
11 |
12 | void playTopHat() {
13 | System.out.println("Ding ding da-ding!");
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Chapter02/src/main/java/com/headfirstjava/chaptertwo/drumkit/DrumKitTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptertwo.drumkit;
2 |
3 | public class DrumKitTestDrive {
4 |
5 | public static void main(String[] args) {
6 |
7 | DrumKit drumKit = new DrumKit();
8 | drumKit.playSnare();
9 | drumKit.snare = false;
10 | drumKit.playTopHat();
11 |
12 | if (drumKit.snare == true) {
13 | drumKit.playSnare();
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/Chapter02/src/main/java/com/headfirstjava/chaptertwo/dvdplayer/DVDPlayer.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptertwo.dvdplayer;
2 |
3 | public class DVDPlayer {
4 |
5 | boolean canRecord = false;
6 |
7 | void recordDVD() {
8 | System.out.println("DVD recording!");
9 | }
10 |
11 | void playDVD() {
12 | System.out.println("DVD playing!");
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/Chapter02/src/main/java/com/headfirstjava/chaptertwo/dvdplayer/DVDPlayerTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptertwo.dvdplayer;
2 |
3 | public class DVDPlayerTestDrive {
4 |
5 | public static void main(String[] args) {
6 |
7 | DVDPlayer dvdPlayer = new DVDPlayer();
8 | dvdPlayer.canRecord = true;
9 | dvdPlayer.playDVD();
10 |
11 | if (dvdPlayer.canRecord == true) {
12 | dvdPlayer.recordDVD();
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Chapter02/src/main/java/com/headfirstjava/chaptertwo/echo/Echo.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptertwo.echo;
2 |
3 | public class Echo {
4 |
5 | int count = 0;
6 |
7 | void hello() {
8 | System.out.println("helloooo... ");
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/Chapter02/src/main/java/com/headfirstjava/chaptertwo/echo/EchoTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptertwo.echo;
2 |
3 | public class EchoTestDrive {
4 |
5 | public static void main(String[] args) {
6 |
7 | Echo echoOne = new Echo();
8 | Echo echoTwo = new Echo();
9 | // Bonus Answer: Echo echoTwo = echoOne;
10 |
11 | int x = 0;
12 |
13 | while (x < 4) {
14 | echoOne.hello();
15 | echoOne.count = echoOne.count + 1;
16 |
17 | if (x == 3) {
18 | echoTwo.count = echoTwo.count + 1;
19 | }
20 |
21 | if (x > 0) {
22 | echoTwo.count = echoTwo.count + echoOne.count;
23 | }
24 |
25 | x = x + 1;
26 | }
27 |
28 | System.out.println(echoTwo.count);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/Chapter02/src/main/java/com/headfirstjava/chaptertwo/guessgame/GameLauncher.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptertwo.guessgame;
2 |
3 | public class GameLauncher {
4 |
5 | public static void main(String[] args) {
6 |
7 | GuessGame guessGame = new GuessGame();
8 | guessGame.startGame();
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/Chapter02/src/main/java/com/headfirstjava/chaptertwo/guessgame/GuessGame.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptertwo.guessgame;
2 |
3 | public class GuessGame {
4 |
5 | Player playerOne;
6 | Player playerTwo;
7 | Player playerThree;
8 |
9 | public void startGame() {
10 | playerOne = new Player();
11 | playerTwo = new Player();
12 | playerThree = new Player();
13 |
14 | int playerOneGuess = 0;
15 | int playerTwoGuess = 0;
16 | int playerThreeGuess = 0;
17 |
18 | boolean playerOneIsRight = false;
19 | boolean playerTwoIsRight = false;
20 | boolean playerThreeIsRight = false;
21 |
22 | int targetNumber = (int) (Math.random() * 10);
23 | System.out.println("I'm thinking of a numberGuessed between 0 and 9...");
24 |
25 | while (true) {
26 | System.out.println("Number to guess is... " + targetNumber);
27 |
28 | playerOne.guess();
29 | playerTwo.guess();
30 | playerThree.guess();
31 |
32 | playerOneGuess = playerOne.numberGuessed;
33 | System.out.println("Player one guessed... " + playerOneGuess);
34 |
35 | playerTwoGuess = playerTwo.numberGuessed;
36 | System.out.println("Player two guessed... " + playerTwoGuess);
37 |
38 | playerThreeGuess = playerThree.numberGuessed;
39 | System.out.println("Player three guessed... " + playerThreeGuess);
40 |
41 | if (playerOneGuess == targetNumber) {
42 | playerOneIsRight = true;
43 | }
44 |
45 | if (playerTwoGuess == targetNumber) {
46 | playerTwoIsRight = true;
47 | }
48 |
49 | if (playerThreeGuess == targetNumber) {
50 | playerThreeIsRight = true;
51 | }
52 |
53 | if (playerOneIsRight || playerTwoIsRight || playerThreeIsRight) {
54 | System.out.println("We have a winner!");
55 | System.out.println("Player one got it right? " + playerOneIsRight);
56 | System.out.println("Player two got it right? " + playerTwoIsRight);
57 | System.out.println("Player three got it right? " + playerThreeIsRight);
58 | System.out.println("Game is over!");
59 | break;
60 | } else {
61 | System.out.println("Players will have to try again...");
62 | }
63 | }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/Chapter02/src/main/java/com/headfirstjava/chaptertwo/guessgame/Player.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptertwo.guessgame;
2 |
3 | public class Player {
4 |
5 | int numberGuessed = 0;
6 |
7 | public void guess() {
8 | numberGuessed = (int) (Math.random() * 10);
9 | System.out.println("I'm guessing " + numberGuessed);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Chapter02/src/main/java/com/headfirstjava/chaptertwo/movie/Movie.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptertwo.movie;
2 |
3 | public class Movie {
4 |
5 | String title;
6 | String genre;
7 | int rating;
8 |
9 | void playIt() {
10 | System.out.println("Playing the movie...");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/Chapter02/src/main/java/com/headfirstjava/chaptertwo/movie/MovieTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptertwo.movie;
2 |
3 | public class MovieTestDrive {
4 |
5 | public static void main(String[] args) {
6 |
7 | Movie movieOne = new Movie();
8 | movieOne.title = "Gone with the Stock";
9 | movieOne.genre = "Tragic";
10 | movieOne.rating = -2;
11 |
12 | Movie movieTwo = new Movie();
13 | movieTwo.title = "Lost in Cubicle Space";
14 | movieTwo.genre = "Comedy";
15 | movieTwo.rating = 5;
16 | movieTwo.playIt();
17 |
18 | Movie movieThree = new Movie();
19 | movieThree.title = "Byte Club";
20 | movieThree.genre = "Tragic but ultimately uplifting";
21 | movieThree.rating = 127;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/Chapter02/src/main/java/com/headfirstjava/chaptertwo/tapedeck/TapeDeck.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptertwo.tapedeck;
2 |
3 | public class TapeDeck {
4 |
5 | boolean canRecord = false;
6 |
7 | void playTape() {
8 | System.out.println("tape playing");
9 | }
10 |
11 | void recordTape() {
12 | System.out.println("tape recording");
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/Chapter02/src/main/java/com/headfirstjava/chaptertwo/tapedeck/TapeDeckTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptertwo.tapedeck;
2 |
3 | public class TapeDeckTestDrive {
4 |
5 | public static void main(String[] args) {
6 |
7 | TapeDeck tapeDeck = new TapeDeck();
8 | tapeDeck.canRecord = true;
9 | tapeDeck.playTape();
10 |
11 | if (tapeDeck.canRecord == true) {
12 | tapeDeck.recordTape();
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/Chapter03/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.headfirstjava
8 | Chapter03
9 | 1.0-SNAPSHOT
10 | jar
11 |
12 |
13 |
14 |
15 | maven-compiler-plugin
16 | 3.1
17 |
18 | 1.7
19 | 1.7
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Chapter03/src/main/java/com/headfirstjava/chapterthree/books/Books.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterthree.books;
2 |
3 | public class Books {
4 |
5 | String title;
6 | String author;
7 | }
8 |
--------------------------------------------------------------------------------
/Chapter03/src/main/java/com/headfirstjava/chapterthree/books/BooksTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterthree.books;
2 |
3 | public class BooksTestDrive {
4 |
5 | public static void main(String[] args) {
6 |
7 | Books[] myBooks = new Books[3];
8 | int x = 0;
9 | myBooks[0] = new Books();
10 | myBooks[1] = new Books();
11 | myBooks[2] = new Books();
12 | myBooks[0].title = "The Grapes of Java";
13 | myBooks[1].title = "The Java Gatsby";
14 | myBooks[2].title = "The Java Cookbook";
15 | myBooks[0].author = "Bob";
16 | myBooks[1].author = "Sue";
17 | myBooks[2].author = "Ian";
18 |
19 | while (x < 3) {
20 | System.out.print(myBooks[x].title);
21 | System.out.print(" by ");
22 | System.out.println(myBooks[x].author);
23 | x = x + 1;
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Chapter03/src/main/java/com/headfirstjava/chapterthree/dog/Dog.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterthree.dog;
2 |
3 | public class Dog {
4 |
5 | String name;
6 |
7 | public static void main(String[] args) {
8 |
9 | Dog dog1 = new Dog();
10 | dog1.bark();
11 | dog1.name = "Bart";
12 |
13 | Dog[] myDogs = new Dog[3];
14 | myDogs[0] = new Dog();
15 | myDogs[1] = new Dog();
16 | myDogs[2] = dog1;
17 |
18 | myDogs[0].name = "Fred";
19 | myDogs[1].name = "Marge";
20 |
21 | System.out.print("last dog's name is ");
22 | System.out.println(myDogs[2].name);
23 |
24 | int x = 0;
25 |
26 | while (x < myDogs.length) {
27 | myDogs[x].bark();
28 | x = x + 1;
29 | }
30 | }
31 |
32 | public void bark() {
33 | System.out.println(name + " says Ruff!");
34 | }
35 |
36 | public void eat() {
37 |
38 | }
39 |
40 | public void chaseCat() {
41 |
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Chapter03/src/main/java/com/headfirstjava/chapterthree/heapquiz/HeapQuiz.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterthree.heapquiz;
2 |
3 | public class HeapQuiz {
4 | int id = 0;
5 |
6 | public static void main(String[] args) {
7 | int x = 0;
8 |
9 | HeapQuiz[] hq = new HeapQuiz[5];
10 | while (x < 3) {
11 | hq[x] = new HeapQuiz();
12 | hq[x].id = x;
13 | x = x + 1;
14 | }
15 |
16 | hq[3] = hq[1];
17 | hq[4] = hq[1];
18 | hq[3] = null;
19 | hq[4] = hq[0];
20 | hq[0] = hq[3];
21 | hq[3] = hq[2];
22 | hq[2] = hq[0];
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/Chapter03/src/main/java/com/headfirstjava/chapterthree/hobbits/Hobbits.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterthree.hobbits;
2 |
3 | public class Hobbits {
4 |
5 | String name;
6 |
7 | public static void main(String[] args) {
8 |
9 | Hobbits[] hobbits = new Hobbits[3];
10 | int z = 0;
11 |
12 | while (z < 3) {
13 | hobbits[z] = new Hobbits();
14 | hobbits[z].name = "Bilbo";
15 |
16 | if (z == 1) {
17 | hobbits[z].name = "Frodo";
18 | }
19 |
20 | if (z == 2) {
21 | hobbits[z].name = "Sam";
22 | }
23 |
24 | System.out.print(hobbits[z].name + " is a ");
25 | System.out.println("good Hobbit name!");
26 | z = z + 1;
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Chapter03/src/main/java/com/headfirstjava/chapterthree/testarrays/TestArrays.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterthree.testarrays;
2 |
3 | public class TestArrays {
4 |
5 | public static void main(String[] args) {
6 |
7 | String[] islands = new String[4];
8 | islands[0] = "Bermuda";
9 | islands[1] = "Fiji";
10 | islands[2] = "Azores";
11 | islands[3] = "Cozumel";
12 |
13 | int[] index = new int[4];
14 | index[0] = 1;
15 | index[1] = 3;
16 | index[2] = 0;
17 | index[3] = 2;
18 |
19 | int y = 0;
20 | int ref;
21 |
22 | while (y < 4) {
23 | ref = index[y];
24 | System.out.print("islands = ");
25 | System.out.println(islands[ref]);
26 | y = y + 1;
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Chapter03/src/main/java/com/headfirstjava/chapterthree/triangle/Triangle.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterthree.triangle;
2 |
3 | public class Triangle {
4 |
5 | double area;
6 | int height;
7 | int length;
8 |
9 | public static void main(String[] args) {
10 | int x = 0;
11 | Triangle[] triangleArray = new Triangle[4];
12 |
13 | while (x < 4) { //
14 | triangleArray[x] = new Triangle();//
15 | triangleArray[x].height = (x + 1) * 2; //
16 | triangleArray[x].length = x + 4; //
17 | triangleArray[x].setArea();//
18 | System.out.print("triangle " + x + ", area");
19 | System.out.println(" = " + triangleArray[x].area); //
20 | x = x + 1; //
21 | }
22 |
23 | int y = x;
24 | x = 27;
25 | Triangle t5 = triangleArray[2];
26 | triangleArray[2].area = 343;
27 | System.out.print("y = " + y);
28 | System.out.println(", t5 area = " + t5.area);
29 | }
30 |
31 | void setArea() {
32 | area = (height * length) / 2; //
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Chapter04/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.headfirstjava
8 | Chapter04
9 | 1.0-SNAPSHOT
10 | jar
11 |
12 |
13 |
14 |
15 | maven-compiler-plugin
16 | 3.1
17 |
18 | 1.7
19 | 1.7
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Chapter04/src/main/java/com/headfirstjava/chapterfour/clock/Clock.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfour.clock;
2 |
3 | public class Clock {
4 |
5 | String time;
6 |
7 | public String getTime() {
8 | return time;
9 | }
10 |
11 | public void setTime(String time) {
12 | this.time = time;
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/Chapter04/src/main/java/com/headfirstjava/chapterfour/clock/ClockTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfour.clock;
2 |
3 | public class ClockTestDrive {
4 |
5 | public static void main(String[] args) {
6 |
7 | Clock clock = new Clock();
8 |
9 | clock.setTime("1245");
10 | String time = clock.getTime();
11 | System.out.print("Time: " + time);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/Chapter04/src/main/java/com/headfirstjava/chapterfour/dog/Dog.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfour.dog;
2 |
3 | public class Dog {
4 |
5 | int size;
6 | String name;
7 |
8 | void bark() {
9 | if (size > 60) {
10 | System.out.println("Woof! Woof!");
11 | } else if (size > 14) {
12 | System.out.println("Ruff! Ruff!");
13 | } else {
14 | System.out.println("Yip! Yip!");
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Chapter04/src/main/java/com/headfirstjava/chapterfour/dog/DogTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfour.dog;
2 |
3 | public class DogTestDrive {
4 |
5 | public static void main(String[] args) {
6 | Dog one = new Dog();
7 | one.size = 70;
8 | Dog two = new Dog();
9 | two.size = 8;
10 | Dog three = new Dog();
11 | three.size = 35;
12 |
13 | one.bark();
14 | two.bark();
15 | three.bark();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Chapter04/src/main/java/com/headfirstjava/chapterfour/electricguitar/ElectricGuitar.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfour.electricguitar;
2 |
3 | public class ElectricGuitar {
4 |
5 | String brand;
6 | int numberOfPickups;
7 | boolean rockStarUsesIt;
8 |
9 | public String getBrand() {
10 | return brand;
11 | }
12 |
13 | public void setBrand(String brand) {
14 | this.brand = brand;
15 | }
16 |
17 | public int getNumberOfPickups() {
18 | return numberOfPickups;
19 | }
20 |
21 | public void setNumberOfPickups(int numberOfPickups) {
22 | this.numberOfPickups = numberOfPickups;
23 | }
24 |
25 | public boolean isRockStarUsesIt() {
26 | return rockStarUsesIt;
27 | }
28 |
29 | public void setRockStarUsesIt(boolean rockStarUsesIt) {
30 | this.rockStarUsesIt = rockStarUsesIt;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Chapter04/src/main/java/com/headfirstjava/chapterfour/gooddog/GoodDog.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfour.gooddog;
2 |
3 | public class GoodDog {
4 |
5 | private int size;
6 |
7 | public int getSize() {
8 | return size;
9 | }
10 |
11 | public void setSize(int size) {
12 | this.size = size;
13 | }
14 |
15 | void bark() {
16 | if (size > 60) {
17 | System.out.println("Woof! Woof!");
18 | } else if (size > 14) {
19 | System.out.println("Ruff! Ruff!");
20 | } else {
21 | System.out.println("Yip! Yip!");
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/Chapter04/src/main/java/com/headfirstjava/chapterfour/gooddog/GoodDogArrayTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfour.gooddog;
2 |
3 | public class GoodDogArrayTestDrive {
4 |
5 | public static void main(String[] args) {
6 |
7 | GoodDog[] goodDogs = new GoodDog[2];
8 | goodDogs[0] = new GoodDog();
9 | goodDogs[1] = new GoodDog();
10 |
11 | goodDogs[0].setSize(70);
12 | goodDogs[1].setSize(8);
13 |
14 | System.out.println("Dog one: " + goodDogs[0].getSize());
15 | System.out.println("Dog two: " + goodDogs[1].getSize());
16 | goodDogs[0].bark();
17 | goodDogs[1].bark();
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/Chapter04/src/main/java/com/headfirstjava/chapterfour/gooddog/GoodDogTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfour.gooddog;
2 |
3 | public class GoodDogTestDrive {
4 |
5 | public static void main(String[] args) {
6 |
7 | GoodDog goodDogOne = new GoodDog();
8 | goodDogOne.setSize(70);
9 | GoodDog goodDogTwo = new GoodDog();
10 | goodDogTwo.setSize(8);
11 | System.out.println("Dog one: " + goodDogOne.getSize());
12 | System.out.println("Dog two: " + goodDogTwo.getSize());
13 | goodDogOne.bark();
14 | goodDogTwo.bark();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/Chapter04/src/main/java/com/headfirstjava/chapterfour/improveddog/ImprovedDog.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfour.improveddog;
2 |
3 | public class ImprovedDog {
4 |
5 | void bark(int numberOfBarks) {
6 | while (numberOfBarks > 0) {
7 | System.out.println("Ruff!");
8 | numberOfBarks = numberOfBarks - 1;
9 | }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Chapter04/src/main/java/com/headfirstjava/chapterfour/improveddog/ImprovedDogTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfour.improveddog;
2 |
3 | public class ImprovedDogTestDrive {
4 |
5 | public static void main(String[] args) {
6 | ImprovedDog improvedDog = new ImprovedDog();
7 | improvedDog.bark(3);
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Chapter04/src/main/java/com/headfirstjava/chapterfour/mixfour/MixFour.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfour.mixfour;
2 |
3 | public class MixFour {
4 |
5 | int counter = 0;
6 |
7 | public static void main(String[] args) {
8 |
9 | int count = 0;
10 | MixFour[] mixFourArray = new MixFour[20];
11 | int x = 0;
12 |
13 | while (x < 9) {
14 | mixFourArray[x] = new MixFour();
15 | mixFourArray[x].counter = mixFourArray[x].counter + 1;
16 | count = count + 1;
17 | count = count + mixFourArray[x].maybeNew(x);
18 | x = x + 1;
19 | }
20 |
21 | System.out.println(count + " " + mixFourArray[1].counter);
22 | }
23 |
24 | public int maybeNew(int index) {
25 | if (index < 5) {
26 | MixFour mixFour = new MixFour();
27 | mixFour.counter = mixFour.counter + 1;
28 | return 1;
29 | }
30 |
31 | return 0;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Chapter04/src/main/java/com/headfirstjava/chapterfour/poordog/PoorDog.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfour.poordog;
2 |
3 | public class PoorDog {
4 |
5 | private int size;
6 | private String name;
7 |
8 | public int getSize() {
9 | return size;
10 | }
11 |
12 | public void setSize(int size) {
13 | this.size = size;
14 | }
15 |
16 | public String getName() {
17 | return name;
18 | }
19 |
20 | public void setName(String name) {
21 | this.name = name;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/Chapter04/src/main/java/com/headfirstjava/chapterfour/poordog/PoorDogTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfour.poordog;
2 |
3 | public class PoorDogTestDrive {
4 |
5 | public static void main(String[] args) {
6 |
7 | PoorDog poorDog = new PoorDog();
8 | System.out.println("Dog size is " + poorDog.getSize());
9 | System.out.println("Dog name is " + poorDog.getName());
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Chapter04/src/main/java/com/headfirstjava/chapterfour/puzzlefour/PuzzleFour.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfour.puzzlefour;
2 |
3 | public class PuzzleFour {
4 |
5 | int ivar;
6 |
7 | public int doStuff(int factor) {
8 | if (ivar > 100) {
9 | return ivar * factor;
10 | } else {
11 | return ivar * (5 - factor);
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/Chapter04/src/main/java/com/headfirstjava/chapterfour/puzzlefour/PuzzleFourTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfour.puzzlefour;
2 |
3 | public class PuzzleFourTestDrive {
4 |
5 | public static void main(String[] args) {
6 | PuzzleFour[] puzzleFours = new PuzzleFour[6];
7 | int y = 1;
8 | int x = 0;
9 | int result = 0;
10 |
11 | while (x < 6) {
12 | puzzleFours[x] = new PuzzleFour();
13 | puzzleFours[x].ivar = y;
14 | y = y * 10;
15 | x = x + 1;
16 | }
17 |
18 | x = 6;
19 |
20 | while (x > 0) {
21 | x = x - 1;
22 | result = result + puzzleFours[x].doStuff(x);
23 | }
24 |
25 | System.out.println("Result: " + result);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/Chapter04/src/main/java/com/headfirstjava/chapterfour/xcopy/XCopy.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfour.xcopy;
2 |
3 | public class XCopy {
4 |
5 | public static void main(String[] args) {
6 | int orig = 42;
7 | XCopy xCopy = new XCopy();
8 | int y = xCopy.go(orig);
9 | System.out.println(orig + " " + y);
10 | }
11 |
12 | int go(int arg) {
13 | arg = arg * 2;
14 | return arg;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/Chapter05/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.headfirstjava
8 | Chapter05
9 | 1.0-SNAPSHOT
10 | jar
11 |
12 |
13 |
14 |
15 | maven-compiler-plugin
16 | 3.1
17 |
18 | 1.7
19 | 1.7
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Chapter05/src/main/java/com/headfirstjava/chapterfive/mixforfive/MixForFive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfive.mixforfive;
2 |
3 | public class MixForFive {
4 |
5 | public static void main(String[] args) {
6 | int x = 0;
7 | int y = 30;
8 |
9 | for (int outer = 0; outer < 3; outer++) {
10 |
11 | for (int inner = 4; inner > 1; inner--) {
12 |
13 | x = x + 3;
14 | y = y - 2;
15 |
16 | if (x == 6) {
17 | break;
18 | }
19 |
20 | x = x + 3;
21 | }
22 |
23 | y = y - 2;
24 | }
25 |
26 | System.out.println(x + " " + y);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Chapter05/src/main/java/com/headfirstjava/chapterfive/multifor/MultiFor.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfive.multifor;
2 |
3 | public class MultiFor {
4 |
5 | public static void main(String[] args) {
6 |
7 | for (int x = 0; x < 4; x++) {
8 |
9 | for (int y = 4; y > 2; y--) {
10 | System.out.println(x + " " + y);
11 | }
12 |
13 | if (x == 1) {
14 | x++;
15 | }
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/Chapter05/src/main/java/com/headfirstjava/chapterfive/output/Output.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfive.output;
2 |
3 | public class Output {
4 |
5 | public static void main(String[] args) {
6 |
7 | Output output = new Output();
8 | output.go();
9 | ;
10 | }
11 |
12 | void go() {
13 | int y = 7;
14 |
15 | for (int x = 1; x < 8; x++) {
16 | y++;
17 |
18 | if (x > 4) {
19 | System.out.print(++y + " ");
20 | }
21 |
22 | if (y > 14) {
23 | System.out.println(" x = " + x);
24 | break;
25 | }
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Chapter05/src/main/java/com/headfirstjava/chapterfive/simpledotcomgame/GameHelper.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfive.simpledotcomgame;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.IOException;
5 | import java.io.InputStreamReader;
6 |
7 | public class GameHelper {
8 |
9 | public String getUserInput(String prompt) {
10 | String inputLine = null;
11 | System.out.print(prompt + " ");
12 |
13 | try {
14 | BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in));
15 | inputLine = inputStream.readLine();
16 | if (inputLine.length() == 0) return null;
17 | } catch (IOException e) {
18 | System.out.println("IOException: " + e);
19 | }
20 |
21 | return inputLine;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/Chapter05/src/main/java/com/headfirstjava/chapterfive/simpledotcomgame/SimpleDotCom.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfive.simpledotcomgame;
2 |
3 | public class SimpleDotCom {
4 |
5 | int[] locationCells;
6 | int numberOfHits = 0;
7 |
8 | public void setLocationCells(int[] locations) {
9 | locationCells = locations;
10 | }
11 |
12 | public String checkYourself(String stringGuess) {
13 | int guess = Integer.parseInt(stringGuess);
14 | String result = "miss";
15 |
16 | for (int cell : locationCells) {
17 | if (guess == cell) {
18 | result = "hit";
19 | numberOfHits++;
20 | break;
21 | }
22 | }
23 |
24 | if (numberOfHits == locationCells.length) {
25 | result = "kill";
26 | }
27 |
28 | System.out.println(result);
29 | return result;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/Chapter05/src/main/java/com/headfirstjava/chapterfive/simpledotcomgame/SimpleDotComGame.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfive.simpledotcomgame;
2 |
3 | public class SimpleDotComGame {
4 |
5 | public static void main(String[] args) {
6 |
7 | int numberOfGuesses = 0;
8 | GameHelper gameHelper = new GameHelper();
9 | SimpleDotCom simpleDotCom = new SimpleDotCom();
10 | int randomNumber = (int) (Math.random() * 5);
11 | int[] locations = {randomNumber, randomNumber + 1, randomNumber + 2};
12 | simpleDotCom.setLocationCells(locations);
13 | boolean isAlive = true;
14 |
15 | while (isAlive == true) {
16 | String guess = gameHelper.getUserInput("Enter a number: ");
17 | String result = simpleDotCom.checkYourself(guess);
18 | numberOfGuesses++;
19 |
20 | if (result.equals("kill")) {
21 | isAlive = false;
22 | System.out.print("You took " + numberOfGuesses + " guesses");
23 | }
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Chapter05/src/main/java/com/headfirstjava/chapterfive/simpledotcomgame/SimpleDotComTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfive.simpledotcomgame;
2 |
3 | public class SimpleDotComTestDrive {
4 |
5 | public static void main(String[] args) {
6 | SimpleDotCom simpleDotCom = new SimpleDotCom();
7 | int[] locations = {2, 3, 4};
8 | simpleDotCom.setLocationCells(locations);
9 | String userGuess = "2";
10 | String result = simpleDotCom.checkYourself(userGuess);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/Chapter05/src/test/java/com/headfirstjava/chapterfive/simpledotcomgame/SimpleDotComTest.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfive.simpledotcomgame;
2 |
3 | public class SimpleDotComTest {
4 |
5 | public static void main(String[] args) {
6 |
7 | SimpleDotCom simpleDotCom = new SimpleDotCom();
8 |
9 | int[] locations = {2, 3, 4};
10 | simpleDotCom.setLocationCells(locations);
11 |
12 | String userGuess = "2";
13 | String result = simpleDotCom.checkYourself(userGuess);
14 | String testResult = "failed";
15 |
16 | if (result.equals("hit")) {
17 | testResult = "passed";
18 | }
19 |
20 | System.out.println(testResult);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/Chapter06/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.headfirstjava
8 | Chapter06
9 | 1.0-SNAPSHOT
10 | jar
11 |
12 |
13 |
14 |
15 | maven-compiler-plugin
16 | 3.1
17 |
18 | 1.7
19 | 1.7
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Chapter06/src/main/java/com/headfirstjava/chaptersix/arraylistmagnet/ArrayListMagnet.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptersix.arraylistmagnet;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class ArrayListMagnet {
6 |
7 | public static void main(String[] args) {
8 | ArrayList arrayList = new ArrayList<>();
9 | arrayList.add(0, "zero");
10 | arrayList.add(1, "one");
11 | arrayList.add(2, "two");
12 | arrayList.add(3, "three");
13 | printArrayList(arrayList);
14 |
15 | arrayList.remove(2);
16 | if (arrayList.contains("three")) arrayList.add("four");
17 | printArrayList(arrayList);
18 |
19 | if (arrayList.indexOf("four") != 4) arrayList.add(4, "4.2");
20 | printArrayList(arrayList);
21 |
22 | if (arrayList.contains("two")) arrayList.add("2.2");
23 | printArrayList(arrayList);
24 | }
25 |
26 | public static void printArrayList(ArrayList arrayList) {
27 | for (String element : arrayList) {
28 | System.out.print(element + " ");
29 | }
30 |
31 | System.out.println();
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Chapter06/src/main/java/com/headfirstjava/chaptersix/dotcomgame/DotCom.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptersix.dotcomgame;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class DotCom {
6 |
7 | private String name;
8 | private ArrayList locationCells;
9 |
10 | public String checkYourself(String userInput) {
11 | String result = "Miss!";
12 | int index = locationCells.indexOf(userInput);
13 |
14 | if (index >= 0) {
15 | locationCells.remove(index);
16 |
17 | if (locationCells.isEmpty()) {
18 | result = "Kill!";
19 | System.out.println("Ouch! You sunk " + name + " :(");
20 | } else {
21 | result = "Hit!";
22 | }
23 | }
24 |
25 | return result;
26 | }
27 |
28 | public ArrayList getLocationCells() {
29 | return locationCells;
30 | }
31 |
32 | public void setLocationCells(ArrayList locationCells) {
33 | this.locationCells = locationCells;
34 | }
35 |
36 | public String getName() {
37 | return name;
38 | }
39 |
40 | public void setName(String name) {
41 | this.name = name;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Chapter06/src/main/java/com/headfirstjava/chaptersix/dotcomgame/DotComBust.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptersix.dotcomgame;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class DotComBust {
6 |
7 | private GameHelper gameHelper = new GameHelper();
8 | private ArrayList dotComArrayList = new ArrayList<>();
9 | private int numberOfGuesses = 0;
10 |
11 | public static void main(String[] args) {
12 | DotComBust dotComBustGame = new DotComBust();
13 | dotComBustGame.setUpGame();
14 | dotComBustGame.startPlaying();
15 | }
16 |
17 | private void setUpGame() {
18 | DotCom dotComOne = new DotCom();
19 | dotComOne.setName("Pets.com");
20 | DotCom dotComTwo = new DotCom();
21 | dotComTwo.setName("eToys.com");
22 | DotCom dotComThree = new DotCom();
23 | dotComThree.setName("Go2.com");
24 | dotComArrayList.add(dotComOne);
25 | dotComArrayList.add(dotComTwo);
26 | dotComArrayList.add(dotComThree);
27 |
28 | System.out.println("Your goal is to sink three Dot Coms!");
29 | System.out.println(dotComOne.getName() + ", " + dotComTwo.getName() + ", " + dotComThree.getName());
30 | System.out.println("Try and sink them all in the fewest number of guesses!");
31 |
32 | for (DotCom dotComToSet : dotComArrayList) {
33 | ArrayList newLocation = gameHelper.placeDotCom(3);
34 | dotComToSet.setLocationCells(newLocation);
35 | }
36 | }
37 |
38 | private void startPlaying() {
39 | while (!dotComArrayList.isEmpty()) {
40 | String userGuess = gameHelper.getUserInput("Enter a guess: ");
41 | checkUserGuess(userGuess);
42 | }
43 |
44 | finishGame();
45 | }
46 |
47 | private void checkUserGuess(String userGuess) {
48 | numberOfGuesses++;
49 | String result = "Miss!";
50 |
51 | for (DotCom dotComToTest : dotComArrayList) {
52 | result = dotComToTest.checkYourself(userGuess);
53 |
54 | if (result.equals("Hit!")) break;
55 |
56 | if (result.equals("Kill!")) {
57 | dotComArrayList.remove(dotComToTest);
58 | break;
59 | }
60 | }
61 |
62 | System.out.println(result);
63 | }
64 |
65 | private void finishGame() {
66 | System.out.println("All Dot Coms are dead! Your stock is now worthless.");
67 |
68 | if (numberOfGuesses <= 18) {
69 | System.out.println("It only took you " + numberOfGuesses + " guesses.");
70 | System.out.println("You got out before your options sank.");
71 | } else {
72 | System.out.println("Took you long enough. " + numberOfGuesses + " guesses.");
73 | System.out.println("Fish are dancing with your options.");
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/Chapter06/src/main/java/com/headfirstjava/chaptersix/dotcomgame/GameHelper.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptersix.dotcomgame;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.IOException;
5 | import java.io.InputStreamReader;
6 | import java.util.ArrayList;
7 |
8 | public class GameHelper {
9 |
10 | private static final String alphabet = "abcdefg";
11 | private int gridLength = 7;
12 | private int gridSize = 49;
13 | private int[] grid = new int[gridSize];
14 | private int comCount = 0;
15 |
16 | public String getUserInput(String prompt) {
17 | String inputLine = null;
18 | System.out.print(prompt + " ");
19 |
20 | try {
21 | BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in));
22 | inputLine = inputStream.readLine();
23 |
24 | if (inputLine.length() == 0) return null;
25 | } catch (IOException e) {
26 | System.out.println("IOException: " + e);
27 | }
28 |
29 | return inputLine.toLowerCase();
30 | }
31 |
32 | public ArrayList placeDotCom(int dotComSize) {
33 | ArrayList alphaCells = new ArrayList<>();
34 | String[] alphaCoords = new String[dotComSize];
35 | String temp = null;
36 | int[] coords = new int[dotComSize];
37 | int attempts = 0;
38 | boolean success = false;
39 | int location = 0;
40 |
41 | comCount++;
42 | int inc = 1;
43 |
44 | if ((comCount % 2) == 1) {
45 | inc = gridLength;
46 | }
47 |
48 | while (!success && attempts++ < 200) {
49 | location = (int) (Math.random() * gridSize);
50 | int x = 0;
51 | success = true;
52 |
53 | while (success && x < dotComSize) {
54 |
55 | if (grid[location] == 0) {
56 | coords[x++] = location;
57 | location += inc;
58 |
59 | if (location >= gridSize) {
60 | success = false;
61 | }
62 |
63 | if (x > 0 && (location % gridLength == 0)) {
64 | success = false;
65 | }
66 | } else {
67 | success = false;
68 | }
69 | }
70 | }
71 |
72 | int x = 0;
73 | int row = 0;
74 | int column = 0;
75 |
76 | while (x < dotComSize) {
77 | grid[coords[x]] = 1;
78 | row = (int) (coords[x] / gridLength);
79 | column = coords[x] % gridLength;
80 | temp = String.valueOf(alphabet.charAt(column));
81 |
82 | alphaCells.add(temp.concat(Integer.toString(row)));
83 | x++;
84 | }
85 |
86 | return alphaCells;
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/Chapter07/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.headfirstjava
8 | Chapter07
9 | 1.0-SNAPSHOT
10 | jar
11 |
12 |
13 |
14 |
15 | maven-compiler-plugin
16 | 3.1
17 |
18 | 1.7
19 | 1.7
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Chapter07/src/main/java/com/headfirstjava/chapterseven/boats/Boat.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterseven.boats;
2 |
3 | public class Boat {
4 |
5 | private int length;
6 |
7 | public int getLength() {
8 | return length;
9 | }
10 |
11 | public void setLength(int len) {
12 | length = len;
13 | }
14 |
15 | public void move() {
16 | System.out.print("Drift! ");
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/Chapter07/src/main/java/com/headfirstjava/chapterseven/boats/RowBoat.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterseven.boats;
2 |
3 | public class RowBoat extends Boat {
4 |
5 | public void rowTheBoat() {
6 | System.out.print("Stroke Natasha! ");
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/Chapter07/src/main/java/com/headfirstjava/chapterseven/boats/SailBoat.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterseven.boats;
2 |
3 | public class SailBoat extends Boat {
4 |
5 | @Override
6 | public void move() {
7 | System.out.print("Hoist Sail! ");
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Chapter07/src/main/java/com/headfirstjava/chapterseven/boats/TestBoats.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterseven.boats;
2 |
3 | public class TestBoats {
4 |
5 | public static void main(String[] args) {
6 |
7 | Boat boat = new Boat();
8 | SailBoat sailBoat = new SailBoat();
9 | RowBoat rowBoat = new RowBoat();
10 |
11 | sailBoat.setLength(32);
12 | boat.move();
13 | rowBoat.move();
14 | sailBoat.move();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/Chapter07/src/main/java/com/headfirstjava/chapterseven/mixedmessages/A.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterseven.mixedmessages;
2 |
3 | class A {
4 |
5 | int ivar = 7;
6 |
7 | public void m1() {
8 | System.out.print("A's ml, ");
9 | }
10 |
11 | public void m2() {
12 | System.out.print("A's m2, ");
13 | }
14 |
15 | public void m3() {
16 | System.out.print("A's m3, ");
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/Chapter07/src/main/java/com/headfirstjava/chapterseven/mixedmessages/B.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterseven.mixedmessages;
2 |
3 | class B extends A {
4 |
5 | @Override
6 | public void m1() {
7 | System.out.print("B's m1, ");
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Chapter07/src/main/java/com/headfirstjava/chapterseven/mixedmessages/C.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterseven.mixedmessages;
2 |
3 | class C extends B {
4 |
5 | @Override
6 | public void m3() {
7 | System.out.print("C's m3, " + (ivar + 6));
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Chapter07/src/main/java/com/headfirstjava/chapterseven/mixedmessages/MixedMessages.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterseven.mixedmessages;
2 |
3 | public class MixedMessages {
4 |
5 | public static void main(String[] args) {
6 | A a = new A();
7 | B b = new B();
8 | C c = new C();
9 | A a2 = new C();
10 |
11 | b.m1();
12 | c.m2();
13 | a.m3();
14 |
15 | System.out.println();
16 |
17 | c.m1();
18 | c.m2();
19 | c.m3();
20 |
21 | System.out.println();
22 |
23 | a.m1();
24 | b.m2();
25 | c.m3();
26 |
27 | System.out.println();
28 |
29 | a2.m1();
30 | a2.m2();
31 | a2.m3();
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Chapter07/src/main/java/com/headfirstjava/chapterseven/monsters/Dragon.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterseven.monsters;
2 |
3 | public class Dragon extends Monster {
4 |
5 | boolean frighten(int degree) {
6 | System.out.println("Breath Fire!");
7 | return true;
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Chapter07/src/main/java/com/headfirstjava/chapterseven/monsters/Monster.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterseven.monsters;
2 |
3 | public class Monster {
4 |
5 | boolean frighten(int d) {
6 | System.out.println("Arrrgh!");
7 | return true;
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Chapter07/src/main/java/com/headfirstjava/chapterseven/monsters/MonsterTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterseven.monsters;
2 |
3 | public class MonsterTestDrive {
4 |
5 | public static void main(String[] args) {
6 |
7 | Monster[] monsters = new Monster[3];
8 | monsters[0] = new Vampire();
9 | monsters[1] = new Dragon();
10 | monsters[2] = new Monster();
11 |
12 | for (int x = 0; x < 3; x++) {
13 | monsters[x].frighten(x);
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/Chapter07/src/main/java/com/headfirstjava/chapterseven/monsters/Vampire.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterseven.monsters;
2 |
3 | public class Vampire extends Monster {
4 |
5 | boolean frighten(int x) {
6 | System.out.println("A bite?");
7 | return false;
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Chapter08/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.headfirstjava
8 | Chapter08
9 | 1.0-SNAPSHOT
10 | jar
11 |
12 |
13 |
14 |
15 | maven-compiler-plugin
16 | 3.1
17 |
18 | 1.7
19 | 1.7
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Chapter08/src/main/java/com/headfirstjava/chaptereight/animals/Animal.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereight.animals;
2 |
3 | public class Animal {
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/Chapter08/src/main/java/com/headfirstjava/chaptereight/animals/AnimalTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereight.animals;
2 |
3 | public class AnimalTestDrive {
4 |
5 | public static void main(String[] args) {
6 |
7 | MyAnimalList list = new MyAnimalList();
8 | Dog dog = new Dog();
9 | Cat cat = new Cat();
10 | list.add(dog);
11 | list.add(cat);
12 |
13 | System.out.println("Is Dog equal to Cat? " + dog.equals(cat));
14 |
15 | System.out.println("The hashCode of Dog is: " + cat.hashCode());
16 |
17 | System.out.println("The getClass of Cat is: " + cat.getClass());
18 |
19 | System.out.println("The toString of Dog is: " + dog.toString());
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/Chapter08/src/main/java/com/headfirstjava/chaptereight/animals/Cat.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereight.animals;
2 |
3 | public class Cat extends Animal {
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/Chapter08/src/main/java/com/headfirstjava/chaptereight/animals/Dog.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereight.animals;
2 |
3 | public class Dog extends Animal {
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/Chapter08/src/main/java/com/headfirstjava/chaptereight/animals/MyAnimalList.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereight.animals;
2 |
3 | public class MyAnimalList {
4 |
5 | private Animal[] animals = new Animal[5];
6 | private int nextIndex = 0;
7 |
8 | public void add(Animal animal) {
9 |
10 | if (nextIndex < animals.length) {
11 | animals[nextIndex] = animal;
12 | System.out.println("Animal added at: " + nextIndex);
13 | nextIndex++;
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/Chapter08/src/main/java/com/headfirstjava/chaptereight/animals/MyDogList.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereight.animals;
2 |
3 | public class MyDogList {
4 |
5 | private Dog[] dogs = new Dog[5];
6 | private int nextIndex = 0;
7 |
8 | public void add(Dog d) {
9 |
10 | if (nextIndex < dogs.length) {
11 | dogs[nextIndex] = d;
12 | System.out.println("Dog added at: " + nextIndex);
13 | nextIndex++;
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/Chapter08/src/main/java/com/headfirstjava/chaptereight/poolpuzzle/Acts.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereight.poolpuzzle;
2 |
3 | public class Acts extends Picasso {
4 |
5 | @Override
6 | public int iMethod() {
7 | return 5;
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Chapter08/src/main/java/com/headfirstjava/chaptereight/poolpuzzle/Clowns.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereight.poolpuzzle;
2 |
3 | public class Clowns extends Picasso {
4 | }
5 |
--------------------------------------------------------------------------------
/Chapter08/src/main/java/com/headfirstjava/chaptereight/poolpuzzle/Nose.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereight.poolpuzzle;
2 |
3 | public interface Nose {
4 |
5 | public int iMethod();
6 | }
7 |
--------------------------------------------------------------------------------
/Chapter08/src/main/java/com/headfirstjava/chaptereight/poolpuzzle/Of76.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereight.poolpuzzle;
2 |
3 | public class Of76 extends Clowns {
4 |
5 | public static void main(String[] args) {
6 |
7 | Nose[] noses = new Nose[3];
8 | noses[0] = new Acts();
9 | noses[1] = new Clowns();
10 | noses[2] = new Of76();
11 |
12 | for (int x = 0; x < 3; x++) {
13 | System.out.println(
14 | noses[x].iMethod()
15 | + " "
16 | + noses[x].getClass()
17 | );
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/Chapter08/src/main/java/com/headfirstjava/chaptereight/poolpuzzle/Picasso.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereight.poolpuzzle;
2 |
3 | abstract class Picasso implements Nose {
4 |
5 | @Override
6 | public int iMethod() {
7 | return 7;
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/Chapter09/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.headfirstjava
8 | Chapter09
9 | 1.0-SNAPSHOT
10 | jar
11 |
12 |
13 |
14 |
15 | maven-compiler-plugin
16 | 3.1
17 |
18 | 1.7
19 | 1.7
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Chapter09/src/main/java/com/headfirstjava/chapternine/duck/Duck.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapternine.duck;
2 |
3 | public class Duck {
4 |
5 | int size;
6 |
7 | public Duck() {
8 | System.out.println("Quack!");
9 | this.size = 10;
10 | System.out.println("Size is: " + size);
11 | }
12 |
13 | public Duck(int size) {
14 | System.out.println("Quack!");
15 | this.size = size;
16 | System.out.println("Size is: " + size);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/Chapter09/src/main/java/com/headfirstjava/chapternine/duck/MakeDuck.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapternine.duck;
2 |
3 | public class MakeDuck {
4 |
5 | public static void main(String[] args) {
6 |
7 | Duck defaultDuck = new Duck();
8 | Duck customDuck = new Duck(31);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/Chapter09/src/main/java/com/headfirstjava/chapternine/hippo/Animal.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapternine.hippo;
2 |
3 | public abstract class Animal {
4 |
5 | private String name;
6 |
7 | public Animal(String name) {
8 | this.name = name;
9 | }
10 |
11 | public String getName() {
12 | return name;
13 | }
14 |
15 | public void setName(String name) {
16 | this.name = name;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/Chapter09/src/main/java/com/headfirstjava/chapternine/hippo/Hippo.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapternine.hippo;
2 |
3 | public class Hippo extends Animal {
4 |
5 | public Hippo(String name) {
6 | super(name);
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/Chapter09/src/main/java/com/headfirstjava/chapternine/hippo/MakeHippo.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapternine.hippo;
2 |
3 | public class MakeHippo {
4 |
5 | public static void main(String[] args) {
6 |
7 | Hippo hippo = new Hippo("Buffy!");
8 | System.out.print(hippo.getName());
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/Chapter09/src/main/java/com/headfirstjava/chapternine/radiator/RetentionBot.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapternine.radiator;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class RetentionBot {
6 |
7 | public RetentionBot(ArrayList arrayList) {
8 | arrayList.add(new SimUnit("Retention"));
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/Chapter09/src/main/java/com/headfirstjava/chapternine/radiator/SimUnit.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapternine.radiator;
2 |
3 | public class SimUnit {
4 |
5 | private String botType;
6 |
7 | public SimUnit(String botType) {
8 | this.botType = botType;
9 | System.out.println("New SimUnit created: " + botType);
10 | }
11 |
12 | public int powerUse() {
13 | if ("Retention".equals(botType)) {
14 | return 2;
15 | } else {
16 | // return 4;
17 | return 3;
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/Chapter09/src/main/java/com/headfirstjava/chapternine/radiator/TestLifeSupportSim.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapternine.radiator;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class TestLifeSupportSim {
6 |
7 | public static void main(String[] args) {
8 |
9 | ArrayList arrayList = new ArrayList();
10 | V2Radiator v2Radiator = new V2Radiator(arrayList);
11 | V3Radiator v3Radiator = new V3Radiator(arrayList);
12 |
13 | for (int z = 0; z < 20; z++) {
14 | RetentionBot ret = new RetentionBot(arrayList);
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Chapter09/src/main/java/com/headfirstjava/chapternine/radiator/V2Radiator.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapternine.radiator;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class V2Radiator {
6 |
7 | public V2Radiator() {
8 |
9 | }
10 |
11 | public V2Radiator(ArrayList arrayList) {
12 |
13 | for (int x = 0; x < 5; x++) {
14 | arrayList.add(new SimUnit("V2Radiator"));
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Chapter09/src/main/java/com/headfirstjava/chapternine/radiator/V3Radiator.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapternine.radiator;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class V3Radiator extends V2Radiator {
6 |
7 | public V3Radiator(ArrayList arrayList) {
8 | // super(arrayList);
9 |
10 | for (int g = 0; g < 10; g++) {
11 | arrayList.add(new SimUnit("V3Radiator"));
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/Chapter10/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.headfirstjava
8 | Chapter10
9 | 1.0-SNAPSHOT
10 | jar
11 |
12 |
13 |
14 |
15 | maven-compiler-plugin
16 | 3.1
17 |
18 | 1.7
19 | 1.7
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Chapter10/src/main/java/com/headfirstjava/chapterten/calendar/CalendarTest.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterten.calendar;
2 |
3 | import java.util.Calendar;
4 |
5 | public class CalendarTest {
6 |
7 | public static void main(String[] args) {
8 |
9 | Calendar calendar = Calendar.getInstance();
10 | calendar.set(2004, 0, 7, 15, 40);
11 | System.out.println("Date: " + calendar.getTime());
12 |
13 | long time = calendar.getTimeInMillis();
14 | time += 1000 * 60 * 60;
15 |
16 | calendar.setTimeInMillis(time);
17 | System.out.println("Date Plus 1 Hour: " + calendar.getTime());
18 |
19 | calendar.add(calendar.DATE, 35);
20 | System.out.println("Date Plus 35 Days: " + calendar.getTime());
21 |
22 | calendar.roll(calendar.DATE, 35);
23 | System.out.println("Date Rolled 35 Days: " + calendar.getTime());
24 |
25 | calendar.set(calendar.DATE, 1);
26 | System.out.println("Date Set to the 1st: " + calendar.getTime());
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Chapter10/src/main/java/com/headfirstjava/chapterten/dateformatting/TestDateFormatting.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterten.dateformatting;
2 |
3 | import java.util.Date;
4 |
5 | public class TestDateFormatting {
6 |
7 | public static void main(String[] args) {
8 |
9 | String stringOne = String.format("%tc", new Date());
10 | System.out.println(stringOne);
11 |
12 | String stringTwo = String.format("%tr", new Date());
13 | System.out.println(stringTwo);
14 |
15 | Date today = new Date();
16 |
17 | String stringThree = String.format("%tA, %tB %td", today, today, today);
18 | System.out.println(stringThree);
19 |
20 | String stringFour = String.format("%tA, %
2 |
5 | 4.0.0
6 |
7 | com.headfirstjava
8 | Chapter11
9 | 1.0-SNAPSHOT
10 | jar
11 |
12 |
13 |
14 |
15 | maven-compiler-plugin
16 | 3.1
17 |
18 | 1.7
19 | 1.7
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Chapter11/src/main/java/com/headfirstjava/chaptereleven/myex/ExTestDrive.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereleven.myex;
2 |
3 | public class ExTestDrive {
4 |
5 | public static void main(String[] args) {
6 |
7 | String test = "No";
8 |
9 | try {
10 | System.out.print("T");
11 | doRisky(test);
12 | } catch (MyEx e) {
13 | System.out.print("a");
14 | } finally {
15 | System.out.print("w");
16 | System.out.print("s");
17 | }
18 | }
19 |
20 | private static void doRisky(String t) throws MyEx {
21 | System.out.print("h");
22 | if ("Yes".equals(t)) throw new MyEx();
23 | System.out.print("r");
24 | System.out.print("o");
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Chapter11/src/main/java/com/headfirstjava/chaptereleven/myex/MyEx.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereleven.myex;
2 |
3 | public class MyEx extends Exception {
4 | }
5 |
6 |
--------------------------------------------------------------------------------
/Chapter11/src/main/java/com/headfirstjava/chaptereleven/soundplayerapp/MiniMiniMusicApp.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereleven.soundplayerapp;
2 |
3 | import javax.sound.midi.*;
4 |
5 | public class MiniMiniMusicApp {
6 |
7 | public static void main(String[] args) {
8 | MiniMiniMusicApp miniMiniMusicApp = new MiniMiniMusicApp();
9 | miniMiniMusicApp.play();
10 | }
11 |
12 | private void play() {
13 |
14 | try {
15 | Sequencer sequencer = MidiSystem.getSequencer();
16 | sequencer.open();
17 |
18 | Sequence sequence = new Sequence(Sequence.PPQ, 4);
19 |
20 | Track track = sequence.createTrack();
21 |
22 | ShortMessage shortMessageA = new ShortMessage();
23 | shortMessageA.setMessage(144, 1, 44, 100);
24 |
25 | MidiEvent noteOn = new MidiEvent(shortMessageA, 1);
26 | track.add(noteOn);
27 |
28 | ShortMessage shortMessageB = new ShortMessage();
29 | shortMessageB.setMessage(128, 1, 44, 100);
30 |
31 | MidiEvent noteOff = new MidiEvent(shortMessageB, 16);
32 | track.add(noteOff);
33 |
34 | sequencer.setSequence(sequence);
35 | sequencer.start();
36 | } catch (Exception e) {
37 | e.printStackTrace();
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/Chapter11/src/main/java/com/headfirstjava/chaptereleven/soundplayerapp/MiniMusicCmdLine.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereleven.soundplayerapp;
2 |
3 | import javax.sound.midi.*;
4 |
5 | public class MiniMusicCmdLine {
6 |
7 | public static void main(String[] args) {
8 |
9 | MiniMusicCmdLine miniMusicCmdLine = new MiniMusicCmdLine();
10 | miniMusicCmdLine.play(40, 70);
11 |
12 | /* if (args.length < 2) {
13 | System.out.println("Don't forget the instrument and note arguments!");
14 | } else {
15 | int instrument = Integer.parseInt(args[0]);
16 | int note = Integer.parseInt(args[1]);
17 | miniMusicCmdLine.play(instrument, note);
18 | } */
19 | }
20 |
21 | private void play(int instrument, int note) {
22 |
23 | try {
24 | Sequencer sequencer = MidiSystem.getSequencer();
25 | sequencer.open();
26 |
27 | Sequence sequence = new Sequence(Sequence.PPQ, 4);
28 | Track track = sequence.createTrack();
29 |
30 | ShortMessage firstMessage = new ShortMessage();
31 | firstMessage.setMessage(192, 1, instrument, 0);
32 |
33 | MidiEvent changeInstrument = new MidiEvent(firstMessage, 1);
34 | track.add(changeInstrument);
35 |
36 | ShortMessage shortMessageA = new ShortMessage();
37 | shortMessageA.setMessage(144, 1, note, 100);
38 |
39 | MidiEvent noteOn = new MidiEvent(shortMessageA, 1);
40 | track.add(noteOn);
41 |
42 | ShortMessage shortMessageB = new ShortMessage();
43 | shortMessageB.setMessage(128, 1, note, 100);
44 |
45 | MidiEvent noteOff = new MidiEvent(shortMessageB, 16);
46 | track.add(noteOff);
47 |
48 | sequencer.setSequence(sequence);
49 | sequencer.start();
50 | } catch (Exception e) {
51 | e.printStackTrace();
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/Chapter11/src/main/java/com/headfirstjava/chaptereleven/testexceptions/ScaryException.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereleven.testexceptions;
2 |
3 | public class ScaryException extends Throwable {
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/Chapter11/src/main/java/com/headfirstjava/chaptereleven/testexceptions/TestExceptions.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereleven.testexceptions;
2 |
3 | public class TestExceptions {
4 |
5 | public static void main(String[] args) {
6 |
7 | String testString = "Yes";
8 |
9 | try {
10 | System.out.println("Start try!");
11 | doRisky(testString);
12 | System.out.println("End try!");
13 | } catch (ScaryException scaryException) {
14 | System.out.println("Scary exception!");
15 | } finally {
16 | System.out.println("Finally!");
17 | }
18 |
19 | System.out.println("End of main!");
20 | }
21 |
22 | private static void doRisky(String test) throws ScaryException {
23 | System.out.println("Start risky method!");
24 |
25 | if ("Yes".equals(test)) {
26 | throw new ScaryException();
27 | }
28 |
29 | System.out.println("End risky!");
30 | // return;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Chapter11/src/main/java/com/headfirstjava/chaptereleven/testmusic/MusicTest.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptereleven.testmusic;
2 |
3 | import javax.sound.midi.MidiSystem;
4 | import javax.sound.midi.MidiUnavailableException;
5 | import javax.sound.midi.Sequencer;
6 |
7 | public class MusicTest {
8 |
9 | public static void main(String[] args) {
10 | MusicTest musicTest = new MusicTest();
11 | musicTest.play();
12 | }
13 |
14 | public void play() {
15 | try {
16 | Sequencer sequencer = MidiSystem.getSequencer();
17 | System.out.println("Successfully got a sequencer!");
18 | } catch (MidiUnavailableException m) {
19 | System.out.println("Did not get a sequencer!");
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/Chapter14/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.headfirstjava
8 | Chapter14
9 | 1.0-SNAPSHOT
10 | jar
11 |
12 |
13 |
14 |
15 | maven-compiler-plugin
16 | 3.1
17 |
18 | 1.7
19 | 1.7
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/Chapter14/src/main/java/com/headfirstjava/chapterfourteen/dungeongame/DungeonGame.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfourteen.dungeongame;
2 |
3 | import java.io.Serializable;
4 |
5 | class DungeonGame implements Serializable {
6 | public int x = 3;
7 | transient long y = 4;
8 | private short z = 5;
9 |
10 | int getX() {
11 | return x;
12 | }
13 |
14 | long getY() {
15 | return y;
16 | }
17 |
18 | short getZ() {
19 | return z;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/Chapter14/src/main/java/com/headfirstjava/chapterfourteen/dungeongame/DungeonTest.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfourteen.dungeongame;
2 |
3 | import java.io.FileInputStream;
4 | import java.io.FileOutputStream;
5 | import java.io.ObjectInputStream;
6 | import java.io.ObjectOutputStream;
7 |
8 | public class DungeonTest {
9 | public static void main(String[] args) {
10 | DungeonGame d = new DungeonGame();
11 | System.out.println(d.getX() + d.getY() + d.getZ());
12 | try {
13 | FileOutputStream fos = new FileOutputStream("dg.ser");
14 | ObjectOutputStream oos = new ObjectOutputStream(fos);
15 | oos.writeObject(d);
16 | oos.close();
17 | FileInputStream fis = new FileInputStream("dg.ser");
18 | ObjectInputStream ois = new ObjectInputStream(fis);
19 | d = (DungeonGame) ois.readObject();
20 | ois.close();
21 | } catch (Exception e) {
22 | e.printStackTrace();
23 | }
24 | System.out.println(d.getX() + d.getY() + d.getZ());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Chapter14/src/main/java/com/headfirstjava/chapterfourteen/gamesavertest/GameCharacter.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfourteen.gamesavertest;
2 |
3 | import java.io.Serializable;
4 |
5 | public class GameCharacter implements Serializable {
6 |
7 | int power;
8 | String type;
9 | String[] weapons;
10 |
11 | public GameCharacter(int p, String t, String[] w) {
12 | power = p;
13 | type = t;
14 | weapons = w;
15 | }
16 |
17 | public String getType() {
18 | return type;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/Chapter14/src/main/java/com/headfirstjava/chapterfourteen/gamesavertest/GameSaverTest.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfourteen.gamesavertest;
2 |
3 | import java.io.*;
4 |
5 | public class GameSaverTest {
6 |
7 | public static void main(String[] args) {
8 | GameCharacter one = new GameCharacter(50, "Elf", new String[]{"bow", "sword", "dust"});
9 | GameCharacter two = new GameCharacter(200, "Troll", new String[]{"bare hands", "big axe"});
10 | GameCharacter three = new GameCharacter(120, "Magician", new String[]{"spells", "invisibility"});
11 |
12 | try {
13 | ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("Game.ser"));
14 | os.writeObject(one);
15 | os.writeObject(two);
16 | os.writeObject(three);
17 | os.close();
18 | } catch (IOException ex) {
19 | ex.printStackTrace();
20 | }
21 |
22 | try {
23 | ObjectInputStream is = new ObjectInputStream(new FileInputStream("Game.ser"));
24 | GameCharacter oneRestore = (GameCharacter) is.readObject();
25 | GameCharacter twoRestore = (GameCharacter) is.readObject();
26 | GameCharacter threeRestore = (GameCharacter) is.readObject();
27 |
28 | System.out.println("One's type: " + oneRestore.getType());
29 | System.out.println("Two's type: " + twoRestore.getType());
30 | System.out.println("Three's type: " + threeRestore.getType());
31 | } catch (Exception ex) {
32 | ex.printStackTrace();
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Chapter14/src/main/java/com/headfirstjava/chapterfourteen/quizcard/QuizCard.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfourteen.quizcard;
2 |
3 | import java.io.Serializable;
4 |
5 | public class QuizCard implements Serializable {
6 |
7 | private String uniqueID;
8 | private String category;
9 | private String question;
10 | private String answer;
11 | private String hint;
12 |
13 | public QuizCard(String q, String a) {
14 | question = q;
15 | answer = a;
16 | }
17 |
18 | public String getUniqueID() {
19 | return uniqueID;
20 | }
21 |
22 | public void setUniqueID(String id) {
23 | uniqueID = id;
24 | }
25 |
26 | public String getCategory() {
27 | return category;
28 | }
29 |
30 | public void setCategory(String c) {
31 | category = c;
32 | }
33 |
34 | public String getQuestion() {
35 | return question;
36 | }
37 |
38 | public void setQuestion(String q) {
39 | question = q;
40 | }
41 |
42 | public String getAnswer() {
43 | return answer;
44 | }
45 |
46 | public void setAnswer(String a) {
47 | answer = a;
48 | }
49 |
50 | public String getHint() {
51 | return hint;
52 | }
53 |
54 | public void setHint(String h) {
55 | hint = h;
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/Chapter14/src/main/java/com/headfirstjava/chapterfourteen/quizcard/QuizCardBuilder.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfourteen.quizcard;
2 |
3 | import javax.swing.*;
4 | import java.awt.*;
5 | import java.awt.event.ActionEvent;
6 | import java.awt.event.ActionListener;
7 | import java.io.BufferedWriter;
8 | import java.io.File;
9 | import java.io.FileWriter;
10 | import java.io.IOException;
11 | import java.util.ArrayList;
12 | import java.util.Iterator;
13 |
14 | public class QuizCardBuilder {
15 |
16 | private JTextArea question;
17 | private JTextArea answer;
18 | private ArrayList cardList;
19 | private JFrame frame;
20 |
21 | public static void main(String[] args) {
22 | QuizCardBuilder builder = new QuizCardBuilder();
23 | builder.go();
24 | }
25 |
26 | public void go() {
27 | frame = new JFrame("Quiz Card Builder");
28 | frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
29 | JPanel mainPanel = new JPanel();
30 | Font bigFont = new Font("sanserif", Font.BOLD, 24);
31 | question = new JTextArea(6, 20);
32 | question.setLineWrap(true);
33 | question.setWrapStyleWord(true);
34 | question.setFont(bigFont);
35 |
36 | JScrollPane qScroller = new JScrollPane(question);
37 | qScroller.setVerticalScrollBarPolicy(
38 | ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
39 | qScroller.setHorizontalScrollBarPolicy(
40 | ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
41 |
42 | answer = new JTextArea(6, 20);
43 | answer.setLineWrap(true);
44 | answer.setWrapStyleWord(true);
45 | answer.setFont(bigFont);
46 |
47 | JScrollPane aScroller = new JScrollPane(answer);
48 | aScroller.setVerticalScrollBarPolicy(
49 | ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
50 | aScroller.setHorizontalScrollBarPolicy(
51 | ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
52 |
53 | JButton nextButton = new JButton("Next Card");
54 | cardList = new ArrayList();
55 | JLabel qLabel = new JLabel("Question:");
56 | JLabel aLabel = new JLabel("Answer:");
57 |
58 | mainPanel.add(qLabel);
59 | mainPanel.add(qScroller);
60 | mainPanel.add(aLabel);
61 | mainPanel.add(aScroller);
62 | mainPanel.add(nextButton);
63 | nextButton.addActionListener(new NextCardListener());
64 | JMenuBar menuBar = new JMenuBar();
65 | JMenu fileMenu = new JMenu("File");
66 | JMenuItem newMenuItem = new JMenuItem("New");
67 |
68 | JMenuItem saveMenuItem = new JMenuItem("Save");
69 | newMenuItem.addActionListener(new NewMenuListener());
70 | saveMenuItem.addActionListener(new SaveMenuListener());
71 |
72 | fileMenu.add(newMenuItem);
73 | fileMenu.add(saveMenuItem);
74 | menuBar.add(fileMenu);
75 | frame.setJMenuBar(menuBar);
76 |
77 | frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
78 | frame.setSize(500, 600);
79 | frame.setVisible(true);
80 | }
81 |
82 | private void clearCard() {
83 | question.setText("");
84 | answer.setText("");
85 | question.requestFocus();
86 | }
87 |
88 | private void saveFile(File file) {
89 |
90 | try {
91 | BufferedWriter writer = new BufferedWriter(new FileWriter(file));
92 | Iterator cardIterator = cardList.iterator();
93 | while (cardIterator.hasNext()) {
94 | QuizCard card = (QuizCard) cardIterator.next();
95 | writer.write(card.getQuestion() + "/");
96 | writer.write(card.getAnswer() + "\n");
97 | }
98 | writer.close();
99 | } catch (IOException ex) {
100 | System.out.println("couldn't write the cardList out");
101 | ex.printStackTrace();
102 | }
103 | }
104 |
105 | public class NextCardListener implements ActionListener {
106 | public void actionPerformed(ActionEvent ev) {
107 | QuizCard card = new QuizCard(question.getText(), answer.getText());
108 | cardList.add(card);
109 | clearCard();
110 | }
111 | }
112 |
113 | public class SaveMenuListener implements ActionListener {
114 | public void actionPerformed(ActionEvent ev) {
115 | QuizCard card = new QuizCard(question.getText(), answer.getText());
116 | cardList.add(card);
117 |
118 | JFileChooser fileSave = new JFileChooser();
119 | fileSave.showSaveDialog(frame);
120 | saveFile(fileSave.getSelectedFile());
121 | }
122 | }
123 |
124 | public class NewMenuListener implements ActionListener {
125 | public void actionPerformed(ActionEvent ev) {
126 | cardList.clear();
127 | clearCard();
128 | }
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/Chapter14/src/main/java/com/headfirstjava/chapterfourteen/quizcard/QuizCardReader.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chapterfourteen.quizcard;
2 |
3 | import javax.swing.*;
4 | import java.awt.*;
5 | import java.awt.event.ActionEvent;
6 | import java.awt.event.ActionListener;
7 | import java.io.BufferedReader;
8 | import java.io.File;
9 | import java.io.FileReader;
10 | import java.util.ArrayList;
11 | import java.util.Iterator;
12 | import java.util.StringTokenizer;
13 |
14 | public class QuizCardReader {
15 |
16 | private JTextArea display;
17 | private ArrayList cardList;
18 | private QuizCard currentCard;
19 | private Iterator cardIterator;
20 | private JFrame frame;
21 | private JButton nextButton;
22 | private boolean isShowAnswer;
23 |
24 | public static void main(String[] args) {
25 | QuizCardReader qReader = new QuizCardReader();
26 | qReader.go();
27 | }
28 |
29 | public void go() {
30 | frame = new JFrame("Quiz Card Player");
31 | frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
32 | JPanel mainPanel = new JPanel();
33 | Font bigFont = new Font("sanserif", Font.BOLD, 24);
34 |
35 | display = new JTextArea(9, 20);
36 | display.setFont(bigFont);
37 | display.setLineWrap(true);
38 | display.setWrapStyleWord(true);
39 | display.setEditable(false);
40 |
41 | JScrollPane qScroller = new JScrollPane(display);
42 | qScroller.setVerticalScrollBarPolicy(
43 | ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
44 | qScroller.setHorizontalScrollBarPolicy(
45 | ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
46 |
47 | nextButton = new JButton("Show Question");
48 |
49 | mainPanel.add(qScroller);
50 | mainPanel.add(nextButton);
51 | nextButton.addActionListener(new NextCardListener());
52 | JMenuBar menuBar = new JMenuBar();
53 | JMenu fileMenu = new JMenu("File");
54 |
55 | JMenuItem loadMenuItem = new JMenuItem("Load card set");
56 |
57 | loadMenuItem.addActionListener(new OpenMenuListener());
58 |
59 | fileMenu.add(loadMenuItem);
60 |
61 | menuBar.add(fileMenu);
62 | frame.setJMenuBar(menuBar);
63 |
64 | frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
65 | frame.setSize(500, 600);
66 | frame.setVisible(true);
67 | }
68 |
69 | private void loadFile(File file) {
70 | cardList = new ArrayList();
71 | try {
72 | BufferedReader reader = new BufferedReader(new FileReader(file));
73 | String line;
74 | while ((line = reader.readLine()) != null) {
75 | makeCard(line);
76 | }
77 | reader.close();
78 |
79 | } catch (Exception ex) {
80 | System.out.println("couldn't read the card file");
81 | ex.printStackTrace();
82 | }
83 |
84 | cardIterator = cardList.iterator();
85 | showNextCard();
86 | }
87 |
88 | private void makeCard(String lineToParse) {
89 |
90 | StringTokenizer parser = new StringTokenizer(lineToParse, "/");
91 | if (parser.hasMoreTokens()) {
92 | QuizCard card = new QuizCard(parser.nextToken(), parser.nextToken());
93 | cardList.add(card);
94 | }
95 | }
96 |
97 | private void showNextCard() {
98 | currentCard = (QuizCard) cardIterator.next();
99 | display.setText(currentCard.getQuestion());
100 | nextButton.setText("Show Answer");
101 | isShowAnswer = true;
102 | }
103 |
104 | public class NextCardListener implements ActionListener {
105 | public void actionPerformed(ActionEvent ev) {
106 | if (isShowAnswer) {
107 | display.setText(currentCard.getAnswer());
108 | nextButton.setText("Next Card");
109 | isShowAnswer = false;
110 | } else {
111 | if (cardIterator.hasNext()) {
112 |
113 | showNextCard();
114 |
115 | } else {
116 | display.setText("That was last card");
117 | nextButton.disable();
118 | }
119 | }
120 | }
121 | }
122 |
123 | public class OpenMenuListener implements ActionListener {
124 | public void actionPerformed(ActionEvent ev) {
125 | JFileChooser fileOpen = new JFileChooser();
126 | fileOpen.showOpenDialog(frame);
127 | loadFile(fileOpen.getSelectedFile());
128 | }
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/Chapter16/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.headfirstjava
8 | Chapter16
9 | 1.0-SNAPSHOT
10 | jar
11 |
12 |
13 |
14 |
15 | maven-compiler-plugin
16 | 3.1
17 |
18 | 1.7
19 | 1.7
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/Chapter16/src/main/java/com/headfirstjava/chaptersixteen/jukebox/JukeBox1.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptersixteen.jukebox;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.File;
5 | import java.io.FileReader;
6 | import java.util.ArrayList;
7 |
8 | public class Jukebox1 {
9 |
10 | ArrayList songList = new ArrayList();
11 |
12 | public static void main(String[] args) {
13 | new Jukebox1().go();
14 | }
15 |
16 | public void go() {
17 | getSongs();
18 | System.out.println(songList);
19 | }
20 |
21 | void getSongs() {
22 | try {
23 | File file = new File("Chapter16/src/main/java/com/headfirstjava/chaptersixteen/jukebox/SongList.txt");
24 | BufferedReader reader = new BufferedReader(new FileReader(file));
25 | String line;
26 | while ((line = reader.readLine()) != null) {
27 | addSong(line);
28 | }
29 | } catch (Exception ex) {
30 | ex.printStackTrace();
31 | }
32 | }
33 |
34 | void addSong(String lineToParse) {
35 | String[] tokens = lineToParse.split("/");
36 | songList.add(tokens[0]);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/Chapter16/src/main/java/com/headfirstjava/chaptersixteen/jukebox/JukeBox3.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptersixteen.jukebox;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.File;
5 | import java.io.FileReader;
6 | import java.util.ArrayList;
7 | import java.util.Collections;
8 |
9 | public class Jukebox3 {
10 | ArrayList songList = new ArrayList();
11 |
12 | public static void main(String[] args) {
13 | new Jukebox3().go();
14 | }
15 |
16 | public void go() {
17 | getSongs();
18 | System.out.println(songList);
19 | Collections.sort(songList);
20 | System.out.println(songList);
21 | }
22 |
23 | void getSongs() {
24 | try {
25 | File file = new File("Chapter16/src/main/java/com/headfirstjava/chaptersixteen/jukebox/SongList.txt");
26 | BufferedReader reader = new BufferedReader(new FileReader(file));
27 | String line;
28 | while ((line = reader.readLine()) != null) {
29 | addSong(line);
30 | }
31 | } catch (Exception ex) {
32 | ex.printStackTrace();
33 | }
34 | }
35 |
36 | void addSong(String lineToParse) {
37 | String[] tokens = lineToParse.split("/");
38 | Song nextSong = new Song(tokens[0], tokens[1], tokens[2], tokens[3]);
39 | songList.add(nextSong);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Chapter16/src/main/java/com/headfirstjava/chaptersixteen/jukebox/JukeBox5.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptersixteen.jukebox;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.File;
5 | import java.io.FileReader;
6 | import java.util.ArrayList;
7 | import java.util.Collections;
8 | import java.util.Comparator;
9 |
10 | public class Jukebox5 {
11 | ArrayList songList = new ArrayList();
12 |
13 | public static void main(String[] args) {
14 | new Jukebox5().go();
15 | }
16 |
17 | public void go() {
18 | getSongs();
19 | System.out.println(songList);
20 | Collections.sort(songList);
21 | System.out.println(songList);
22 |
23 | ArtistCompare artistCompare = new ArtistCompare();
24 | Collections.sort(songList, artistCompare);
25 |
26 | System.out.println(songList);
27 | }
28 |
29 | void getSongs() {
30 | try {
31 | File file = new File("Chapter16/src/main/java/com/headfirstjava/chaptersixteen/jukebox/SongList.txt");
32 | BufferedReader reader = new BufferedReader(new FileReader(file));
33 | String line;
34 | while ((line = reader.readLine()) != null) {
35 | addSong(line);
36 | }
37 | } catch (Exception ex) {
38 | ex.printStackTrace();
39 | }
40 | }
41 |
42 | void addSong(String lineToParse) {
43 | String[] tokens = lineToParse.split("/");
44 | Song nextSong = new Song(tokens[0], tokens[1], tokens[2], tokens[3]);
45 | songList.add(nextSong);
46 | }
47 |
48 | class ArtistCompare implements Comparator {
49 | public int compare(Song one, Song two) {
50 | return one.getArtist().compareTo(two.getArtist());
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/Chapter16/src/main/java/com/headfirstjava/chaptersixteen/jukebox/JukeBox6.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptersixteen.jukebox;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.File;
5 | import java.io.FileReader;
6 | import java.util.ArrayList;
7 | import java.util.Collections;
8 | import java.util.HashSet;
9 |
10 | public class Jukebox6 {
11 | ArrayList songList = new ArrayList();
12 |
13 | public static void main(String[] args) {
14 | new Jukebox6().go();
15 | }
16 |
17 | public void go() {
18 | getSongs();
19 | System.out.println(songList);
20 | Collections.sort(songList);
21 | System.out.println(songList);
22 |
23 | HashSet songSet = new HashSet();
24 | songSet.addAll(songList);
25 | System.out.println(songSet);
26 | }
27 |
28 | void getSongs() {
29 | try {
30 | File file = new File("Chapter16/src/main/java/com/headfirstjava/chaptersixteen/jukebox/SongList.txt");
31 | BufferedReader reader = new BufferedReader(new FileReader(file));
32 | String line;
33 | while ((line = reader.readLine()) != null) {
34 | addSong(line);
35 | }
36 | } catch (Exception ex) {
37 | ex.printStackTrace();
38 | }
39 | }
40 |
41 | void addSong(String lineToParse) {
42 | String[] tokens = lineToParse.split("/");
43 | SongBad nextSong = new SongBad(tokens[0], tokens[1], tokens[2], tokens[3]);
44 | songList.add(nextSong);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/Chapter16/src/main/java/com/headfirstjava/chaptersixteen/jukebox/JukeBox8.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptersixteen.jukebox;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.File;
5 | import java.io.FileReader;
6 | import java.util.ArrayList;
7 | import java.util.Collections;
8 | import java.util.TreeSet;
9 |
10 | public class Jukebox8 {
11 |
12 | ArrayList songList = new ArrayList();
13 |
14 | public static void main(String[] args) {
15 | new Jukebox8().go();
16 | }
17 |
18 | public void go() {
19 | getSongs();
20 | System.out.println(songList);
21 | Collections.sort(songList);
22 | System.out.println(songList);
23 |
24 | TreeSet songSet = new TreeSet();
25 | songSet.addAll(songList);
26 | System.out.println(songSet);
27 | }
28 |
29 | void getSongs() {
30 | try {
31 | File file = new File("Chapter16/src/main/java/com/headfirstjava/chaptersixteen/jukebox/SongList.txt");
32 | BufferedReader reader = new BufferedReader(new FileReader(file));
33 | String line = null;
34 | while ((line = reader.readLine()) != null) {
35 | addSong(line);
36 | }
37 | } catch (Exception ex) {
38 | ex.printStackTrace();
39 | }
40 | }
41 |
42 | void addSong(String lineToParse) {
43 | String[] tokens = lineToParse.split("/");
44 | Song nextSong = new Song(tokens[0], tokens[1], tokens[2], tokens[3]);
45 | songList.add(nextSong);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/Chapter16/src/main/java/com/headfirstjava/chaptersixteen/jukebox/Song.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptersixteen.jukebox;
2 |
3 | public class Song implements Comparable {
4 |
5 | String title;
6 | String artist;
7 | String rating;
8 | String bpm;
9 |
10 | public Song(String t, String a, String r, String b) {
11 | title = t;
12 | artist = a;
13 | rating = r;
14 | bpm = b;
15 | }
16 |
17 | public boolean equals(Object aSong) {
18 | Song s = (Song) aSong;
19 | return getTitle().equals(s.getTitle());
20 | }
21 |
22 | public int hashCode() {
23 | return title.hashCode();
24 | }
25 |
26 | public int compareTo(Song s) {
27 | return title.compareTo(s.getTitle());
28 | }
29 |
30 | public String getArtist() {
31 | return artist;
32 | }
33 |
34 | public String getBpm() {
35 | return bpm;
36 | }
37 |
38 | public String getRating() {
39 | return rating;
40 | }
41 |
42 | public String getTitle() {
43 | return title;
44 | }
45 |
46 | public String toString() {
47 | return title;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/Chapter16/src/main/java/com/headfirstjava/chaptersixteen/jukebox/SongBad.java:
--------------------------------------------------------------------------------
1 | package com.headfirstjava.chaptersixteen.jukebox;
2 |
3 | class SongBad implements Comparable {
4 |
5 | String title;
6 | String artist;
7 | String rating;
8 | String bpm;
9 |
10 | public SongBad(String t, String a, String r, String b) {
11 | title = t;
12 | artist = a;
13 | rating = r;
14 | bpm = b;
15 | }
16 |
17 | public boolean equals(Object aSong) {
18 | SongBad s = (SongBad) aSong;
19 | return getTitle().equals(s.getTitle());
20 | }
21 |
22 | public int compareTo(SongBad s) {
23 | return title.compareTo(s.getTitle());
24 | }
25 |
26 | public String getArtist() {
27 | return artist;
28 | }
29 |
30 | public String getBpm() {
31 | return bpm;
32 | }
33 |
34 | public String getRating() {
35 | return rating;
36 | }
37 |
38 | public String getTitle() {
39 | return title;
40 | }
41 |
42 | public String toString() {
43 | return title;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/Chapter16/src/main/java/com/headfirstjava/chaptersixteen/jukebox/SongList.txt:
--------------------------------------------------------------------------------
1 | Pink Moon/Nick Drake/5/140
2 | Somersault/Zero 7/4/130
3 | Shiva Moon/Prem/3/120
4 | Joshua Circles/BT/3/110
5 | Deep Channel/Afro/3/120
6 | Celts Passenger/Headmix/4/130
7 | Listen/Tahiti 80/5/140
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Head First Java, 2nd Edition - Book exercises.
2 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.headfirstjava
8 | HeadFirstJava
9 | 1.0-SNAPSHOT
10 | pom
11 |
12 |
13 | UTF-8
14 | UTF-8
15 |
16 |
17 |
18 | Chapter01
19 | Chapter02
20 | Chapter03
21 | Chapter04
22 | Chapter05
23 | Chapter06
24 | Chapter07
25 | Chapter08
26 | Chapter09
27 | Chapter10
28 | Chapter11
29 | Chapter14
30 | Chapter16
31 |
32 |
33 |
--------------------------------------------------------------------------------