






















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
An overview of methods and algorithms in java, including writing your own methods, the mechanics of method calling, decomposition, and algorithmic methods. It covers the general form of a method definition, scope, type, argument list, and return statements. The document also discusses various types of methods, such as non-numeric methods, methods returning graphical objects, and predicate methods.
Typology: Lab Reports
1 / 30
This page cannot be seen from the preview
Don't miss anything!























Methods – Algorithms
Spring 2009
I (^) Reading: Textbook, Sections 5.2 – 5. I (^) Lab I (^) Attendance
I (^) 5.2 Writing your own methods I (^) 5.3 Mechanics of the method–calling process I (^) 5.4 Decomposition I (^) 5.5 Algorithmic methods
scope type name (argument list) { statements in the method body }
I (^) Scope: what code blocks have access?
I (^) If a method does not return a value, type should be void. Such methods are called procedures.
I (^) If a method has a return type other than void, then it must return a value.
I (^) You can return a single value from a method by including a return statement, which is usually written as:
return expression;
where expression is a Java expression that specifies the value the method is to return
I (^) As an example, the method definition:
private double feetToInches (double feet) { return 12.0 * feet; }
converts an argument indicating a distance in feet to the equivalent number of inches, and returns this calculated value to the caller.
I (^) The factorial of a number n (which is usually written as n!) is defined to be the product of the integers from 1 up to n. Thus, 5! is 120, or 1 × 2 × 3 × 4 × 5.
I (^) The following method definition uses a for loop to compute the factorial function: private int factorial (int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }
Methods in Java can return values of any type. The following method, for example, returns the English name of the day of the week, given a number between 0(Sunday) and 6(Saturday):
private String weekdayName (int day) { switch (day) { case 0: return "Sunday"; case 1: return "Monday"; case 2: return "Tuesday"; case 3: return "Wednesday"; case 4: return "Thursday"; case 5: return "Friday"; case 6: return "Saturday"; default: return "Illegal weekday"; } }
(String is a class defined in the package java.lang.) There is no need for a break statement following a return statement.
I (^) Methods that return a boolean value play an important role in programming and are called predicate methods.
I (^) As an example, the following method returns true if the first argument is divisible by the second, and false otherwise:
private boolean isDivisibleBy (int x, int y) { return x % y == 0; }
I (^) Once you have defined a predicate method, you can use it just like any other Boolean value.
I (^) For example, you can print the integers between 1 and 100 that are divisible by 7:
for (int i = 1; i <= 100; i++) { if (isDivisibleBy(i, 7)) { println(i); } }
I (^) The following method takes an integer n and returns true if n is a power of two, and false otherwise. I (^) The powers of 2 are 1, 2, 4, 8, 16, 32, and so forth; numbers that are less than or equal to zero cannot be powers of two.
private boolean isPowerOfTwo (int n) { if (n < 1) return false; while (n > 1) { if (n % 2 == 1) return false; n /= 2; } return true; }
I (^) If at any time it is discovered that the value is not a power of 2, false is returned. If execution drops out of the loop, then the original number was a power of 2, and true is returned.
I (^) The argument expressions are evaluated (in the context of the calling method)
I (^) Each (primitive type) argument value is copied into the corresponding parameter variable, which is allocated in a newly assigned region of memory called a stack frame.
This assignment follows the order in which the arguments appear: the first argument is copied into the first parameter variable, and so on.
I (^) To illustrate method calls, the text uses a function C (n, k) that computes the combinations function — the number of ways one can select k elements from a set of n objects.
I (^) Suppose, for example, that you have a set of five coins:
I (^) How many ways are there to select two coins? penny + nickel nickel + dime dime + quarter quarter + dollar penny + dime nickel + quarter dime + dollar penny + quarter nickel + dollar penny + dollar for a total of 10 ways.
I (^) Fortunately, mathematics provides an easier way to compute the combinations function than by counting out all the ways.
I (^) The value of the combinations function is given by the formula:
C (n, k) =
n! k! × (n − k)!
I (^) Given that we already have a factorial() method, it is easy to turn this formula directly into a Java method:
private int combinations (int n, int k) { return factorial(n) / (factorial(k) * factorial(n−k)); }
I (^) 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
Subtask 1 Subtask 2 Subtask 3
Subtask 2a Subtask 2b
I (^) Once you have completed the decomposition, you can then write a method to implement each subtask.
I (^) One of the most subtle aspects of programming is the process of deciding how to decompose large tasks into smaller ones.
I (^) In most cases, the best decomposition strategy for a program follows the structure of the real–world problem that program is intended to solve.
I (^) If the problem seems to have natural subdivisions, those subdivisions usually provide a useful basis for designing the program decomposition.
I (^) Each subtask in the decomposition should perform a function that is easy to name and describe.