Page 288 - Introduction to Programming with Java: A Problem Solving Approach
P. 288
254 Chapter 7 Object-Oriented Programming—Additional Details
make
year
color
"Audi"
nathanCar
nickCar
The Car2 program in Figures 7.4 and 7.5 illustrates this example. Figure 7.5’s Car2 class defines an equals method, and Figure 7.4’s Car2Driver class calls the equals method while comparing two Car2 objects. As is common with equals method calls, Figure 7.4’s equals method call is embedded in the condition of an if statement. That should make sense when you realize that an if statement condi- tion must evaluate to true or false and an equals method does indeed evaluate to true or false. Typically, an equals method evaluates to true if the instance variables in two objects contain the same data values, and it evaluates to false otherwise. For our Car2 program, the equals method evaluates to true if nathanCar contains the same data (make, year, and color) as nickCar. Figure 7.4 shows that nathanCar and nickCar are assigned the same data. Therefore, the equals method returns true and the program prints “Cars havAe ipdeanticgalofeatuPresD.”F Enhancer
In the equals method call, note how the first Car2 reference variable, nathanCar, appears at the left of the .equals and the second Car2 reference variable, nickCar, appears inside the parentheses. Thus, nathanCar is the calling object, and nickCar is an argument This happens a lot when using two reference variables with a method call—one reference variable will be the calling object and the other one will be the argument.
Let’s now examine the equals method definition in Figure 7.5. First, note the equals method head- ing. Why is the return type boolean? Because the return type must match the type of the returned value, and equals methods always return a Boolean value (either true or false). Also note that the type of the otherCar parameter is Car2. That should make sense when you look back at the equals method call in Figure 7.4. It shows that the argument being passed into the equals method is nickCar, and nickCar is a Car2 reference variable.
OK, now it’s time to examine the body of the equals method. Notice that there is just one statement— the return statement. The return value must be a boolean, so the expression after the word return must evaluate to either true or false. This expression is an “anding” together of three boolean sub- expressions, each of which evaluates to either true or false. For the overall expression to be true all three of the sub-expressions must be true.
Each sub-expression checks whether a particular instance variable has the same value in the calling ob- ject and the passed-in parameter object. For example, to check whether the year instance variable has the same value in the calling object and the passed-in parameter object, we do this:
this.year == otherCar.year
In this case, we use the == operator to check for equality. That works fine for the year instance variable because year is an int. But the make and color instance variables are strings, and the == operator is
1998
"green"
"Audi"
make
year
color
1998
"green"