Skip to content

Commit 74d3d5f

Browse files
committed
Chapter 13 Complete
1 parent f3e943e commit 74d3d5f

File tree

658 files changed

+15133
-47
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

658 files changed

+15133
-47
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
/.project
66
/.settings/
77
/.classpath
8-
8+
/files/

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ ____
5656
>Chapter 11</strong></a> - Inheritance and Polymorphism (Complete)</li><br>
5757
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_12"><strong
5858
>Chapter 12</strong></a> - Exception Handling and Text I/O (Complete)</li><br>
59-
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_13"><strong>Chapter 13</strong></a> - Abstract Classes and Interfaces</li><br>
59+
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_13"><strong
60+
>Chapter 13</strong></a> - Abstract Classes and Interfaces (Complete) </li><br>
6061
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_14"><strong>Chapter 14</strong></a> - JavaFx Basics</li><br>
6162
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_15"><strong>Chapter 15</strong></a> - Event-Driven Programming and Animations</li><br>
6263
<li><a href="https://github.com/HarryDulaney/intro-to-java-programming/tree/master/src/ch_16"><strong>Chapter

files/book/ATest.class

1.05 KB
Binary file not shown.

files/book/AVLTree$AVLTreeNode.class

642 Bytes
Binary file not shown.

files/book/AbstractGraph$Edge.class

610 Bytes
Binary file not shown.

files/book/AbstractGraph$Tree.class

2.65 KB
Binary file not shown.

files/book/AbstractGraph.class

7.07 KB
Binary file not shown.
Binary file not shown.
873 Bytes
Binary file not shown.
705 Bytes
Binary file not shown.

files/book/AccountWithoutSync.java

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.util.concurrent.*;
2+
3+
public class AccountWithoutSync {
4+
private static Account account = new Account();
5+
6+
public static void main(String[] args) {
7+
ExecutorService executor = Executors.newCachedThreadPool();
8+
9+
// Create and launch 100 threads
10+
for (int i = 0; i < 100; i++) {
11+
executor.execute(new AddAPennyTask());
12+
}
13+
14+
executor.shutdown();
15+
16+
// Wait until all tasks are finished
17+
while (!executor.isTerminated()) {
18+
}
19+
20+
System.out.println("What is balance? " + account.getBalance());
21+
}
22+
23+
// A thread for adding a penny to the account
24+
private static class AddAPennyTask implements Runnable {
25+
public void run() {
26+
account.deposit(1);
27+
}
28+
}
29+
30+
// An inner class for account
31+
private static class Account {
32+
private int balance = 0;
33+
34+
public int getBalance() {
35+
return balance;
36+
}
37+
38+
public void deposit(int amount) {
39+
int newBalance = balance + amount;
40+
41+
// This delay is deliberately added to magnify the
42+
// data-corruption problem and make it easy to see.
43+
try {
44+
Thread.sleep(5);
45+
}
46+
catch (InterruptedException ex) {
47+
}
48+
49+
balance = newBalance;
50+
}
51+
}
52+
}
1.88 KB
Binary file not shown.

files/book/AddNewRowDemo.class

5.1 KB
Binary file not shown.

