|How to Think Like a Computer Scientist|
source ref: ebookit.html
In Section 4.8 we used a Graphics object to draw circles in a window, and I used the phrase "invoke a method on an object," to refer to the statements like
In this case drawOval is the method being invoked on the object named g. At the time I didn't provide a definition of object, and I still can't provide a complete definition, but it is time to try.
In Java and other object-oriented languages, objects are collections of related data that come with a set of methods. These methods operate on the objects, performing computations and sometimes modifying the object's data.
So far we have only seen one object, g, so this definition might not mean much yet. Another example is strings. Strings are objects (and ints and doubles are not). Based on the definition of object, you might ask "What is the data contained in a String object?" and "What are the methods we can invoke on String objects?"
The data contained in a String object are the letters of the string. There are quite a few methods the operate on Strings, but I will only use a few in this book. The rest are documented at
The first method we will look at is charAt, which allows you to extract letters from a string. In order to store the result, we need a variable type that can store individual letters (as opposed to strings). Individial letters are called characters, and the variable type that stores them is called char.
chars work just like the other types we have seen:
Character values appear in single quotes ('c'). Unlike string values (which appear in double quotes), character values can contain only a single letter.
Here's how the charAt method is used:
The syntax fruit.charAt indicates that I am invoking the charAt method on the object named fruit. I am passing the argument 1 to this method, which indicates that I would like to know the first letter of the string. The result is a character, which is stored in a char named letter. When I print the value of letter, I get a surprise:
a is not the first letter of "banana". Unless you are a computer scientist. For perverse reasons, computer scientists always start counting from zero. The 0th letter ("zeroeth") of "banana" is b. The 1th letter ("oneth") is a and the 2th ("twoeth") letter is n.
If you want the the zereoth letter of a string, you have to pass zero as an argument:
The second String method we'll look at is length, which returns the number of characters in the string. For example:
length takes no arguments (as indicated by ()), and returns an integer, in this case 6. Notice that it is legal to have a variable with the same name as a method (although it can be confusing for human readers).
To find the last letter of a string, you might be tempted to try something like
That won't work. The reason is that there is no 6th letter in "banana". Since we started counting at 0, the 6 letters are numbered from 0 to 5. To get the last character, you have to subtract 1 from length.
A common thing to do with a string is start at the beginning, select each character in turn, do something to it, and continue until the end. This pattern of processing is called a traversal. A natural way to encode a traversal is with a while statement:
This loop traverses the string and prints each letter on a line by itself. Notice that the condition is index < fruit.length(), which means that when index is equal to the length of the string, the condition is false and the body of the loop is not executed. The last character we access is the one with the index fruit.length()-1.
The name of the loop variable is index. An index is a variable or value used to specify one member of an ordered set (in this case the set of characters in the string). The index indicates (hence the name) which one you want. The set has to be ordered so that each letter has an index and each index refers to a single character.
Way back in Section 1.3.2 I talked about run-time errors, which are errors that don't appear until a program has started running. In Java run-time errors are called exceptions.
So far, you probably haven't seen many run-time errors, because we haven't been doing many things that can cause one. Well, now we are. If you use the charAt command and you provide an index that is negative or greater than length-1, you will get an exception: specifically, a StringIndexOutOfBoundsException. Try it and see how it looks.
If your program causes an exception, it prints an error message indicating the type of exception and where in the program it occurred. Then the program terminates.
If you go to
and click on charAt, you will get the following documentation (or something like it):
The first line is the method's prototype (see Section 4.14), which indicates the name of the method, the type of the parameters, and the return type.
The next line describes what the method does. The next two lines explain the parameters and return values. In this case the explanations are a bit redundant, but the documentation is supposed to fit a standard format. The last line explains what exceptions, if any, can be caused by this method.
In some ways, indexOf is the opposite of charAt. charAt takes an index and returns the character at that index. indexOf takes a character and finds the index where that character appears.
charAt fails if the index is out of range, and causes an exception. indexOf fails if the character does not appear in the string, and returns the value -1.
This finds the index of the letter 'a' in the string. In this case, the letter appears three times, so it is not obvious what indexOf should do. According to the documentation, it returns the index of the first appearance.
In order to find subsequent appearances, there is an alternate version of indexOf (for an explanation of this kind of overloading, see Section 5.4). It takes a second argument that indicates where in the string to start looking. If we invoke
it will start at the twoeth letter (the first n) and find the second a, which is at index 3. If the letter happens to appear at the starting index, the starting index is the answer.
returns 5. Based on the documentation, it is a little tricky to figure out what happens if the starting index is out of range:
indexOf returns the index of the first occurrence of the character in the character sequence represented by this object that is greater than or equal to fromIndex, or -1 if the character does not occur.
One way to figure out what this means is to try out a couple of cases. Here are the results of my experiments:
If you go back and look at the documentation, you'll see that this behavior is consistent with the definition, even if it was not immediately obvious. Now that we have a better idea how indexOf works, we can use it as part of a program.
The following program counts the number of times the letter 'a' appears in a string:
This program demonstrates a common idiom, called a counter. The variable count is initialized to zero and then incremented each time we find an 'a' (to increment is to increase by one; it is the opposite of decrement, and unrelated to excrement, which is a noun). When we exit the loop, count contains the result.
The first time we invoke indexOf, we omit the second argument, which means that the search starts at the beginning of the word. Each subsequent time (inside the loop), indexOf starts looking at the position one to the right of the previous location, index+1. It is necessary to add 1 to index to avoid finding the same letter over and over.
As an exercise, encapsulate this code in a method named countLetters, and generalize it so that it accepts the string and the letter as arguments.
Incrementing and decrementing are such common operations that Java provides special operators for them. The ++ operator adds one to the current value of an int or char. -- subtracts one. Neither operator works on doubles, booleans or Strings.
Technically, it is legal to increment a variable and use it in an expression at the same time. For example, you might see something like:
Looking at this, it is not clear whether the increment will take effect before or after the value is printed. Because expressions like this tend to be confusing, I would discourage you from using them. In fact, to discourage you even more, I'm not going to tell you what the result is. If you really want to know, you can try it.
It may seem odd, but you can do arithmetic with characters! For example 'a' + 1 is 'b'. Similarly, if you have a variable named letter that contains a character, then letter - 'a' will tell you where in the alphabet it appears (keeping in mind that 'a' is the zeroeth letter of the alphabet and 'z' is the 25th).
This sort of thing is useful for converting between the characters that contain numbers, like '0', '1' and '2', and the corresponding integers. They are not the same thing. For example, if you try this
you might expect the value 3, but depending on your environment, you might get 51, which is the ASCII code that is used to represent the character '3', or you might get something else altogether. To convert '3' to the corresponding integer value you can subtract '0':
Technically, in both of these examples the typecast ((int)) is unnecessary, since Java will convert type char to type int automatically. I included the typecasts to emphasize the difference between the types, and because I'm a stickler about that sort of thing.
Since this conversion can be a little ugly, it is preferable to use the digit method in the Character class. For example:
converts letter to the corresponding digit, interpreting it as a base 10 number.
Another use for character arithmetic is to loop through the letters of the alphabet in order. For example, in Robert McCloskey's book Make Way for Ducklings, the names of the ducklings form an abecedarian series: Jack, Kack, Lack, Mack, Nack, Ouack, Pack and Quack. Here is a loop that prints these names in order:
Notice that in addition to the arithmetic operators, we can also use the conditional operators on characters. The output of this program is:
Of course, that's not quite right because I've misspelled "Ouack" and "Quack." As an exercise, modify the program to correct this error.
Here's a puzzler to see if you really know what's going on. Normally, the statement x++ is exactly equivalent to x = x + 1. Unless x is a char! In that case, x++ is legal, but x = x + 1 causes an error.
Try it out and see what the error message is, then see if you can figure out what is going on.
As you look over the documentation of the String methods, you might notice toUpperCase and toLowerCase. These methods are often a source of confusion, because it sounds like they have the effect of changing (or mutating) an existing string. Actually, neither these methods nor any others can change a string, because strings are immutable.
When you invoke toUpperCase on a string, you get a new string as a return value. For example:
After the second line is executed, upperName contains the value "ALAN TURING", but name still contains "Alan Turing".
It is often necessary to compare strings to see if they are the same, or to see which comes first in alphabetical order. It would be nice if we could use the comparison operators, like == and >, BUT WE CAN'T.
In order to compare Strings, we have to use the equals and compareTo methods. For example:
The syntax here is a little weird. To compare two things, you have to invoke a method on one of them and pass the other as an argument.
The return value from equals is straightforward enough; true if the strings contain the same characters, and false otherwise.
The return value from compareTo is a little odd. It is the difference between the first characters in the strings that differ. If the strings are equal, it is 0. If the first string (the one on which the method is invoked) comes first in the alphabet, the difference is negative. Otherwise, the difference is positive. In this case the return value is positive 8, because the second letter of "Ada" comes before the second letter of "Alan" by 8 letters.
Using compareTo is often tricky, and I never remember which way is which without looking it up, but the good news is that the interface is pretty standard for comparing many types of objects, so once you get it you are all set.
Just for completeness, I should admit that it is legal, but very seldom correct, to use the == operator with Strings. But what that means will not make sense until later, so for now, don't do it.