User Defined Methods
User Defined Methods
METHODS
November 8, 2022
METHODS
• Methods in Java are like automobile parts; they are
building blocks.
• Methods are used to divide complicated programs into
manageable pieces.
• Methods are often called modules.
• There are both predefined methods, methods that are
already written and provided by Java, and user-defined
methods, methods that you create.
• While working on one method, you can focus on just that part of the program and
construct it, debug it, and perfect it.
• Using methods greatly enhances the program’s readability because it reduces the
complexity of the method main.
a. Predefined Methods
b. Prewritten Methods
c. User-defined Methods
d. Preprogrammed Methods
3. Which of the following is
NOT true about methods?
System.out.print(Math.abs(-19.5));
9. Identify the output:
System.out.print(Math.ceil(25.27));
10. Identify the output:
System.out.print(Math.floor(18.73));
11. Identify the output:
System.out.print(Math.pow(2, 5));
12. Identify the output:
System.out.print(Math.sqrt(81));
13. Identify the output:
System.out.print(Math.round(15.23));
14. Identify the output:
System.out.print(Math.max(85, 80));
15. Identify the output:
System.out.print(Math.min(15, 23.1));
16. Identify the output:
System.out.print(Character.isDigit(‘1’));
17. Identify the output:
System.out.print(Character.isSpaceChar(‘ ’));
18. Identify the output:
System.out.print(Character.isLetter(‘R’));
19. Fill in the blank:
import javax.swing.JOptionPane;
public class CharacterClass1 {
public static void main(String[] args) {
char ch = JOptionPane.showInputDialog(null, "Enter a character").charAt(0);
if(Character.______________(ch)){
JOptionPane.showMessageDialog(null, "It is a letter");
}
}
20. Fill in the blank:
import javax.swing.JOptionPane;
public class Math_Class1 {
public static void main(String[] args) {
double num = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter a number"));
JOptionPane.showMessageDialog(null,
String.format("The square root of %.2f is %.2f", num, Math.______(num)));
}
}
USER-DEFINED
METHODS
November 10, 2022
User-Defined Methods
User-defined methods in Java are classified into two
categories:
• Value-returning methods—methods that have a
return data type. These methods return a value of a
specific data type using the return statement.
• Void methods—methods that do not have a return
data type. These methods do not use a return
statement to return a value.
Properties of Value-Returning
Method
1. The name of the method
2. The number of parameters, if any
3. The data type of each parameter
4. The data type of the value computed (that is, the value
returned) by the method, called the type of the method
5. The code required to accomplish the task
First four properties is what called heading; the fifth property
(the code) is called the body; The five properties is what we
called definition.
The variable declared in the heading, within the parentheses, of the
method abs is called the formal parameter of the method abs. Thus,
the formal parameter of abs is number
Formal parameter: A variable declared in the method heading.
Actual parameter: A variable or expression listed in a call to a method
Syntax: Value-Returning Method
Syntax: Value-Returning Method
modifier(s) indicates the visibility of the method, that is, where in a
program the method can be used (called). Some of the modifiers
are public, private, protected, static, abstract, and final
returnType is the type of value that the method returns. This type is
also called the type of the value-returning method.
methodName is a Java identifier, giving a name to the method.
Statements enclosed between braces form the body of the method
Syntax: Formal Parameter List
Method Call
Syntax: Actual Parameter List
Syntax: Actual Parameter List
return Statement
A value-returning method uses a return(s) statement to return
its value; that is, it passes a value back when the method
completes its task.