Title: Chapter 2: Applications
1Xavier University Computer Center Sun
Microsystems, Philippines
IM 06 Advanced Programming Language
2Chapter 6
Methods
3Chapter 6
- Outline
- Why Use Methods
- Creating Custom Methods
- MATH Class Methods
- Java API Packages
- Method Overloading
- Variable Scope
- Objects and Methods
4Objectives
- At the end of these sessions, the students are
expected to be able to - Explain the uses of methods
- Use the MATH class methods
- Determine the scope of variables
- Determine the methods of hypothetical program
objects
5Introduction to MethodsWhat are methods?
- Functional units of a class
- Think of them as functions or tasks of a whole
6Introduction to MethodsWhy use methods?
- Think of how students accomplish their group
projects division of labor. - Think of how companies are organized into
functional units
- Production
- Marketing
- Promotion
- Sales
- Accounting
- Human Resources
- Information Management
7Introduction to MethodsWhy use methods?
- Suppose your group is tasked to create school
supplies packages for an outreach program to poor
children. The package will contain several pens
of different colors, crayons, several types of
papers, and some notebooks. What are the tasks
needed to create a school supplies package?
8Advantages of Using Methods
- Facilitates
- Program construction
- Testing and Debugging
- Program Maintenance
- In programming, this procedure is called DIVIDE
and CONQUER - Eliminates repetitive code
- Promotes software reuse
9Creating Custom Methods
- Create a program to determine the higher numbers
among these sets - 10 or 20
- 10280 or 202
- (21020) or (12010)
10 // This program determines the higher of
numbers public class higher public static
void main(String a) if (10gt20)
System.out.println(The first is
higher) else System.out.println(The
second is higher) if ( (101080) gt (2020)
) System.out.println(The first is
higher) else System.out.println(The
second is higher) if ( (21020) gt (12010)
) System.out.println(The first is
higher) else System.out.println(The
second is higher)
11Creating Custom Methods
- We can make the previous code shown a lot more
efficient by defining a METHOD that does the same
thing
12 // This program determines the higher of
numbers public class higher //here is my
method to find the maximum public static
void max(int first, int second) if (first gt
second ) System.out.println(The first is
higher) else System.out.println(The
second is higher) public static
void main(String a) max(10,20)
max(101080,2020) max(21020,12010)
System.exit(0)
13 max(10,20) This statement invokes or
calls the method named max. It sends in the
numbers 10 and 20 as arguments. public static
void max(int first, int second) This
statement defines a method named max that accepts
two parameters both of type int. The variable
first accepts the first argument sent, which was
10 in this case, and the variable second accepts
the next number, which was 20. The void keyword
signifies that it does not return a value to the
command that invoked it.
14 // This program determines the higher of
numbers public class higher //here is my
method to find the maximum public static int
max(int first, int second) if (first gt second
) return first else return second
public static void main(String a)
int higherNum higherNummax(10,20)
System.out.println(higherNum) System.out.print
ln(max(101080,2020)) System.out.println(max(
21020,12010)) System.exit(0)
15 public static int max(int first, int
second) if (first gt second ) return
first else return second
Notice that in this case, the method was declared
to be of type int. That means that we are
expecting this method to return a number of type
int. The return command specifies what that
number is. higherNummax(10,20)
System.out.println(higherNum)
System.out.println(max(101080,2020)) Since
we know that method max returns a number, we need
a variable of type int such as higherNum to catch
it and output it afterwards, or output it
directly as in the last line.
16Using the MATH class
- An alternative to the code presented is using
predefined methods in Javas library - The MATH class contains many of these predefined
methods - No need to reinvent the wheel
17 // This program determines the higher of
numbers public class higher public static
void main(String a)
System.out.println( MATH.max(10,20) )
System.out.println( MATH.max(101080,2020)
) System.out.println(
MATH.max(21020,12010) ) System.exit(0)
18(No Transcript)
19The Java API
- Java has a very extensive library of predefined
methods - These methods are defined inside classes (like
MATH and JOptionPane), which are classified into
packages (like javax.swing) - This is called the Java API (Application Program
Interface)
20(No Transcript)
21Method Overloading
- Back to the method on finding the maximum of two
numbers - public static int max(int first, int second)
- Suppose we want to create similar methods that
accepts and returns double values instead, or
that accepts three ints instead
22Method Overloading
- We can create several definitions of this same
method with different return types or different
parameters - public static int max(int first, int second)
- public static double max(double first, double
second) - public static int max(int first, int second, int
third) - This is called method overloading.
23Method Overloading
- public static int max(int first, int second)
- public static double max(double first, double
second) - public static int max(int first, int second, int
third) - Even though these methods have the same names,
they have different return types or different
parameters. And so the computer is not confused. - Java finds the most appropriate method for every
method call
24Method Overloading
- public static int max(int first, int second)
- public static double max(double first, double
second) - public static int max(int first, int second, int
third) - Exercise Which of the three methods above will
be called by these statements below - max (2,4) max (2.0, 4.0)
- max (3,4,5) max ((int)3.0,2)
25Exercises
- Create a program (hypotenuse.java) that allows a
user to input the two sides of a right triangle.
The program should compute for the hypotenuse of
the triangle. - (Formula c2 a2 b2)
- Your program should have this method
- public static double hypotenuse(int sideA, int
sideB) - Note that the two parameters are int while the
return type is double.
26Exercise
- Create a program (multiple.java) that allows the
user to enter two numbers (lets say x and y). The
program should determine if y is a multiple of x,
meaning, x divided by y will give a quotient
which is a whole number. (Hint use MATH.floor( )
to determine if the quotient is indeed a whole
number or you can use the MOD function instead)
27Scope of variables
- Local variables/automatic variables
- Exists only within the block they are created
- After the block ceases execution, these variables
die away and are collected by Javas garbage
collector - Local variables should be initialized before using
28Local Variables
- For example
- for (int a0 alt10 a)
- System.out.print(The next number is )
- System.out.println(a)
-
- System.out.println(a)
- This will produce an error, since the variable
a no longer existed outside the curly braces
29Local Variables
- public static void max(int first, int second)
- Here, the variables first and second exists
only within the curly braces of the method max - public static void main(String args )
- max(10,20)
- System.out.println(first)
-
- This will produce an error, since the variable
first does not exist within the method main
30Scope of variables
- Local variables/automatic variables
- Class variable/global variables
- Declared right after class definitions
- public class duration
- int a,b // these are class variables
- public static void main(String arg )
-
- Can be modified by any method in the class
31Exercise
- Determine the output of the following code
- public class scope
- int x10
- public static void main(String arg )
- methodB( )
- System.out.println(x)
- methodA( )
- methodA( )
- methodB( )
- System.out.println(x)
-
- public static void methodA( )
- x x 2
- public static void methodB( )
- x x 5
-
32Scope Rules
- Global variables / class variables can be
accessed and modified by any method in the class - Local variables only exists within the block that
created it - If a local variable in a method has the same name
as a class variable, the class variable is
hidden until the block terminates execution
33Exercises
- Determine the output of the following
- public class scope
- int x10
- public static void main(String arg )
- x
- System.out.println(x)
- methodA( )
- methodB( )
- public static void methodA( )
- int x 5
- System.out.println(x)
- public static void methodB( )
- System.out.println(x)
-
34 public class scoping int x1 public
static void main(String a) int
x5 System.out.println(x) methodA(
) methodB( ) methodA( ) methodB(
) System.out.println( ) public
static void methodA( ) int x25 x System.
out.println(x) public static void
methodB( ) x10 System.out.println(x)