You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.}
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>
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 <= 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:
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 <= 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};
0 commit comments