Copyright 2011, 2013 by Rex A. Barzee. All rights reserved.Except as permitted under the Copyright Act of 1976, no part of thispublication may be reproduced, stored in a retrieval system, ortransmitted, in any form or by any means, electronic, mechanical,photocopying, recording, or otherwise, without the prior writtenpermission of the author.
Published by Maia L.L.C.
Idaho, U.S.A.
maiaco.com
The author and publisher of this book have used their best efforts inpreparing this book, and the information contained in this book isbelieved to be reliable. These efforts include the development,research, and testing of the theories and computer programs in this bookto determine their correctness. However, because of the possibility ofhuman or mechanical error, the author and publisher make no warranty ofany kind, expressed or implied, with regard to these programs or thedocumentation or information contained in this book. The author andpublisher shall not be liable in any event for incidental orconsequential damages in connection with or arising out of thefurnishing, performance, or use of these programs.
Preface
vii
Learning to program a computer is a frustrating task for many people.In my teaching experience I have found that much of this frustrationcomes because students are not shown enough programming examples. All ofus learn by watching others, so why should learning to program acomputer be any different? I wrote this book with minimal text butfilled with many computer programming examples to help students andprofessionals learn computer programming more efficiently and thoroughlywith less frustration.
Who Should Read This Book?
I wrote this book for my students graduating with a degree incomputer science or information technology from Brigham Young University Idaho. Consequently, those that will benefit most from readingthis book are fourth year undergraduate students, graduate students, andsoftware developers with less than three years of work experience. Eachchapter within this book starts with content easily understood by secondyear undergraduate students and moves quickly to more difficult content.This is my attempt to lure students to study content that is moredifficult than they normally would.
How to Use This Book
You can use this book as a tutorial or a reference. When using it asa tutorial, you will find it helpful to step through the example codeline by line as if you were a computer. Doing this is sometimes called adesk check because you are checking the codeon paper or at your desk instead of running it on acomputer. To aid you in desk checking the example code, I have provideddesk check locations throughout the book. Each desk check locationincludes a list of all the variables found in the corresponding examplecode and values for the input variables. To perform a desk check, stepthrough the code as if you were the computer and change the value ofeach variable just as the computer would. The answers to all desk checksare in .
Because of the many code examples in this book, you may also use itas a reference where you can find how to correctly implement and usealgorithms, including binary search; setting, clearing, and countingbits; converting recursion to iteration and vice versa; computing theintersection, union, and complement of two sets; computing in anumerically stable way the mean and variance of a sample and thecorrelation of two samples; and much more. All the code examples in thisbook are written in Java except for those in, which are writtenin C.
viii
Acknowledgments
Thank you to my many professors, fellow students, and colleagues forthe knowledge they shared with me.
Review This Book
Please write a review of this book athttp://www.amazon.com/dp/B006R2CSXM. Your comments andsuggestions help the author and publisher produce better books.
1
Arrays
An array is a collection of variables whereeach variable has the same data type, for example, an array of integersor an array of Appliance objects. Each variable in an array iscalled an element and has anindex, also called thesubscript, which denotes that elementslocation within the array. One advantage of using arrays is thatall the elements within an array can be declared, created, and passed toa function as a whole. However, each element may still be accessedindividually by using the elements index. shows a representation of an arraywith 10 elements. Notice that each element has a unique index, andthe first index of an array is 0.
elements | | | | | | | | | | |
---|
indices | [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] |
---|
| array length is 10 |
---|
Figure 1: A representation of an array showing the elementsand indices.
Fill an Array
It is often desirable to have the computer initialize all theelements in an array to a single value (usually zero). We do thisby writing a statement that assigns a value to one element in the array,and then put that assignment statement inside a loop. If the arrayhas n elements in it, then the loop causes the computer torepeat the assignment statement n times, once for eachelement in the array.
Example 1
/** Fills an array with the value x. */public static void fill(double[] list, double x) { for (int i = 0; i < list.length; ++i) { list[i] = x; }}
Desk |
---|
list | | x | i |
---|
| | | | | 8.3 | |
---|
[0] | [1] | [2] | [3] | | |
---|
| |
---|
|
---|
|
---|
Within the Java 1.7 libraries, there is already afill
method in thejava.util.Arrays
class. It is writtenalmost exactly as shown above.
Initialize an Array to a Ramp
Figure 2: A plot of constantly increasing values forming adiscrete ramp function.
Within programs that perform image processing, we often use an arrayas a palette. To get the color value to display for a pixel,the computer reads a pixel value from the image and then looks up thatvalue in the palette. Then the computer displays the color from thepalette at the corresponding pixel location on the monitor. Often thepalette must be initialized to contain values that are constantlyincreasing, for example: 0, 1, 2, 3 This is known as a ramp because if you plotted the values in the array,you would see a sloping line or ramp, constantly increasing to the rightas shown in