Java Methods: Understanding Definitions, Calling Process, and Decomposition, Lab Reports of Computer Science

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

Pre 2010

Uploaded on 07/28/2009

koofers-user-xj9
koofers-user-xj9 🇺🇸

10 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Mat 2170
Week 7
Methods Algorithms
Spring 2009
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download Java Methods: Understanding Definitions, Calling Process, and Decomposition and more Lab Reports Computer Science in PDF only on Docsity!

Mat 2170

Week 7

Methods – Algorithms

Spring 2009

Student Responsibilities

I (^) Reading: Textbook, Sections 5.2 – 5. I (^) Lab I (^) Attendance

Overview Chapter Five, Sections 2 — 5:

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 and Type

scope type name (argument list) { statements in the method body }

I (^) Scope: what code blocks have access?

  1. The most common value for scope is private, which means that the method is available only within its own class.
  2. If other classes need access to the method, scope should be public instead.

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.

Returning Values from a Method

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.

The factorial Method

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; }

Non–numeric Methods

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.

Predicate Methods

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; }

Predicate Methods

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); } }

Method: Powers of Two

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.

5.3 Mechanics of the Method–Calling Process

When you invoke a method, the following actions occur:

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.

The Combinations Function

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.

Combinations and Factorials

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)); }

5.4 Decomposition

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.

Choosing a Decomposition Strategy

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.