diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..75f510f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+
+.idea/workspace.xml
+.idea/compiler.xml
diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml
new file mode 100644
index 0000000..e96534f
--- /dev/null
+++ b/.idea/uiDesigner.xml
@@ -0,0 +1,124 @@
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 28b67ac..c266c95 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -1,10 +1,19 @@
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
@@ -14,25 +23,65 @@
+
+
+
-
-
-
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
-
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -50,11 +99,6 @@
-
-
@@ -91,8 +151,6 @@
-
-
@@ -105,15 +163,24 @@
+
+
+
+
+
+
+
+
+
@@ -127,7 +194,7 @@
-
+
@@ -149,6 +216,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -223,9 +304,14 @@
+
+
+
+
-
+
+
@@ -237,64 +323,209 @@
-
- 1541095190663
+
+ 1543423815013
- 1541095190663
+ 1543423815013
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
-
+
+
-
-
-
-
-
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -302,13 +533,131 @@
-
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SongList_C16.txt b/SongList_C16.txt
new file mode 100644
index 0000000..968fd10
--- /dev/null
+++ b/SongList_C16.txt
@@ -0,0 +1,10 @@
+Pink Moon/Nick Drake/5/80
+Somersault/Zero 7/4/84
+Shiva Moon/Prem Joshua/6/120
+Circles/BT/5/110
+Deep Channel/Afro Celts/4/120
+Passenger/Headmix/4/100
+Listen/Tahiti 80/5/90
+Listen/Tahiti 80/5/90
+Listen/Tahiti 80/5/90
+Circles/BT/5/110
diff --git a/out/production/HeadFirstJavaExamples/CHAPTER_14/ReadAFile.class b/out/production/HeadFirstJavaExamples/CHAPTER_14/ReadAFile.class
new file mode 100644
index 0000000..2666f91
Binary files /dev/null and b/out/production/HeadFirstJavaExamples/CHAPTER_14/ReadAFile.class differ
diff --git a/out/production/HeadFirstJavaExamples/CHAPTER_15/DailyAdviceClient.class b/out/production/HeadFirstJavaExamples/CHAPTER_15/DailyAdviceClient.class
new file mode 100644
index 0000000..ffe5654
Binary files /dev/null and b/out/production/HeadFirstJavaExamples/CHAPTER_15/DailyAdviceClient.class differ
diff --git a/out/production/HeadFirstJavaExamples/CHAPTER_15/DailyAdviceServer.class b/out/production/HeadFirstJavaExamples/CHAPTER_15/DailyAdviceServer.class
new file mode 100644
index 0000000..4986d0f
Binary files /dev/null and b/out/production/HeadFirstJavaExamples/CHAPTER_15/DailyAdviceServer.class differ
diff --git a/out/production/HeadFirstJavaExamples/CHAPTER_15/SimpleChatClientA$IncomingReader.class b/out/production/HeadFirstJavaExamples/CHAPTER_15/SimpleChatClientA$IncomingReader.class
new file mode 100644
index 0000000..1da7d6a
Binary files /dev/null and b/out/production/HeadFirstJavaExamples/CHAPTER_15/SimpleChatClientA$IncomingReader.class differ
diff --git a/out/production/HeadFirstJavaExamples/CHAPTER_15/SimpleChatClientA$SendButtonListener.class b/out/production/HeadFirstJavaExamples/CHAPTER_15/SimpleChatClientA$SendButtonListener.class
new file mode 100644
index 0000000..1dae32f
Binary files /dev/null and b/out/production/HeadFirstJavaExamples/CHAPTER_15/SimpleChatClientA$SendButtonListener.class differ
diff --git a/out/production/HeadFirstJavaExamples/CHAPTER_15/SimpleChatClientA.class b/out/production/HeadFirstJavaExamples/CHAPTER_15/SimpleChatClientA.class
new file mode 100644
index 0000000..18b11a7
Binary files /dev/null and b/out/production/HeadFirstJavaExamples/CHAPTER_15/SimpleChatClientA.class differ
diff --git a/out/production/HeadFirstJavaExamples/CHAPTER_15/VerySimpleChatServer$ClientHanlder.class b/out/production/HeadFirstJavaExamples/CHAPTER_15/VerySimpleChatServer$ClientHanlder.class
new file mode 100644
index 0000000..55ea2a0
Binary files /dev/null and b/out/production/HeadFirstJavaExamples/CHAPTER_15/VerySimpleChatServer$ClientHanlder.class differ
diff --git a/out/production/HeadFirstJavaExamples/CHAPTER_15/VerySimpleChatServer.class b/out/production/HeadFirstJavaExamples/CHAPTER_15/VerySimpleChatServer.class
new file mode 100644
index 0000000..986e6ff
Binary files /dev/null and b/out/production/HeadFirstJavaExamples/CHAPTER_15/VerySimpleChatServer.class differ
diff --git a/out/production/HeadFirstJavaExamples/CHAPTER_16/Jukebox$ArtistCompare.class b/out/production/HeadFirstJavaExamples/CHAPTER_16/Jukebox$ArtistCompare.class
new file mode 100644
index 0000000..3310491
Binary files /dev/null and b/out/production/HeadFirstJavaExamples/CHAPTER_16/Jukebox$ArtistCompare.class differ
diff --git a/out/production/HeadFirstJavaExamples/CHAPTER_16/Jukebox.class b/out/production/HeadFirstJavaExamples/CHAPTER_16/Jukebox.class
new file mode 100644
index 0000000..687317f
Binary files /dev/null and b/out/production/HeadFirstJavaExamples/CHAPTER_16/Jukebox.class differ
diff --git a/out/production/HeadFirstJavaExamples/CHAPTER_16/Song.class b/out/production/HeadFirstJavaExamples/CHAPTER_16/Song.class
new file mode 100644
index 0000000..9ab9de6
Binary files /dev/null and b/out/production/HeadFirstJavaExamples/CHAPTER_16/Song.class differ
diff --git a/out/production/HeadFirstJavaExamples/CHAPTER_16/TestGenerics$Animal.class b/out/production/HeadFirstJavaExamples/CHAPTER_16/TestGenerics$Animal.class
new file mode 100644
index 0000000..a34ab04
Binary files /dev/null and b/out/production/HeadFirstJavaExamples/CHAPTER_16/TestGenerics$Animal.class differ
diff --git a/out/production/HeadFirstJavaExamples/CHAPTER_16/TestGenerics$Cat.class b/out/production/HeadFirstJavaExamples/CHAPTER_16/TestGenerics$Cat.class
new file mode 100644
index 0000000..d332a3b
Binary files /dev/null and b/out/production/HeadFirstJavaExamples/CHAPTER_16/TestGenerics$Cat.class differ
diff --git a/out/production/HeadFirstJavaExamples/CHAPTER_16/TestGenerics$Dog.class b/out/production/HeadFirstJavaExamples/CHAPTER_16/TestGenerics$Dog.class
new file mode 100644
index 0000000..0ec485f
Binary files /dev/null and b/out/production/HeadFirstJavaExamples/CHAPTER_16/TestGenerics$Dog.class differ
diff --git a/out/production/HeadFirstJavaExamples/CHAPTER_16/TestGenerics.class b/out/production/HeadFirstJavaExamples/CHAPTER_16/TestGenerics.class
new file mode 100644
index 0000000..031f8c0
Binary files /dev/null and b/out/production/HeadFirstJavaExamples/CHAPTER_16/TestGenerics.class differ
diff --git a/out/production/HeadFirstJavaExamples/CHAPTER_16/TestMap.class b/out/production/HeadFirstJavaExamples/CHAPTER_16/TestMap.class
new file mode 100644
index 0000000..04ca2c7
Binary files /dev/null and b/out/production/HeadFirstJavaExamples/CHAPTER_16/TestMap.class differ
diff --git a/out/production/HeadFirstJavaExamples/Main.class b/out/production/HeadFirstJavaExamples/Main.class
index 46c2cd4..b730a6e 100644
Binary files a/out/production/HeadFirstJavaExamples/Main.class and b/out/production/HeadFirstJavaExamples/Main.class differ
diff --git a/out/production/HeadFirstJavaExamples/ReadAFile_C14.class b/out/production/HeadFirstJavaExamples/ReadAFile_C14.class
deleted file mode 100644
index f3e7c77..0000000
Binary files a/out/production/HeadFirstJavaExamples/ReadAFile_C14.class and /dev/null differ
diff --git a/src/Main.java b/src/Main.java
index eed5649..2705a77 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -1,11 +1,24 @@
+import chapter16.*;
+
public class Main {
public static void main(String[] args) {
- // Chapter 14
- ReadAFile_C14 readAFile_c14 = new ReadAFile_C14();
- readAFile_c14.go();
+ // CHAPTER 14
+
+ // ReadAFile readAFile = new ReadAFile();
+ // readAFile.go();
+
+ // CHAPTER 15
- // Chapter 15
+ // DailyAdviceServer server = new DailyAdviceServer();
+ // server.go();
+ // DailyAdviceClient client = new DailyAdviceClient();
+ // client.go();
+ // new SimpleChatClientA().go();
+ // CHAPTER 16
+ //new Jukebox().go();
+ //new TestMap().go();
+ new TestGenerics().go();
}
}
diff --git a/src/ReadAFile_C14.java b/src/chapter14/ReadAFile.java
similarity index 92%
rename from src/ReadAFile_C14.java
rename to src/chapter14/ReadAFile.java
index d510012..dd8fb15 100644
--- a/src/ReadAFile_C14.java
+++ b/src/chapter14/ReadAFile.java
@@ -1,8 +1,10 @@
+package chapter14;
+
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
-public class ReadAFile_C14 {
+public class ReadAFile {
public void go() {
try {
File file = new File("MyFile_C14.txt");
diff --git a/src/chapter15/DailyAdviceClient.java b/src/chapter15/DailyAdviceClient.java
new file mode 100644
index 0000000..d10fa82
--- /dev/null
+++ b/src/chapter15/DailyAdviceClient.java
@@ -0,0 +1,23 @@
+package chapter15;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.Socket;
+
+public class DailyAdviceClient {
+ public void go() {
+ try {
+ Socket s = new Socket("127.0.0.1", 4242);
+ InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
+ BufferedReader reader = new BufferedReader(streamReader);
+
+ String advice = reader.readLine();
+ System.out.println("Today you must: " + advice);
+
+ reader.close();
+ } catch (IOException e){
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/src/chapter15/DailyAdviceServer.java b/src/chapter15/DailyAdviceServer.java
new file mode 100644
index 0000000..f499b23
--- /dev/null
+++ b/src/chapter15/DailyAdviceServer.java
@@ -0,0 +1,30 @@
+package chapter15;
+
+import java.io.*;
+import java.net.*;
+
+
+public class DailyAdviceServer {
+ String[] adviceList = {"Advice 1", "Advice 2", "Advice 3", "Advice 4"};
+
+ public void go() {
+ try {
+ ServerSocket serverSocket = new ServerSocket(4242);
+ while(true){
+ Socket socket = serverSocket.accept();
+ PrintWriter writer = new PrintWriter(socket.getOutputStream());
+ String advice = getAdvice();
+ writer.println(advice);
+ writer.close();
+ System.out.println(advice);
+ }
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ }
+ }
+
+ private String getAdvice() {
+ int random = (int) (Math.random() * adviceList.length);
+ return adviceList[random];
+ }
+}
diff --git a/src/chapter15/SimpleChatClientA.java b/src/chapter15/SimpleChatClientA.java
new file mode 100644
index 0000000..f128d36
--- /dev/null
+++ b/src/chapter15/SimpleChatClientA.java
@@ -0,0 +1,84 @@
+package chapter15;
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.net.Socket;
+
+public class SimpleChatClientA {
+ JTextArea incoming;
+ JTextField outgoing;
+ BufferedReader reader;
+ PrintWriter writer;
+ Socket socket;
+
+ public void go() {
+ JFrame frame = new JFrame("Simple chat client");
+ JPanel mainPanel = new JPanel();
+ incoming = new JTextArea(15, 50);
+ incoming.setLineWrap(true);
+ incoming.setWrapStyleWord(true);
+ incoming.setEditable(false);
+ JScrollPane qScroller = new JScrollPane(incoming);
+ qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
+ qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
+ outgoing = new JTextField(20);
+ JButton sendButton = new JButton("Send");
+ sendButton.addActionListener(new SendButtonListener());
+ mainPanel.add(qScroller);
+ mainPanel.add(outgoing);
+ mainPanel.add(sendButton);
+ setUpNetworking();
+
+ Thread readerThread = new Thread(new IncomingReader());
+ readerThread.start();
+
+ frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
+ frame.setSize(400, 500);
+ frame.setVisible(true);
+ }
+
+ private void setUpNetworking(){
+ try{
+ socket = new Socket("127.0.0.1", 5000);
+ InputStreamReader streamReader = new InputStreamReader(socket.getInputStream());
+ reader = new BufferedReader(streamReader);
+ writer = new PrintWriter(socket.getOutputStream());
+ System.out.println("OK");
+ } catch (IOException ex){
+ ex.printStackTrace();
+ }
+ }
+
+ public class SendButtonListener implements ActionListener {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ try{
+ writer.println(outgoing.getText());
+ writer.flush();
+ } catch(Exception ex) {
+ ex.printStackTrace();
+ }
+ outgoing.setText("");
+ outgoing.requestFocus();
+ }
+ }
+
+ public class IncomingReader implements Runnable {
+ @Override
+ public void run() {
+ String message;
+ try {
+ while ((message = reader.readLine()) != null) {
+ System.out.println("read " + message);
+ incoming.append(message + "\n");
+ }
+ } catch (Exception ex) {ex.printStackTrace();}
+ }
+ }
+}
diff --git a/src/chapter15/VerySimpleChatServer.java b/src/chapter15/VerySimpleChatServer.java
new file mode 100644
index 0000000..93dfcf6
--- /dev/null
+++ b/src/chapter15/VerySimpleChatServer.java
@@ -0,0 +1,64 @@
+package chapter15;
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+
+public class VerySimpleChatServer {
+ ArrayList clientOutputStreams;
+
+ public class ClientHanlder implements Runnable {
+ BufferedReader reader;
+ Socket socket;
+
+ public ClientHanlder(Socket clientSocket){
+ try {
+ socket = clientSocket;
+ InputStreamReader isReader = new InputStreamReader(socket.getInputStream());
+ reader = new BufferedReader(isReader);
+ } catch (Exception ex) {ex.printStackTrace();}
+ }
+
+ public void run(){
+ String message;
+ try {
+ while ((message = reader.readLine()) != null) {
+ System.out.println("read " + message);
+ sendEveryone(message);
+ }
+ } catch (Exception ex) {ex.printStackTrace();}
+ }
+ }
+
+ public static void main(String[] args){
+ new VerySimpleChatServer().go();
+ }
+
+ public void go() {
+ clientOutputStreams = new ArrayList();
+ try {
+ ServerSocket serverSocket = new ServerSocket(5000);
+
+ while(true){
+ Socket clientSocket = serverSocket.accept();
+ PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
+ clientOutputStreams.add(writer);
+
+ Thread t = new Thread(new ClientHanlder(clientSocket));
+ t.start();
+ System.out.println("got a connection");
+ }
+ } catch (Exception ex) {ex.printStackTrace();}
+ }
+
+ public void sendEveryone(String message) {
+ Iterator iterator = clientOutputStreams.iterator();
+ while (iterator.hasNext()) {
+ try {
+ PrintWriter writer = (PrintWriter) iterator.next();
+ writer.println(message);
+ writer.flush();
+ } catch (Exception ex) {ex.printStackTrace();}
+ }
+ }
+}
diff --git a/src/chapter16/Jukebox.java b/src/chapter16/Jukebox.java
new file mode 100644
index 0000000..7d78616
--- /dev/null
+++ b/src/chapter16/Jukebox.java
@@ -0,0 +1,47 @@
+package chapter16;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+
+
+public class Jukebox {
+ ArrayList songList = new ArrayList();
+
+ class ArtistCompare implements Comparator {
+ public int compare(Song one, Song two) {
+ return one.getArtist().compareTo(two.getArtist());
+ }
+ }
+
+ public void go() {
+ getSongs();
+ System.out.println(songList);
+ Collections.sort(songList);
+ System.out.println(songList);
+ HashSet songSet = new HashSet();
+ songSet.addAll(songList);
+ System.out.println(songSet);
+ }
+
+ void getSongs() {
+ try {
+ File file = new File("SongList_C16.txt");
+ BufferedReader reader = new BufferedReader(new FileReader(file));
+ String line = null;
+ while((line = reader.readLine()) != null) {
+ addSong(line);
+ }
+ } catch (Exception ex) {ex.printStackTrace();}
+ }
+
+ void addSong(String lineToParse) {
+ String[] tokens = lineToParse.split("/");
+ Song nextSong = new Song(tokens[0], tokens[1], tokens[2], tokens[3]);
+ songList.add(nextSong);
+ }
+}
diff --git a/src/chapter16/Song.java b/src/chapter16/Song.java
new file mode 100644
index 0000000..494f80f
--- /dev/null
+++ b/src/chapter16/Song.java
@@ -0,0 +1,50 @@
+package chapter16;
+
+class Song implements Comparable {
+ String title;
+ String artist;
+ String rating;
+ String bpm;
+
+ public boolean equals(Object aSong){
+ Song s = (Song) aSong;
+ return getTitle().equals(s.getTitle());
+ }
+
+ public int hashCode() {
+ return title.hashCode();
+ }
+
+ Song (String t, String a, String r, String b) {
+ title = t;
+ artist = a;
+ rating = r;
+ bpm = b;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public String getArtist() {
+ return artist;
+ }
+
+ public String getRating() {
+ return rating;
+ }
+
+ public String getBpm() {
+ return bpm;
+ }
+
+ @Override
+ public String toString() {
+ return title;
+ }
+
+ @Override
+ public int compareTo(Song s) {
+ return title.compareTo(s.getTitle());
+ }
+}
diff --git a/src/chapter16/TestGenerics.java b/src/chapter16/TestGenerics.java
new file mode 100644
index 0000000..cc53315
--- /dev/null
+++ b/src/chapter16/TestGenerics.java
@@ -0,0 +1,39 @@
+package chapter16;
+
+import java.util.ArrayList;
+
+public class TestGenerics {
+ public void go() {
+ ArrayList animals = new ArrayList();
+ animals.add(new Dog());
+ animals.add(new Cat());
+ animals.add(new Dog());
+
+ takeAnimals(animals);
+
+ ArrayList dogs = new ArrayList();
+ dogs.add(new Dog());
+ dogs.add(new Dog());
+ takeAnimals(dogs);
+ }
+
+ public void takeAnimals(ArrayList extends Animal> animals) {
+ for(Animal a: animals) {
+ a.eat();
+ }
+ }
+
+ abstract class Animal {
+ void eat() {
+ System.out.println("Animal eats");
+ }
+ }
+
+ class Dog extends Animal {
+ void bark() {}
+ }
+
+ class Cat extends Animal {
+ void meow() {}
+ }
+}
diff --git a/src/chapter16/TestMap.java b/src/chapter16/TestMap.java
new file mode 100644
index 0000000..06aba11
--- /dev/null
+++ b/src/chapter16/TestMap.java
@@ -0,0 +1,16 @@
+package chapter16;
+
+import java.util.HashMap;
+
+public class TestMap {
+ public void go() {
+ HashMap scores = new HashMap();
+
+ scores.put("Kathy", 42);
+ scores.put("Bert", 343);
+ scores.put("Skyler", 420);
+
+ System.out.println(scores);
+ System.out.println(scores.get("Bert"));
+ }
+}