files/book/AddressRegistration.java

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package jsf2demo;
2+
3+
import javax.inject.Named;
4+
import javax.enterprise.context.SessionScoped;
5+
import java.sql.*;
6+
7+
@Named
8+
@SessionScoped
9+
public class AddressRegistration implements java.io.Serializable {
10+
private String lastName;
11+
private String firstName;
12+
private String mi;
13+
private String telephone;
14+
private String email;
15+
private String street;
16+
private String city;
17+
private String state;
18+
private String zip;
19+
private String status = "Nothing stored";
20+
// Use a prepared statement to store a student into the database
21+
private PreparedStatement pstmt;
22+
23+
public AddressRegistration() {
24+
initializeJdbc();
25+
}
26+
27+
public String getLastName() {
28+
return lastName;
29+
}
30+
31+
public void setLastName(String lastName) {
32+
this.lastName = lastName;
33+
}
34+
35+
public String getFirstName() {
36+
return firstName;
37+
}
38+
39+
public void setFirstName(String firstName) {
40+
this.firstName = firstName;
41+
}
42+
43+
public String getMi() {
44+
return mi;
45+
}
46+
47+
public void setMi(String mi) {
48+
this.mi = mi;
49+
}
50+
51+
public String getTelephone() {
52+
return telephone;
53+
}
54+
55+
public void setTelephone(String telephone) {
56+
this.telephone = telephone;
57+
}
58+
59+
public String getEmail() {
60+
return email;
61+
}
62+
63+
public void setEmail(String email) {
64+
this.email = email;
65+
}
66+
67+
public String getStreet() {
68+
return street;
69+
}
70+
71+
public void setStreet(String street) {
72+
this.street = street;
73+
}
74+
75+
public String getCity() {
76+
return city;
77+
}
78+
79+
public void setCity(String city) {
80+
this.city = city;
81+
}
82+
83+
public String getState() {
84+
return state;
85+
}
86+
87+
public void setState(String state) {
88+
this.state = state;
89+
}
90+
91+
public String getZip() {
92+
return zip;
93+
}
94+
95+
public void setZip(String zip) {
96+
this.zip = zip;
97+
}
98+
99+
private boolean isRquiredFieldsFilled() {
100+
return !(lastName == null || firstName == null
101+
|| lastName.trim().length() == 0
102+
|| firstName.trim().length() == 0);
103+
}
104+
105+
public String processSubmit() {
106+
if (isRquiredFieldsFilled()) {
107+
return "ConfirmAddress";
108+
} else {
109+
return "";
110+
}
111+
}
112+
113+
public String getRequiredFields() {
114+
if (isRquiredFieldsFilled()) {
115+
return "";
116+
} else {
117+
return "Last Name and First Name are required";
118+
}
119+
}
120+
121+
public String getInput() {
122+
return "<p style=\"color:red\">You entered <br />"
123+
+ "Last Name: " + lastName + "<br />"
124+
+ "First Name: " + firstName + "<br />"
125+
+ "MI: " + mi + "<br />"
126+
+ "Telephone: " + telephone + "<br />"
127+
+ "Email: " + email + "<br />"
128+
+ "Street: " + street + "<br />"
129+
+ "City: " + city + "<br />"
130+
+ "Street: " + street + "<br />"
131+
+ "City: " + city + "<br />"
132+
+ "State: " + state + "<br />"
133+
+ "Zip: " + zip + "</p>";
134+
}
135+
136+
/** Initialize database connection */
137+
private void initializeJdbc() {
138+
try {
139+
// Explicitly load a MySQL driver
140+
Class.forName("com.mysql.jdbc.Driver");
141+
System.out.println("Driver loaded");
142+
143+
// Establish a connection
144+
Connection conn = DriverManager.getConnection(
145+
"jdbc:mysql://localhost/javabook", "scott", "tiger");
146+
147+
// Create a Statement
148+
pstmt = conn.prepareStatement("insert into Address (lastName,"
149+
+ " firstName, mi, telephone, email, street, city, "
150+
+ "state, zip) values (?, ?, ?, ?, ?, ?, ?, ?, ?)");
151+
} catch (Exception ex) {
152+
System.out.println(ex);
153+
}
154+
}
155+
156+
/** Store an address to the database */
157+
public String storeStudent() {
158+
try {
159+
pstmt.setString(1, lastName);
160+
pstmt.setString(2, firstName);
161+
pstmt.setString(3, mi);
162+
pstmt.setString(4, telephone);
163+
pstmt.setString(5, email);
164+
pstmt.setString(6, street);
165+
pstmt.setString(7, city);
166+
pstmt.setString(8, state);
167+
pstmt.setString(9, zip);
168+
pstmt.executeUpdate();
169+
status = firstName + " " + lastName
170+
+ " is now registered in the database.";
171+
} catch (Exception ex) {
172+
status = ex.getMessage();
173+
}
174+
175+
return "AddressStoredStatus";
176+
}
177+
178+
public String getStatus() {
179+
return status;
180+
}
181+
}

files/book/Animal.class

286 Bytes
Binary file not shown.
1.03 KB
Binary file not shown.
1.03 KB
Binary file not shown.

files/book/AnonymousHandlerDemo.class

1.92 KB
Binary file not shown.

