0% found this document useful (0 votes)
96 views

User Defined Methods

The document discusses user-defined methods in Java. It states that user-defined methods are methods created by the programmer, as opposed to predefined methods provided by Java. User-defined methods are classified as either value-returning methods, which use a return statement to return a value, or void methods, which do not return a value. The syntax for defining value-returning methods is explained, including the method header with modifiers, return type, name, and parameter list, as well as the method body and use of return statements.

Uploaded by

klokil bye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

User Defined Methods

The document discusses user-defined methods in Java. It states that user-defined methods are methods created by the programmer, as opposed to predefined methods provided by Java. User-defined methods are classified as either value-returning methods, which use a return statement to return a value, or void methods, which do not return a value. The syntax for defining value-returning methods is explained, including the method header with modifiers, return type, name, and parameter list, as well as the method body and use of return statements.

Uploaded by

klokil bye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

USER-DEFINED

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.

11/11/2022 QUARTER 2-MODULE 1 2


Using methods has several
advantages

• While working on one method, you can focus on just that part of the program and
construct it, debug it, and perfect it.

• Different people can work on different methods simultaneously.

• If a method is needed in more than one place in a program, or in different


programs, you can write it once and use it many times.

• Using methods greatly enhances the program’s readability because it reduces the
complexity of the method main.

11/11/2022 QUARTER 2-MODULE 1 3


Predefined-methods
• For example, pow(2, 3) is 8.0 and pow(2.5, 3) is 15.625. Because
the value of pow(x, y) is of type double, we say that the
method pow is of type double or that the method pow returns a
value of type double. Also, x and y are called the parameters
(or arguments) of the method pow. The method pow has two
parameters.
• The square root method, sqrt(x), calculates the nonnegative
square root of x for x >= 0.0. For example, sqrt(2.25) is 1.5. The
method sqrt is of type double and has only one parameter.

11/11/2022 QUARTER 2-MODULE 1 4


Predefined-methods
• In Java, predefined methods are organized as a collection of
classes, called class libraries.
• For example, the class Math contains mathematical methods.
• Table 7-1 lists some of Java’s predefined mathematical
methods. The table gives the name of the method (in bold type),
number of parameters, the data type of the parameters, and the
method type.
• The method type is the data type of the value returned by the
method.
• The table also shows how a method works.
• The class Math is contained in the package java.lang.

11/11/2022 QUARTER 2-MODULE 1 5


Thank you
Prepare for a quiz tomorrow ☺
QUIZ
November 9, 2022
1.Predefined methods in Java
are organized as a collection of
classes known as ________.
a. Method libraries
b. Package libraries
c. Class libraries
d. Modules
2. Methods that are already
written and provided by Java.

a. Predefined Methods
b. Prewritten Methods
c. User-defined Methods
d. Preprogrammed Methods
3. Which of the following is
NOT true about methods?

a. They are also called modules


b. They are ideal for shorter programs.
c. It enhances program's readability
d. It is used to divide complicated programs into
manageable pieces
4. Which of the following
statements is INCORRECT?
a. Math.sin(10);
b. Math.pow(2, 4);
c. Math.sqrt(-10);
d. Math.tan(6);
5. What package contains the
Math class?
a. java.util
b. java.io
c. javax.swing
d. java.lang
6. Which of the following
statements is INCORRECT?
a. Math.sin(5);
b. Math.cos(6);
c. Math.pow(7);
d. Math.tan(8);
7. It is the data type of the
value returned by the method.
a. Method type
b. Method name
c. Parameter
d. Argument
8. Identify the output:

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.

SYNTAX: return STATEMENT


return Statement
return Statement

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy