



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An introduction to java methods, including an overview of method calls, static methods in the math class, writing your own methods, and methods involving control statements. It covers the general form of a method definition, returning values, and using control statements within methods.
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!
Chapter 5—Methods The Art and Science of An Introduction
C H A P T E R 5 With method and logic one can accomplish anything. —Agatha Christie, Poirot Investigates, 1924
(adapted from slides by Eric Roberts)
Math.abs( x ) Returns the absolute value of x Math.min( x , y ) Returns the smaller of x and y Math.max( x , y ) Returns the larger of x and y Math.sqrt( x ) (^) Returns the square root of x Math.log( x ) (^) Returns the natural logarithm of x (log e x ) Math.exp( x ) (^) Returns the inverse logarithm of x ( e x^ ) Math.pow( x , y ) Returns the value of x raised to the y power ( x y^ ) Math.sin( theta ) Returns the sine of theta, measured in radians Math.cos( theta ) Returns the cosine of theta Math.tan( theta ) Returns the tangent of theta Math.asin( x ) Returns the angle whose sine is x Math.acos( x ) Returns the angle whose cosine is x Math.atan( x ) Returns the angle whose tangent is x Math.toRadians( degrees ) Converts an angle from degrees to radians Math.toDegrees( radians ) Converts an angle from radians to degrees
scope type name ( parameter declaration list ) { statements in the method body }
Parameter lists contain pairs, consisting of type names followed by identifiers. These identifiers are local variable names. Calling a method with an argument list is exactly the same as initializing each of the local variables declared in the parameter list. Whenever you are considering local variable names within a method, you must also consider each parameter name to be a local variable.
When you call a method, the following actions occur: The argument list in the method call is evaluated in the context of the calling method.
Each argument’s value is copied into the corresponding parameter variable. Assignment follows the order in which the arguments appear: the first argument is copied into the first parameter variable, and so on.
Execution continues from where it left off in the calling method.
If a method doesn’t return anything, you specify its return type as void. A very simple method that returns nothing is shown below: private void printDirections() { this.println("Welcome to my program!"); this.println("To get started, click "+ "anywhere in the window."); } This "void" method would be called by the statement this.printDirections(); in either the constructor or in some other method in the same class body. Instead of a return value, this method has a side- effect , printing text to the console.
One of the most important advantages of methods is that they make it possible to break a large task down into successively simpler pieces. This process is called decomposition. Complete Task Suppose, for example, that you have been given a large task that seems too large to code in a single constructor. Subtask 1 Subtask 2 Subtask 3 Before you start writing any code, you should think about the problem with an eye toward breaking the complete task into simpler subtasks. Subtask 2a Subtask 2b Some of the subtasks may themselves be so difficult that it makes sense to break them into even smaller subtasks. You can then continue the process until each individual subtask is manageable. One of the most important advantages of methods is that they make it possible to break a large task down into simpler pieces, a process called decomposition. Once you have completed the decomposition, you can then write a method to implement each subtask.