Escolar Documentos
Profissional Documentos
Cultura Documentos
FUNCTIONS
Agenda
Example Problem
Lets solve together the Calculator Homework.
Solving Problems
Conclusion
We implemented several methods for solving little problems
How would code look like if we put everything in the main method?
We then used these methods in the main method
=> a method can call other methods in order to solve complex problems
Always try to break problems into sub-problems!
Functions are the first mechanism for doing this
Function mechanism is implemented in Java via static methods.
Rules of thumb
It is elegant to do one thing in one statement. If it doesnt fit, you can
implement a method for it.
You should implement one method whenever the same code is written
multiple times!
Flow of Control
Function call modifies the flow of control!
Java Methods
The only required elements of a method declaration are the method's return type, name, a
pair of parentheses, (), and a body between braces, {}.
Modifierssuch as public, private, and others you will learn about later.
The return typethe data type of the value returned by the method, or void if the method
does not return a value.
The method namethe rules for field names apply to method names as well, but the
convention is a little different.
The parameter list in parenthesisa comma-delimited list of input parameters, preceded by
their data types, enclosed by parentheses, (). If there are no parameters, you must use
empty parentheses.
An exception listto be discussed later.
The method body, enclosed between bracesthe method's code, including the declaration
of local variables, goes here.
Static Methods
By now we used pre-defined methods and wrote only the main() method
Java statements are grouped together in methods
Each method must be inside a class
static methods dont need an object (well see later what an object is):
Integer.parseInt(10);
Terminology
The method which calls another method is called a client.
The act itself is named function call or method invocation.
Clients and Libraries
Terminology
The method which calls another method is called a client.
The act itself is named function call or method invocation.
The main() method makes two function calls to the readInt() method
Clients and Libraries
Libraries
In our activity we deal with complex problems:
make secure online payments
process images (rotate, translate, resize, )
Implement a GUI (in Windows, Linux, Mac, ): we need windows,
buttons, check-boxes, etc
Clients and Libraries
Libraries
In our activity we deal with complex problems:
make secure online payments
process images (rotate, translate, resize, )
Implement a GUI (in Windows, Linux, Mac, ): we need windows,
buttons, check-boxes, etc
We dont want (and dont have time) to implement all these ourselves
Wed like to be able to include this functionality when needed and just call
some methods
=> concentrate on our problem and re-use code written by somebody else
Clients and Libraries
Libraries
In our activity we deal with complex problems:
make secure online payments
process images (rotate, translate, resize, )
Implement a GUI (in Windows, Linux, Mac, ): we need windows,
buttons, check-boxes, etc
We dont want (and dont have time) to implement all these ourselves
Wed like to be able to include this functionality when needed and just call
some methods
=> concentrate on our problem and re-use code written by somebody else
Welcome to libraries!
They simply contain classes and methods
And are packed in .jar files (similar to .zip files)
Clients and Libraries
Libraries
In our activity we deal with complex problems:
make secure online payments
process images (rotate, translate, resize, )
Implement a GUI (in Windows, Linux, Mac, ): we need windows,
buttons, check-boxes, etc
We dont want (and dont have time) to implement all these ourselves
Wed like to be able to include this functionality when needed and just call
some methods
=> concentrate on our problem and re-use code written by somebody else
Welcome to libraries!
They simply contain classes and methods
And are packed in .jar files (similar to .zip files)
e.g. opencv-2.4.6.jar means opencv (open computer vision) version 2.4.6
Clients and Libraries
Libraries
In our activity we deal with complex problems:
make secure online payments
process images (rotate, translate, resize, )
Implement a GUI (in Windows, Linux, Mac, ): we need windows,
buttons, check-boxes, etc
We dont want (and dont have time) to implement all these ourselves
Wed like to be able to include this functionality when needed and just call
some methods
=> concentrate on our problem and re-use code written by somebody else
Welcome to libraries!
They simply contain classes and methods
And are packed in .jar files (similar to .zip files)
e.g. opencv-2.4.6.jar means opencv (open computer vision) version 2.4.6
Using it is easy:
add the path of the .jar file to the CLASSPATH environment variable.
Import the needed package member (class, method, constant, ) in
your class
Activity