# Tracing Objects – Intro to Java Programming

Now that you’ve seen a lot of code for implementing methods. Let me show you a technique that can help you understand what a method implementation does. It’s called the object tracing technique. So, let’s say that you have a, a couple of methods that do some work that may not be so easy to understand. Here’s what you do. Pick some scenario, that exercises these methods. Like here, I’m making a car, I add gas, I drive it, and I drive it a bit more. And now we want to understand, what do these methods calls do to the car. It’s very low tech. Get yourself an index card or a napkin or some other piece of paper. Make a column for each instance variable. So, let’s get started. I put the paper clip so we can see where we are. We construct a car, that initialized the instance variables. I didn’t have room to put in the code for the constructor but hopefully you remember. The gas and tank and miles driven are initialized to zero. The miles per gallon with the parameter of the constructor. Let’s move on. Now we enter the add gas method here. Amount is thirty. Gas in tank is zero. So, the effective set gas in tank is set to thirty. Moving on. Now we drive ten miles. So, distance is ten. And since this method is a bit longer and it has another local variable here, it makes sense to make another card for it. Or actually, since I ran out of simulated cards, I’ll use a napkin. So the distance parameter is 10, over here. We update miles driven, previously it was 0. We add 10. That’s now 10. Now here we compute the gas consumed. That’s another local variable. I’ll put it on my napkin. It’s the distance divided by miles per gallon. 10 divided by 10. So, we consumed a gallon. Now we can look at this statement here. Gas in tank was 30. Gas consumed was 1. The difference is 29. So, that should now by moved into gas in tank. Now, we’re at the end of this method call, and the local variable here and the parameter variable go away. So, we can just crumple up our napkin here. These variables are no longer there once we exited this method. Of course, the instance variables, the long-term memory of the object, they stay around. Now here we are on the next method call, we’re calling the drive method again. That means that this local variable, and this one here, will get recreated. Of course this time the distance is 20. Now let’s see, miles driven is, miles driven plus distance. Previously it was 10. Now we have 20, now it becomes 30. Gas consumed is distance over miles driven, that’s 20 by 10 or two. Gas in tank was 29 minus two is 27. So, now we’ve executed the three steps in this method. This method is now finished. And once again, the local variables go away. And we’re done, and this is the remaining state of the object. What’s the point? By doing this exercise, you gain an understanding of what the statements in the methods do to the instance variables of an object. Sarah will walk you through an example on how you can use this technique to find a bug in a method implementation.