Skip to content

Commit 7a4ec0f

Browse files
authored
Merge pull request #2 from OtherBooks/ptx2
PTX, attempt 2
2 parents c9a8954 + 26c2407 commit 7a4ec0f

File tree

216 files changed

+55454
-12
lines changed

Some content is hidden

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

216 files changed

+55454
-12
lines changed

source/a-command-line-interface.ptx

Lines changed: 487 additions & 0 deletions
Large diffs are not rendered by default.

source/a-graphical-user-interface-gui.ptx

Lines changed: 803 additions & 0 deletions
Large diffs are not rendered by default.

source/abstract-classes-interfacesand-polymorphism.ptx

Lines changed: 373 additions & 0 deletions
Large diffs are not rendered by default.

source/adding-text-network-resourcesforan-application.ptx

Lines changed: 583 additions & 0 deletions
Large diffs are not rendered by default.

source/agui-based-game-optional-graphics.ptx

Lines changed: 1309 additions & 0 deletions
Large diffs are not rendered by default.

source/an-overviewof-networks.ptx

Lines changed: 402 additions & 0 deletions
Large diffs are not rendered by default.

source/appendix-codestyle.ptx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:id="appendix-codestyle">
2+
<title>Coding Conventions</title>
3+
<introduction>
4+
<p>
5+
This appendix covers various aspects of programming style and coding conventions.
6+
It follows the conventions suggested in the Java \mbox{Language} Specification (<c>http://java.sun.com/docs/books/jls/</c>), which is summarized on Sun's Java Web site (<c>http://java.sun.com/docs/</c>).
7+
The conventions have been modified somewhat to fit the needs of an academic programming course.
8+
For further details see
9+
</p>
10+
<p>
11+
\marginpar{ \psfig{file=../commonart/www_dg.eps}}
12+
</p>
13+
<pre>
14+
http://java.sun.com/docs/codeconv/index.html
15+
</pre>
16+
<p>
17+
Coding conventions improve the readability and maintainability of the code.
18+
Because maintenance is often done by programmers who did not have a hand in designing or writing the original code,
19+
it is important that the code follow certain conventions.
20+
For a typical piece of commercial software,
21+
much more time and expense are invested in maintaining the code than in creating the code.
22+
</p>
23+
</introduction>
24+
<xi:include href="comments.ptx"/>
25+
<xi:include href="indentationand-white-space.ptx"/>
26+
<xi:include href="naming-conventions.ptx"/>
27+
<xi:include href="useof-braces.ptx"/>
28+
<xi:include href="file-namesand-layout.ptx"/>
29+
<xi:include href="statements.ptx"/>
30+
<xi:include href="executable-statements.ptx"/>
31+
<xi:include href="preconditionsand-postconditions.ptx"/>
32+
<xi:include href="sample-programs.ptx"/>
33+
</chapter>

source/appendix-jdk.ptx

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:id="appendix-jdk">
2+
<title>The Java Development Kit</title>
3+
<introduction>
4+
<p>
5+
The Java Development Kit (JDK) for Java<m>^{ TM}</m> 2 Platform Standard Edition is a set of command-line tools for developing Java programs.
6+
It is available for free in versions for recent editions of Microsoft Windows, Linus, Macintosh OS X, and Solaris (Sun Microsystems).
7+
</p>
8+
<p>
9+
Download information and documentation are available for the entire range of products associated with the Java<m>^{ TM}</m> 2 Platform, Standard Edition (Java SE) at;
10+
</p>
11+
<pre>
12+
http://java.sun.com/j2se/
13+
</pre>
14+
<p>
15+
This appendix summarizes some of the primary tools available in the JDK. For more detailed information you should consult Sun's Web site.
16+
</p>
17+
<p>
18+
Table<nbsp/>B.1 provides a summary of some of the JDK tools.
19+
</p>
20+
<table>
21+
\TBT{0pc}{Tools included in the Java Development Kit.}
22+
\hspace{-6pt}
23+
<tabular>
24+
<row>
25+
<cell>\multicolumn{2}{l}{\rule{27pc}{1pt}}</cell>
26+
</row>
27+
<row>
28+
<cell><term>Tool Name</term></cell>
29+
<cell><term>Description</term></cell>
30+
</row>
31+
<row>
32+
<cell>[-4pt]\multicolumn{2}{l}{\rule{27pc}{0.5pt}}</cell>
33+
</row>
34+
<row>
35+
<cell>javac</cell>
36+
<cell>Java compiler. Translates source code into bytecode.\cr
37+
java</cell>
38+
<cell>Java interpreter. Translates and executes bytecode.\cr
39+
javadoc</cell>
40+
<cell>Java documentation generator. Creates HTML pages from\cr</cell>
41+
<cell>documentation comments embedded in Java programs.\cr
42+
appletviewer</cell>
43+
<cell>Appletviewer. Used instead of a browser to run Java applets.\cr
44+
jar</cell>
45+
<cell>Java archive manager. Manages Java archive (JAR) files.\cr
46+
jdb</cell>
47+
<cell>Java debugger. Used to find bugs in a program.\cr
48+
javap</cell>
49+
<cell>Java disassembler. Translates bytecode into Java source code.</cell>
50+
</row>
51+
<row>
52+
<cell>[-4pt]\multicolumn{2}{l}{\rule{27pc}{1pt}}</cell>
53+
</row>
54+
</tabular>
55+
\endTB
56+
</table>
57+
<p>
58+
Sun Microsystems provides detailed instructions on how to install JDK for Java SE on computers running any of the above operating systems,
59+
including how to set the system's PATH and CLASSPATH variables.
60+
Installation instructions can be located using the above link to downloading information.
61+
</p>
62+
</introduction>
63+
<xi:include href="the-java-compiler.ptx"/>
64+
<xi:include href="the-java-interpreter.ptx"/>
65+
<xi:include href="the.ptx"/>
66+
<xi:include href="the-java-archiver-tool.ptx"/>
67+
<xi:include href="the-java-documentation-tool.ptx"/>
68+
</chapter>
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
<section xml:id="array-algorithms-searching">
2+
<title>Array Algorithms: Searching</title>
3+
<introduction>
4+
<p>
5+
Suppose we have a large array and we need to find one of its elements.
6+
We need an algorithm to search the array for a particular value,
7+
usually called the <em>key</em>.
8+
If the elements of the array are not arranged in any particular order,
9+
the only way we can be sure to find the key,
10+
assuming it is in the array,
11+
is to search every element, beginning at the first element,
12+
until we find it.
13+
</p>
14+
</introduction>
15+
<subsection xml:id="fig-p430f1">
16+
<title>Sequential Search</title>
17+
<p>
18+
This approach is known as a <em>sequential
19+
<idx><h>sequential search</h></idx>
20+
search</em>,
21+
because each element of the array will be examined in sequence until the key is found
22+
(or the end of the array is reached).
23+
A pseudocode description of this algorithm is as follows:
24+
</p>
25+
<pre>
26+
1. For each element of the array
27+
2. If the element equals the key
28+
3. Return its index
29+
4. If the key is not found in the array
30+
5. Return -1 (to indicate failure)
31+
</pre>
32+
<p>
33+
This algorithm can easily be implemented in a method that searches an integer array,
34+
which is passed as the method's parameter.
35+
If the key is found in the array, its location is returned.
36+
If it is not found, then <m>-1</m> is returned to indicate failure.
37+
</p>
38+
<p>
39+
<image width="73%" source="chptr09"/>
40+
</p>
41+
<p>
42+
The <c>Search</c> class (Figs.<nbsp/>9.15 and <xref ref="fig-search"></xref>) provides the Java implementation of the <c>sequentialSearch()</c> method.
43+
The method takes two parameters:
44+
the array to be searched and the key to be searched for.
45+
It uses a for statement to examine each element of the array,
46+
checking whether it equals the key or not.
47+
If an element that equals the key is found,
48+
the method immediately returns that element's index.
49+
Note that the last statement in the method will only be reached if no element matching the key is found.
50+
</p>
51+
<figure xml:id="fig-search">
52+
<caption>The <c>Search</c> class contains both a <c>sequentialSearch()</c>
53+
and a <c>binarySearch()</c>.</caption>
54+
<pre>
55+
public class Search {
56+
public int sequentialSearch(int arr[], int key) {
57+
for (int k = 0; k &lt; arr.length; k++)
58+
if (arr[k] == key)
59+
return k;
60+
return -1; // Failure if this is reached
61+
} // sequentialSearch()
62+
public int binarySearch(int arr[], int key) {
63+
int low = 0; // Initialize bounds
64+
int high = arr.length - 1;
65+
while (low &lt;= high) { // While not done
66+
int mid = (low + high) / 2;
67+
if (arr[mid] == key)
68+
return mid; // Success
69+
else if (arr[mid] &lt; key)
70+
low = mid + 1; // Search top half
71+
else
72+
high = mid - 1; // Search bottom half
73+
} // while
74+
return -1; // Post: if low > high search failed
75+
} // binarySearch()}//Search
76+
</pre>
77+
</figure>
78+
<principle>
79+
<title>EFFECTIVE DESIGN:Sentinel Return Value</title>
80+
<p>
81+
Like Java's
82+
<c>indexOf()</c> method,
83+
the <c>sequentialSearch()</c> returns a sentinel value (<m>-1</m>) to indicate that the key was not found.
84+
This is a common design for search methods.
85+
</p>
86+
</principle>
87+
</subsection>
88+
<subsection xml:id="fig-p432f1">
89+
<title>Binary Search</title>
90+
<p>
91+
If the elements of an array have been sorted into ascending or descending order,
92+
it is not necessary to search sequentially through each element of the array in order to find the key.
93+
Instead, the search algorithm can make use of the knowledge that the array is ordered and perform what's known as a <em>binary
94+
<idx><h>binary search</h></idx>
95+
search</em>, which is a divide-and-conquer
96+
<idx><h>divide-and-conquer</h></idx>
97+
algorithm that divides the array in half on each iteration and limits its search to just that half that could contain the key.
98+
</p>
99+
<p>
100+
To illustrate the binary search,
101+
recall the familiar guessing game in which you try to guess a secret number between 1 and 100, being told
102+
<q>too high</q>
103+
or
104+
<q>too low</q>
105+
or
106+
<q>just right</q>
107+
on each guess.
108+
A good first guess should be 50.
109+
If this is too high, the next guess should be 25,
110+
because if 50 is too high the number must be between 1 and 49.
111+
If 50 was too low, the next guess should be 75, and so on.
112+
After each wrong guess,
113+
a good guesser should pick the midpoint of the sublist that would contain the secret number.
114+
</p>
115+
<p>
116+
Proceeding in this way,
117+
the correct number can be guessed in at most \marginpar{How many guesses?} <m>log_2 N</m> guesses,
118+
because the base-2 logarithm of <em>N</em>
119+
is the number of times you can divide <em>N</em> in half.
120+
For a list of 100 items, the search should take no more than seven guesses
121+
(<m>2^7 = 128 > 100</m>).
122+
For a list of <m>1,000</m> items,
123+
a binary search would take at most ten guesses (2<m>^{10}=1,024>1,000</m>).
124+
</p>
125+
<p>
126+
So a binary search is a much more efficient way to search,
127+
provided the array's elements are in order.
128+
Note that
129+
<q>order</q>
130+
here needn't be numeric order.
131+
We could use binary search to look up a word in a dictionary or a name in a phone book.
132+
</p>
133+
<p>
134+
A pseudocode representation of the binary search is given as follows:
135+
</p>
136+
<pre>
137+
TO SEARCH AN ARRAY OF N ELEMENTS IN ASCENDING ORDER
138+
1. Assign 0 low and assign N-1 to high initially
139+
2. As long as low is not greater than high
140+
3. Assign (low + high) / 2 to mid
141+
4. If the element at mid equals the key
142+
5. then return its index
143+
6. Else if the element at mid is less than the key
144+
7. then assign mid + 1 to low
145+
8. Else assign mid - 1 to high
146+
9. If this is reached return -1 to indicate failure
147+
</pre>
148+
<p>
149+
Just as with the sequential search algorithm,
150+
this algorithm can easily be implemented in a method that searches an integer array that is passed as the method's parameter (Fig.
151+
<xref ref="fig-search"></xref>).
152+
If the key is found in the array, its location is returned.
153+
If it is not found, then <m>-1</m> is returned to indicate failure.
154+
The <c>binarySearch()</c> method takes the same type of parameters as <c>sequentialSearch()</c>. Its local variables, <c>low</c> and <c>high</c>, are used as pointers,
155+
or references,
156+
to the current low and high ends of the array, respectively.
157+
Note the loop-entry condition: <c>low &lt;= high</c>. If <c>low</c> ever becomes greater than <c>high</c>, this indicates that <c>key</c> is not contained in the array.
158+
In that case, the algorithm returns <m>-1</m>.
159+
</p>
160+
<p>
161+
As a binary search progresses,
162+
the array is repeatedly cut in half and
163+
<c>low</c> and <c>high</c> will be used to point to the low and high index values in that portion of the array that is still being searched.
164+
The local variable <c>mid</c> is used to point to the approximate midpoint of the unsearched portion of the array.
165+
If the key is determined to be past the midpoint,
166+
then <c>low</c> is adjusted to <c>mid+1</c>; if the key occurs before the midpoint,
167+
then <c>high</c> is set to <c>mid-1</c>. The updated values of <c>low</c> and <c>high</c> limit the search to the unsearched portion of the original array.
168+
</p>
169+
<p>
170+
Unlike sequential search,
171+
binary search does not have to examine every location in the array to determine that the key is not in the array.
172+
It searches only that part of the array that could contain the key.
173+
For example,
174+
suppose we are searching for <m>-5</m> in the following array:
175+
</p>
176+
<pre>
177+
int sortArr[] =
178+
{ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
179+
</pre>
180+
<p>
181+
The <m>-5</m> is smaller than the smallest array element.
182+
Therefore, the algorithm will repeatedly divide the low end of the array in half until the condition
183+
<c>low > high</c> becomes true.
184+
We can see this by tracing the values that low, mid,
185+
and high will take during the search:
186+
</p>
187+
<pre>
188+
Key Iteration Low High Mid
189+
----------------------------------
190+
-5 0 0 19 9
191+
-5 1 0 8 4
192+
-5 2 0 3 1
193+
-5 3 0 0 0
194+
-5 4 0 -1 Failure
195+
</pre>
196+
<p>
197+
As this trace shows,
198+
the algorithm examines only four locations to determine that <m>-5</m> is not in the array.
199+
After checking location 0, the new value for <c>high</c> will become <m>-1</m>,
200+
which makes the condition <c>low &lt;= high </c> false.
201+
So the search will terminate.
202+
</p>
203+
<p>
204+
<image width="73%" source="chptr09"/>
205+
</p>
206+
<p>
207+
The <c>TestSearch</c> class (Figs.<nbsp/>9.17 and <xref ref="fig-testsearch"></xref>) provides a program that can be used to test two search methods.
208+
It creates an integer array, whose values are in ascending order.
209+
It then uses the <c>getInput()</c> method to input an integer from the keyboard and then performs both a <c>sequentialSearch()</c> and a <c>binarySearch()</c> for the number.
210+
</p>
211+
<p>
212+
\secEXRHone{Self-Study Exercise}
213+
<ol>
214+
<li>
215+
<p>
216+
For the array containing the elements 2, 4, 6, and so on up to 28 in that order,
217+
draw a trace showing which elements are examined if you search for 21 using a binary search.
218+
</p>
219+
</li>
220+
</ol>
221+
</p>
222+
<figure xml:id="fig-testsearch">
223+
<caption>The <c>TestSearch</c> class.</caption>
224+
<pre>
225+
import java.io.*;
226+
public class TestSearch {
227+
public static int getInput() {
228+
KeyboardReader kb = new KeyboardReader();
229+
kb.prompt("This program searches for values in an array.");
230+
kb.prompt(
231+
"Input any positive integer (or any negative to quit) : ");
232+
return kb.getKeyboardInteger();
233+
} // getInput()
234+
public static void main(String args[]) throws IOException {
235+
int intArr[] = { 2,4,6,8,10,12,14,16,18,20,22,24,26,28};
236+
Search searcher = new Search();
237+
int key = 0, keyAt = 0;
238+
key = getInput();
239+
while (key >= 0) {
240+
keyAt = searcher.sequentialSearch( intArr, key );
241+
if (keyAt != -1)
242+
System.out.println(" Sequential: " + key +
243+
" is at intArr[" + keyAt + "]");
244+
else
245+
System.out.println(" Sequential: " + key
246+
+ " is not contained in intArr[]");
247+
keyAt = searcher.binarySearch(intArr, key);
248+
if (keyAt != -1)
249+
System.out.println(" Binary: " + key +
250+
" is at intArr[" + keyAt + "]");
251+
else
252+
System.out.println(" Binary: " + key +
253+
" is not contained in intArr[]");
254+
key = getInput();
255+
} // while
256+
} // main()} // TestSearch
257+
</pre>
258+
</figure>
259+
</subsection>
260+
</section>

0 commit comments

Comments
 (0)