files/book/AnonymousHandlerDemo.java

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import javafx.application.Application;
2+
import javafx.event.ActionEvent;
3+
import javafx.event.EventHandler;
4+
import javafx.geometry.Pos;
5+
import javafx.scene.Scene;
6+
import javafx.scene.control.Button;
7+
import javafx.scene.layout.HBox;
8+
import javafx.stage.Stage;
9+
10+
public class AnonymousHandlerDemo extends Application {
11+
@Override // Override the start method in the Application class
12+
public void start(Stage primaryStage) {
13+
// Hold two buttons in an HBox
14+
HBox hBox = new HBox();
15+
hBox.setSpacing(10);
16+
hBox.setAlignment(Pos.CENTER);
17+
Button btNew = new Button("New");
18+
Button btOpen = new Button("Open");
19+
Button btSave = new Button("Save");
20+
Button btPrint = new Button("Print");
21+
hBox.getChildren().addAll(btNew, btOpen, btSave, btPrint);
22+
23+
// Create and register the handler
24+
btNew.setOnAction(new EventHandler<ActionEvent>() {
25+
@Override // Override the handle method
26+
public void handle(ActionEvent e) {
27+
System.out.println("Process New");
28+
}
29+
});
30+
31+
btOpen.setOnAction(new EventHandler<ActionEvent>() {
32+
@Override // Override the handle method
33+
public void handle(ActionEvent e) {
34+
System.out.println("Process Open");
35+
}
36+
});
37+
38+
btSave.setOnAction(new EventHandler<ActionEvent>() {
39+
@Override // Override the handle method
40+
public void handle(ActionEvent e) {
41+
System.out.println("Process Save");
42+
}
43+
});
44+
45+
btPrint.setOnAction(new EventHandler<ActionEvent>() {
46+
@Override // Override the handle method
47+
public void handle(ActionEvent e) {
48+
System.out.println("Process Print");
49+
}
50+
});
51+
52+
// Create a scene and place it in the stage
53+
Scene scene = new Scene(hBox, 300, 50);
54+
primaryStage.setTitle("AnonymousHandlerDemo"); // Set title
55+
primaryStage.setScene(scene); // Place the scene in the stage
56+
primaryStage.show(); // Display the stage
57+
}
58+
59+
/**
60+
* The main method is only needed for the IDE with limited
61+
* JavaFX support. Not needed for running from the command line.
62+
*/
63+
public static void main(String[] args) {
64+
launch(args);
65+
}
66+
}

files/book/AnyWildCardDemo.class

1.32 KB
Binary file not shown.

files/book/AppendData.class

255 Bytes
Binary file not shown.

files/book/Apple.class

356 Bytes
Binary file not shown.

files/book/B.class

527 Bytes
Binary file not shown.

files/book/BMI.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
public class BMI {
2+
private String name;
3+
private int age;
4+
private double weight; // in pounds
5+
private double height; // in inches
6+
public static final double KILOGRAMS_PER_POUND = 0.45359237;
7+
public static final double METERS_PER_INCH = 0.0254;
8+
9+
public BMI(String name, int age, double weight, double height) {
10+
this.name = name;
11+
this.age = age;
12+
this.weight = weight;
13+
this.height = height;
14+
}
15+
16+
public BMI(String name, double weight, double height) {
17+
this(name, 20, weight, height);
18+
}
19+
20+
public double getBMI() {
21+
double bmi = weight * KILOGRAMS_PER_POUND /
22+
((height * METERS_PER_INCH) * (height * METERS_PER_INCH));
23+
return Math.round(bmi * 100) / 100.0;
24+
}
25+
26+
public String getStatus() {
27+
double bmi = getBMI();
28+
if (bmi < 18.5)
29+
return "Underweight";
30+
else if (bmi < 25)
31+
return "Normal";
32+
else if (bmi < 30)
33+
return "Overweight";
34+
else
35+
return "Obese";
36+
}
37+
38+
public String getName() {
39+
return name;
40+
}
41+
42+
public int getAge() {
43+
return age;
44+
}
45+
46+
public double getWeight() {
47+
return weight;
48+
}
49+
50+
public double getHeight() {
51+
return height;
52+
}
53+
}

files/book/BST$InorderIterator.class

1.75 KB
Binary file not shown.

files/book/BST$TreeNode.class

664 Bytes
Binary file not shown.

files/book/BST.class

4.89 KB
Binary file not shown.

0 commit comments

Comments
 (0)