{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

05-Methods - TheArtandScienceof CHAPTER 5 ERICS.ROBERTS Jav...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Jav a Methods C H A P T E R 5 With method and logic one can accomplish anything. —Agatha Christie, Poirot Investigates, 1924 5.1 A quick overview of methods 5.2 Writing your own methods 5.3 Mechanics of the method-calling process 5.4 Decomposition 5.5 Algorithmic methods
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
A Quick Overview of Methods You have been working with methods ever since you wrote your first Java program in Chapter 2. The run method in every program is just one example. Most of the programs you have seen have used other methods as well, such as println and setColor . At the most basic level, a method is a sequence of statements that has been collected together and given a name. The name makes it possible to execute the statements much more easily; instead of copying out the entire list of statements, you can just provide the method name. The following terms are useful when learning about methods: Invoking a method using its name is known as calling that method. The caller can pass information to a method by using arguments . When a method completes its operation, it returns to its caller. A method can pass information to the caller by returning a result .
Image of page 2
Methods and Information Hiding One of the most important advantages of methods is that they make it possible for callers to ignore the inner workings of complex operations. When you use a method, it is more important to know what the method does than to understand exactly how it works. The underlying details are of interest only to the programmer who implements a method. Programmers who use a method as a tool can usually ignore the implementation altogether. The idea that callers should be insulated from the details of method operation is the principle of information hiding , which is one of the cornerstones of software engineering.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Methods as Tools for Programmers Particularly when you are first learning about programming, it is important to keep in mind that methods are not the same as application programs, even though both provide a service that hides the underlying complexity involved in computation. The key difference is that an application program provides a service to a user, who is typically not a programmer but rather someone who happens to be sitting in front of the computer. By contrast, a method provides a service to a programmer, who is typically creating some kind of application. This distinction is particularly important when you are trying to understand how the applications-level concepts of input and output differ from the programmer-level concepts of arguments and results. Methods like readInt and println are used to communicate with the user and play no role in communicating information from one part of a program to another.
Image of page 4
Method Calls as Expressions Syntactically, method calls in Java are part of the expression framework. Methods that return a value can be used as terms in an expression just like variables and constants.
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern