Mehran Sahami Handout #14 CS 106A October 8, 2007 Methods Based on a handout by Eric Roberts Chapter 5 introduces the concept of methods in Java, which are analogous to those you created in Karel. In contrast to the topics of expressions and control statements, the material in Chapter 5 involves far fewer details. On the other hand, the ideas introduced in this chapter are far more important. If you plot overall importance on a scale of 1 to 10, the switchstatement probably weighs in somewhere around 2 (and I'm being very generous here... poor old switchstatement); methods are definitely a 10. The common idea that links methods in Karel and Java is that both provide a service to other, higher-level parts of the program and therefore act as tools. In both languages, the runmethod can call subsidiary methods to accomplish parts of the overall task. Those methods in turn call other methods that perform simpler operations, and so on. The caller views the method in terms of the effect it accomplishes. The method supplies all the
This is the end of the preview.
access the rest of the document.