Java Math Methods2
Java Math Methods2
Lecture 9
CGS 3416 Spring 2016
I Divide-and-conquer
I Breaking up programs and algorithms into smaller, more
manageable pieces
I This makes for easier writing, testing, and debugging
I Also easier to break up the work for team development
I Reusability
I Functions can be called to do their tasks anywhere in a
program, as many times as needed
I Avoids repetition of code in a program
I Functions can be placed into libraries to be used by more than
one ”program”
I With methods (functions), there are 2 major points of view
I Builder of the method – responsible for creating the
declaration and the definition of the method (i.e. how it works)
I Caller – somebody (i.e. some portion of code) that uses the
method to perform a task
Using Methods
System.out.print(Math.sqrt(100.0));
//returns 10.0, which gets printed
System.out.print(Math.sqrt(49));
//due to automatic type conversion rules
System.out.print( Math.sqrt(Math.sqrt(625.0)));
A special use of import for static methods
I To import all static methods from a class this way, use the *
wildcard character.
I For example:
import static java.lang.Math.*;
// import all static methods from Math
I It’s best to use this sparingly.
I If a code file is using multiple libraries, it can get confusing
what class different method calls are coming from, especially
if multiple classes have similarly named methods.
Building Methods
I printData(int x){ }
// missing return type
I Parameter lists
I Mathematical functions must have 1 or more parameters
I Java methods can have 0 or more parameters
I To define a method with no parameters, leave the parentheses
empty
I Same goes for the call. (But parintheses must be present, to
identify it as a method call).
I Return types
I A mathematical function must return exactly 1 answer
I A Java method can return 0 or 1 return value
I To declare a method that returns no answer, use void as the
return type
I A void method can still use the keyword return inside, but not
with an expression (only by itself). One might do this to force
early exit from a method.
I To CALL a void method, call it by itself – do NOT put it in
the middle of any other statement or expression
Sample Prototype
I The compiler will check all method CALLS to make sure they
match the expectations (which are described in the method
signature)
I method name must match
I arguments passed in a call must match expected types and
order
I returned value must not be used illegally
I static methods can be called through class name, but instance
methods only through an object
I Decisions about parameters and returns are based on
type-checking.
I legal automatic type conversions apply when passing
arguments into a method, and when checking what is returned
against the expected return type
Pass By Value
I Sample call:
int a = 5, b = 8, ans;
ans = myMethod(a, b);
System.out.println("ans = " + ans);
System.out.println("a = " + a);
System.out.println("b = " + b);
I Notice that the output of the code is:
x = 10
y = 16
ans = 26
a = 5
b = 8
Method Overloading
I Notice that although all three methods above have the same
exact name, they each have a different parameter list.
I Some of them differ in the number of parameters (2
parameters vs. 1 parameter), and the first two differ in types
(double vs. char).
I The compiler will distinguish which function to invoke based
on what is actually passed in when the function is called.
x = process(3.45,12); //invokes the third function