ICT 204 - Lecture 4 Methods
ICT 204 - Lecture 4 Methods
Java
An Introduction
to Computer Science
ERIC S. ROBERTS
LESSSON 5
Methods
With method and logic one can accomplish anything.
—Agatha Christie, Poirot Investigates, 1924
receiver.name(arguments);
Writing Your Own Methods
• The general form of a method definition is
return expression;
• Once you have defined a predicate method, you can use it just
like any other Boolean value. For example, you can print the
integers between 1 and 100 that are divisible by 7 as follows:
for (int i = 1; i <= 100; i++) {
if (isDivisibleBy(i, 7)) {
println(i);
}
}
Using Predicate Methods Effectively
• New programmers often seem uncomfortable with Boolean
values and end up writing ungainly code. For example, a
beginner might write isDivisibleBy like this:
private boolean isDivisibleBy(int x, int y) {
forif
(int
(x i
% =y 1;
== i
0)<=
{ 100; i++) {
if return
(isDivisibleBy(i,
true; 7) == true) {
println(i);
} else {
} return false;
} }
}
output
Combination 10, 2 is: 45
Methods
Create a method in java for the following:
• Even Number Detection
• Prime Number Detecting
• Finding Greatest Common Divisor
• Multiplication using repeated Addition
• Leap Year Detection
Decomposition
One
Some
Suppose,
Before
ofofyou
the
the
formost
start
subtasks
example,
writing
important
may
thatany
themselves
youadvantages
code,
have you
been
be of
so
should
given
difficult
methodsathink
large
that
is about
that
task
it makes
they
that
the
seems to
sense
make
problemittoopossible
largean
break
with to to
themcode as aeven
eyebreak
into
towardasingle
large run
taskmethod.
breaking
smaller subtasks.
down
the complete
intoYou successively
task
can then
into
continuepieces.
simpler subtasks.
the process
This until
process
each is individual
called decomposition.
subtask is manageable.
Complete Task
Subtask 2a Subtask 2b
Once you have completed the decomposition, you can then write
a method to implement each subtask.
Choosing a Decomposition Strategy
• One of the most subtle aspects of programming is the process
of deciding how to decompose large tasks into smaller ones.
• In most cases, the best decomposition strategy for a program
follows the structure of the real-world problem that program
is intended to solve. If the problem seems to have natural
subdivisions, those subdivisions usually provide a useful basis
for designing the program decomposition.
• Each subtask in the decomposition should perform a function
that is easy to name and describe.
• One of the primary goals of decomposition is to simplify the
programming process. A good decomposition strategy must
therefore limit the spread of complexity. As a general rule,
each level in the decomposition should take responsibility for
certain details and avoid having those details percolate up to
higher levels.
Algorithmic Methods
• Methods are important in programming because they provide
a structure in which to express algorithms. Algorithms are
abstract expressions of a solution strategy. Implementing an
algorithm as a method makes that abstract strategy concrete.
• Algorithms for solving a particular problem can vary widely
in their efficiency. It makes sense to think carefully when you
are choosing an algorithm because making a bad choice can
be extremely costly.
• Section 5.5 in the text looks at two algorithms for computing
the greatest common divisor of the integers x and y, which is
defined to be the largest integer that divides evenly into both.
Brute-Force Approaches
• One strategy for computing the greatest common divisor is to
count backwards from the smaller value until you find one
that divides evenly into both. The code looks like this:
public int gcd(int x, int y) {
int guess = Math.min(x, y);
while (x % guess != 0 || y % guess != 0) {
guess--;
}
return guess;
}