Module+6 Java+Methodsv3
Module+6 Java+Methodsv3
Module 6 :
JAVA METHODS
Objectives
After studying this module , students
should be able to:
➢Learn about Java Methods
➢Learn how to create a method
➢Learn how to use methods in Java programs
Java Method
➢ a group of statements that
performs a specific task when
called in a program
B. User-defined Methods
- methods created by the user
Examples:
• Math library (java.lang.Math)
• String library (java.lang.String)
• Graphics library (java.awt.* and javax.swing.*)
• Networking library (java.net)
Basic
Math
Methods
Math.abs()
The Math.abs() function returns the absolute value of the
parameter passed to it. The absolute value is the positive
value of the parameter. If the parameter value is negative,
the negative sign is removed and the positive value
corresponding to the negative value without sign is returned.
Examples:
int abs1 = Math.abs(10); // abs1 = 10
Math.ceil()
Example:
double ceil = Math.ceil(7.343);//ceil=8.0
Math.floor()
The Math.floor() function rounds a floating point
value down to the nearest integer value. The rounded
value is returned as a double.
example:
Double floor=Math.floor(7.343)//floor=7.0
Math.min()
The Math.min() method returns the smallest of two
values passed to it as parameter.
example:
int min = Math.min(10, 20);
Math.max()
The Math.max() method returns the largest of two
values passed to it as parameter.
example:
int max = Math.max(10, 20);
Math.round()
The Math.round() method rounds
a float or double to the nearest integer using normal
math round rules (either up or down).
example:
double roundedDown = Math.round(23.445);
double roundedUp = Math.round(23.545);
Math.random()
The Math.random() method returns a
random floating point number between 0 and
1. Of course the number is not fully random,
but the result of some calculation which is
supposed to make it as unpredictable as
possible.
example:
double random = Math.random();
Math.exp()
The Math.exp() function returns e (Euler's number)
raised to the power of the value provided as parameter.
example:
double exp1 = Math.exp(1);
System.out.println("exp1 = " + exp1);
Math.log()
The Math.log() method provides the logarithm of the
given parameter. The base for the logarithm is i(Euler's
number). Thus, Math.log() provides the reverse
function of Math.exp().
example:
double log1 = Math.log(1);
System.out.println("log1 = " + log1);
Math.log10()
Math.pow()
The Math.pow() function takes two parameters. The
method returns the value of the first parameter raised
to the power of the second parameter.
Example:
double pow2 = Math.pow(2,2);
System.out.println("pow2 = " + pow2);
examples:
double sqrt4 = Math.sqrt(4);
System.out.println("sqrt4 = " + sqrt4);
Math.PI
The Math.PI constant is
a double with a value that is very
close to the value of PI - the
mathematical definition of PI. You will
often need the Math.PI field when
making trigonometric calculations.
Math.sin()
The Math.sin() method calculates the sine value of
some angle value in radians.
Example:
double sin = Math.sin(Math.PI);
System.out.println("sin = " + sin);
Math.cos()
The Math.cos() method calculates the cosine
value of some angle value in radians.
Example:
double cos = Math.cos(Math.PI);
System.out.println("cos = " + cos);
Math.tan()
The Math.tan() method calculates the tangent
value of some angle value in radians.
example:
double tan = Math.tan(Math.PI);
System.out.println("tan = " + tan);
Math.asin()
The Math.asin() method calculates the arc
sine value of a value between 1 and -1.
example
double asin = Math.asin(1.0);
System.out.println("asin = "+ asin);
Math.acos()
The Math.acos() method calculates the arc
cosine value of a value between 1 and -1.
example:
double acos = Math.acos(1.0);
System.out.println("acos = " + acos);
Math.atan()
The Math.atan() method calculates the arc
tangens value of a value between 1 and -1.
example:
double atan = Math.atan(1.0);
System.out.println("atan = " + atan);
Math.atan2()
"Returns the angle theta from the conversion of
rectangular coordinates (x, y) to polar coordinates
(r, theta)".
If you need this method, please read the JavaDoc.
But now you know at least that it exists.
Math.sinh()
Math.cosh()
Math.tanh()
The Math.tanh() method calculates the hyperbolic
tangens value of a value between 1 and -1.
example:
double tanh = Math.tanh(1.0);
System.out.println("tanh = " + tanh);
Math.toDegrees()
Math.toRadians()
example:
double radians = Math.toRadians(180);
System.out.println("radians = "+ radians;
User-Defined Methods
User-Defined Methods
Value-returning methods
Used in expressions
Calculate and return a value
Can save value for later calculation or print value
Method Definition
Syntax:
modifier returnType
nameOfMethod(parameter list)
{
//body of method
}
➢ modifier − defines the access type of the method and this is optional
➢ returnType − return type of the method
➢ nameOfMethod −name of the method
➢ Parameter List − the type, order, and number of parameters of the
method. Some method does not have a parameter.
➢ method body − block of statements that performs the task
Syntax
Syntax: Formal Parameter List
-The syntax of the formal parameter list is:
Method Call
-The syntax to call a value-returning method is:
Syntax (continued)
if (x >= y)
max = x;
else
max = y;
return max;
}
Sample Program:
Void Methods
▪ Similar in structure to value-returning methods
Scope Rules
▪ Scope rules of an identifier declared within a class and accessed
within a method (block) of the class
▪ An identifier, say X, declared within a method (block) is accessible:
▪ Only within the block from the point at which it is declared until the
end of the block
▪ By those blocks that are nested within that block
▪ Suppose X is an identifier declared within a class and outside of
every method’s definition (block)
▪ If X is declared without the reserved word static (such as a
named constant or a method name), then it cannot be accessed in
a static method
▪ If X is declared with the reserved word static (such as a named
constant or a method name), then it can be accessed within a
method (block), provided the method (block) does not have any
other identifier named X
Method Overloading
public void methodOne(int x)
public void methodTwo(int x, double y)
public void methodThree(double y, int x)
public int methodFour(char ch, int x,
double y)
public int methodFive(char ch, int x,
String name)
Both these method headings have the same name and same
formal parameter list
Programming Exercises
1. Write a Java program that will ask the user to enter the side(s) of a
regular polygon, the number of sides(n) and its apothem(r) . The
program will then compute and display the perimeter(P) and area(A)
𝒓𝑷
of the polygon. Use the following formulas: A = P = sn
𝟐
Include the following methods in your program:
a. perimeter() – compute and return the perimeter to main()
b. area() – compute and return the area to main()
c. display() – display perimeter and area
Programming Exercises
2. Write a Java program that accepts the lengths of 2 sides of a right
triangle, a and b, respectively. The program then computes and displays
the hypotenuse and area of the triangle. The program should have the
following methods:
a. hypotenuse() – compute and return the hypotenuse to main() (use
Math.sqrt() )
b. area() – compute and return area to main()
c. display() – display hypotenuse and area
IMPORTANT: The Blackboard Discussion Board is a learning space. Let us use the BB
Discussion Board for learning purposes for the benefit of the entire class. Use decent
words when you create a thread. Also, sending personal message to your classmate is
prohibited.
Any Concerns?
Send an email to:
DOMAIN EMAIL ADDRESS
UST Domain xxxxx@ust.edu.ph
Image Citation:
Image Details
Artist: FixIcon
Iconset: The Lords Applications Icons (10 icons)
License: Free for personal desktop use only.
Commercial usage: Not allowed
Readme file: More_Icons.html
Image Citation:
Image Details
Artist: Icons8
Iconset: iOS 7 Icons (1738 icons)
License: Linkware (Backlink to http://icons8.com required)
Commercial usage: Allowed
License URL: http://icons8.com/license/
Artist: Untergunter
Iconset: Leaf Mimes Icons (67 icons)
License: CC Attribution-Noncommercial-Share Alike 4.0
Commercial usage: Not allowed
Image Citation:
Image Details
Artist: BlackVariant
Iconset: Button UI System Apps Icons (72 icons)
License: Free for non-commercial use.
Commercial usage: Not allowed
Artist: TpdkDesign.net
Iconset: Refresh Cl Icons (258 icons)
License: Free for non-commercial use.
Commercial usage: Not allowed
Readme file: readme_eng.txt
Image: www.pexels.com