Shs - SLK - Ict Prog Creating Methods According To Java Framework

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Y T

LE ER
LEARNING RESOURCES MANAGEMENT AND DEVELOPMENT SECTION
SA P
R RO
FO P
T E NT
NO NM

SELF-LEARNING KIT
R
VE
GO

INDUSTRIAL
INFORMATION AND ARTS TECHNOLOGY
COMMUNICATIONS
PROGRAMMING NC III (JAVA TECHNOLOGY)
ENGLISH AT METRIC
CREATING METHODS ACCORDING
DALAWANG SISTEMA NG PANUKAT
TO JAVA FRAMEWORK

WRITER/LAYOUT ARTIST: KENNETH G. DOCTOLERO


EVALUATORS: MARY ANN S. VALDEZ / EMELITA G. PAGUIO GRADE
GRADE11-12
4
Book Record
School: _____________________________________________________________
District: _____________________________________________________________
Division: ____________________________________________________________
Region: _____________________________________________________________
Date received by school: _______________________________________________

Issued to Date Date


(Name of Teacher) Issued Condition Returned Condition

To the Teacher
Write your name clearly under the column “Issued to.” Use the following letters
in recording the condition of the book:
A New Book
B Used Book in Good Condition
C Used Book in Fair Condition
D Used Book in Poor Condition
Creating Methods According to Java Framework

SELF-LEARNING KIT
INFORMATION AND COMMUNICATIONS TECHNOLOGY

PROGRAMMING NC III (JAVA TECHNOLOGY)

CREATING METHODS ACCORDING


TO JAVA FRAMEWORK

3
This Self-Learning Kit was conceptualized and developed by the
LEARNING RESOURCES MANAGEMENT AND DEVELOPMENT SECTION (LRMDS)
SCHOOLS DIVISION OF BATAAN

ROMEO M. ALIP, PhD, CESO V


Schools Division Superintendent

ROLAND M. FRONDA, EdD, CESE


Assistant Schools Division Superintendent

MILAGROS M. PEÑAFLOR, PhD


Chief Education Supervisor, CID

EDGAR E. GARCIA
Education Program Supervisor, LRMDS
ROSITA P. SERRANO JOAN T. BRIZ RICHARD M. ALBORO
Librarian II Project Development Officer II Information Technology Officer II

MARLON Q. DIEGO OMAR S. MANALANSAN KENNETH G. DOCTOLERO JOVANNI B. BELMONTE


Lead Illustrator Lead Book Designer Lead Book Designer Lead Illustrator
Learning Resource Evaluators
EMELITA G. PAGUIO
ANNE MARIE R. SEÑORA-BONIFACIO, EdD
DONNA T. SANTOS-VILLANUEVA, MAEd
CATHREN DANICA E. RICAPLAZA
MARIO E. DOJILLO JR.
MARY ANN S. VALDEZ

COPYRIGHT NOTICE
Section 9 of Presidential Decree No. 49 provides:

No copy shall subsist in any work of the Government of the Philippines. However, prior approval of the government agency or
office wherein the work is created shall be necessary for exploitation of such work for profit. Such agency or office may, among other
things, impose as a condition the payment of royalties...

A “Work of the Government of the Philippines” is a work created by any officer or employee of the Philippine Government or
any of its subdivisions and instrumentalities, including government owned or controlled corporations as a part of his regularly prescribed
official duties.

Notwithstanding the foregoing provisions, the Government is not precluded from receiving and holding copyright transferred to
it by assignment, bequests or otherwise; nor shall publication or republication by the Government in a public document of any work in
which copyright is subsisting be taken to cause any abridgment or annulment of the copyright or to authorize any use of appropriation
of such work without the consent of the copyright proprietor.

This Self-Learning Kit was conceptualized and developed by the Learning Resources Management and Development Section
of the Department of Education Region III – Schools Division of Bataan. It can be reproduced, used, copied, printed or reprinted for
educational purposes use only, but the source must be clearly acknowledged. This storybook can also be modified for the purpose of
translation into another language or dialect, but the original work must be documented. Derivatives of the work including creating an
edited version, an enhancement or a supplementary work are permitted provided all original works are credited and the copyright is
attributed. No work may be derived from this material for commercial purposes or profit.

PRINTING AND REPRODUCTION MADE POSSIBLE THROUGH THE


SPECIAL EDUCATION FUND (SEF) OF THE PROVINCIAL GOVERNMENT OF BATAAN
AND THE DEDICATED LEADERSHIP OF
HON. GOVERNOR ALBERT RAYMOND S. GARCIA
Creating Methods According to Java Framework

PREFACE

This Self-Learning Kit (SLK) will assist you to strengthen your knowledge in
creating and invoking methods, as well as passing parameters to methods and
returning values from methods. This learning material is designed for grade 11 and
grade 12 students who are willing to learn on self-paced mode. This learning material
includes several challenges that will test your learning. It is best to read the content
of this learning material to achieve the objectives and conquer these challenges.

As impossible as it might seem to you, the principle of writing program has not
changed all that as years past. Programming languages changes, technologies get
modernized, integrated development environment get more and more advanced but
the creating and invoking of the methods remain the same. With the help of this SLK,
you can make your first step in advance programming. This learning material focuses
about methods which is one of the most significant components of programming.

In addition, remember the following tips to keep this learning material neat and
clean; do not write anything on its pages; use separate paper as your answer sheet;
read, understand and follow the instruction carefully; answer the given task after
reading the lesson; try to achieve at least an 80% level of proficiency in every activity.

5
Self-Learning Kit in Information and Communications Technology

OBJECTIVES

At the end of the lesson, with at least 80% level of proficiency, you should be able to:
1. identify the different elements of a method by naming the elements based on its
description;
2. express understanding in the correct syntax of method by re-arranging the parts of a
code to form a correct method; and
3. demonstrate proficiency in creating and invoking methods by completing the given
codes to create a program.

LEARNING COMPETENCIES

Create methods with arguments and return values in accordance with Java
framework. TLE_ICTJAVA11-12POAD-Id-g-36

WHAT DO YOU KNOW?

Welcome! Are you ready to go on a journey? Are you ready for some fun while
learning? Alright then, let’s start this awsome ride with a warm up activity.

INSTRUCTION: Match column A to column B to identify the elements of a method base


on the given description. Write your answer on a separate sheet of paper as your answer
sheet.
DESCRIPTION ELEMENT
1. It is the data type of the value the method returns. a. Method Body
2. It is a unique identifier and it is case sensitive. It cannot be
same as any other identifier declared in the class. b. Method Name
3. It enclosed between parentheses, it is used to pass and
c. Return Type
receive data from a method. This list refers to the type, order
and number of parameters of a method. d. Parameter Liwst
4. This determines the visibility of a variable or a method from
another class. e. Access Specifier
5. It contains the set of instructions needed to complete the
required activity.
Excellent! Did you have a hard time answering this warm up activity? Nothing to
worry! After reading this Self-Learning Kit, I assure you that the succeeding activity is as
easy as a pie. Let’s dig in to this magnificent learning material.
6
Creating Methods According to Java Framework

WHAT YOU NEED TO KNOW?

WHAT IS METHOD?

• A METHOD is a block of code which only runs when it called (w3schools,2019).

• A Java method is a collection of statements that are grouped together to perform an


operation.

• When you call the System.out.println() method, for example, the system actually
executes several statements in order to display a message on the console
(Tutorialpoint,2019).

ELEMENTS OF A METHOD,(TUTORIALPOINT,2019)

• Access Specifier – This determine the visibility of a variable or a method from another
class.
• Return Type – A method may return a value. The return type is the data type of the value
the method returns. If the method is not returning any value, then the return type is void.
• Method Name – Method name is a unique identifier and it is case sensitive. It cannot be
same as any other identifier declared in the class.
• Parameter List – Enclosed between parentheses, the parameters are used to pass and
receive data from a method. The parameter list refers to the type, order and number of
the parameters of a method. Parameters are optional; that is, a method may contain
no parameters.
• Method Body - This contains the set of instruction needed to complete the required
activity.

HOW TO DEFINE METHOD?

To define Method, basically declare the elements of its structure. The syntax for
defining method in Java is as follows:

<Access Specifier> <Return Type> <Method Name> (Parameter List)


{Method Body}

In Java, both Methods and Functions are same, there are no difference and
these are a just different terms to do the same thing.
7
Self-Learning Kit in Information and Communications Technology

EXAMPLE
The following code shows a method Addnum that takes two integer values and return

the sum of the two integer. It has Public access specifier, so it can be accessed outside the

class during an instance of the class.

CALLING A METHOD
Using the example method below, you can call a method using name of the

method. The following example illustrates this:

When the above code is compiled


and executed, it produces the
following result:

8
Creating Methods According to Java Framework

PASSING PARAMETERS TO METHODS


When a parameter is pass-by-value, the caller and the calling method operate on

two different variables which are copies of each other. Any changes to one variable don’t

modify the other.

It means that while calling a method, parameters passed to the calling method will

be clones of original parameters. Any modification done in calling method will have no

effect on the original parameters in caller method (Baeldung,2019).

PASSING PARAMETERS BY VALUE EXAMPLE

Following is the example of passing a value type parameter to a method AddNum.

If you observe above example, the variables a and b are value type and it passed

to the Addnum method. The value of variable a and b copies to the parameter num1 and

num2 respectively and made required modifications in Addnum method but the changes

that made inside of the method have no effect on the original value of the variable.

9
Self-Learning Kit in Information and Communications Technology

When the above code is compiled


and executed, it produces the
following result:

THE VOID KEYWORD

The void keyword allows us to create methods which do not return a value.

If we observe the above codes, we have created a method using the void keyword.
When the main method calls the PrintNum method it only executes the code inside it and not
returning anything from the caller method.

10
Creating Methods According to Java Framework

METHOD OVERLOADING

When a class has two or more methods by the same name but different parameters, it

is known as method overloading. It is different from overriding. In overriding, a method has the

same method name, type, number of parameters, etc.

Let’s consider the example discussed earlier for finding minimum numbers of integer

type. If, let’s say we want to find the minimum number of double types. Then the concept

of overloading will be introduced to create two or more methods with the same name but

different parameters (Tutorialpoint,2019).

The following example explains the same:

11
Self-Learning Kit in Information and Communications Technology

METHOD OVERLOADING

Overloading methods makes program readable. Here, two methods are given by the

same name but with different parameters. The minimum number from integer and double

types is the result.

TYPES OF METHODS (https://www.programiz.com,2019)

Depending on whether a method is define by the user, or available in standard

library, there are two types of methods:

1. Standard Library Methods

2. User-define Methods

STANDARD LIBRARY METHOD


The standard library methods are built-in methods in Java that are readily available

for use. These standard libraries come along with the Java Class Library in a Java archive.

For Example:

• print() is a method of java.io.PrintStream. The print(“..”) prints what ever string inside the

quotation marks.

• sqrt() is a method in Math class. It return the sqare root of a number.

USER-DEFINED METHOD
You can also defined methods inside a class as per your wish. Such methods are

called User-define methods.

For Example:

Addnum method is a User-


define method that returns the
sum of two numbers.

12
Creating Methods According to Java Framework

REMEMBER

Awesome! We are almost done, here are some tips to remember with:

• Methods are block of codes that only executes when called by your main method.

• Methods have five elements the Access Specifier, Return Type, Method Name, Parameter

List and last but not the least the Method Body.

• We can pass a value from method to method with the use of pass-by-value were as if

you pass a value from the calling method to the called method, any changes happened

inside the called method will not affect the value in the calling method.

IT’S CHALLENGE TIME!!!

Wonderful! The end of your journey is now nearing. Now it’s time to test what you have

learned from this journey. The are several activities along the way. Do your best to answer

those activities. Let’s start! Best of luck!

13
Self-Learning Kit in Information and Communications Technology

WHAT HAVE YOU LEARNED?

ACTIVITY # 1

INSTRUCTION: Match column A with column B to identify the elements of a method based
on the given description. Write your answer on a separate sheet of paper as your answer
sheet.

COLUMN A COLUMN B
1. It is the data type of the value the method returns. a. Method Body

2. It is a unique identifier and it is case sensitive. It cannot be

same as any other identifier declared in the class. b. Method Name

3. It enclosed between parentheses, it is used to pass and

receive data from a method. This list refers to the type, order c. Return Type

and number of parameters of a method.

4. This determines the visibility of a variable or a method from d. Parameter Liwst

another class.

5. It contains the set of instructions needed to complete the e. Access Specifier

required activity.

14
Creating Methods According to Java Framework

ACTIVITY # 2

INSTRUCTION: Rearrange the codes to create a valid and correct syntax method named

“CheckNum” that will display if a number is odd or even. Use numbers 1 to 10 to arrange the

codes on a separate sheet of paper.

______1. CheckNum (int a)

______2. If

______3. Private Static

______4. Void

______5. (a % 2 == 0 )

______6. Else

______7. {System.out.println(“Even”);}

______8. { System.out.println (“Odd”);}

______9. }

______10. {

15
Self-Learning Kit in Information and Communications Technology

ACTIVITY # 3

INSTRUCTION: Complete the codes below to create a program that will generate the

greatest common divisor and least common multiple of two numbers. Identify the missing

codes and write your answer on a separate paper.

16
17
ACTIVITY # 1 Pg. 14 WHAT DO YOU KNOW Pg. 6
1. C 1. C
2. B 2. B
3. D 3. D
4. E 4. E
5. A 5. A
ACTIVITY # 2 Pg. 15 ACTIVITY # 3 Pg. 16
3 1. CheckNum (int a) 1. hcf
5 2. If 2. Scanner
1 3. Private Static 3. sc
2 4. Void 4. out
5. gcd
6 5. (a % 2 == 0 )
6. hcf
8 6. Else
7. hcf
7 7. { System.out.println (“Even”);}
8. lcm
9 8. { System.out.println (“Odd”);}
9. Private
10 9. }
10. if
4 10. {
ANSWER KEY
Creating Methods According to Java Framework
Self-Learning Kit in Information and Communications Technology

SELF-ASSESSMENT

RUBRICS OF LEARNING
SCORE SCORE SCORE DESCRIPTORS
1-5 1 - 10 1 - 15

4-5 9 -10 13-15 Excellent!

3 6-8 9-12 Good

2 4-5 5-8 Fair

1 1 -3 1-4 Start Again

18
Creating Methods According to Java Framework

ACKNOWLEDGEMENT

I would like to express my special thanks to LRMDS Bataan for giving me the golden

opportunity to create this Self-Learning Kit. This has been a wonderful experience - to be

a part of this fantastic team that creates learning material for the good of our students.

I wish that this learning material may serve as a tool to achieve greater learning for our

students not only today but in the future as well.

REFERENCES

WEBSITE

“Java Methods“, Refsnes Data, July 24, 2019, https://www.w3schools.com/java/java_


methods.asp

“Java - Methods”, Tutorials Point India Limited, July 24, 2019, https://www.tutorialspoint.
com/java/java_methods

“Method References in Java“, Baeldung SRL. , May 12, 2019, https://www.baeldung.com/


baeldung-company-info

“Java Methods”, Parewa Labs Pvt. Ltd.,”July 24,2019, https://www.programiz.com/java-


programming/methods

19
CREATING METHODS ACCORDING
TO JAVA FRAMEWORK
If you are a senior high school student and you find programming difficult to
learn and to understand, then we have good news for you. We introduce to you this
Self-Learning Kit that will help you to your journey in the world of programming. With
the use this SLK, we made learning programming easy and fun where you are able
to learn at your own pace.
The core topic of this Self-Learning Kit is Methods, one the components of
programming. Using this learning material, you are able to learn how to create and
use method in your programs. Don't miss this chance for a wonderful programming
experience.

C O N T E NT DE VE LO P E RS

WRITER/LAYOUT ARTIST: KENNETH G. DOCTOLERO - He is a graduate of Bataan Peninsula


State University Main Campus with the degree of Bachelor of Science in Computer
Science major in Software Development in 2011. Served as IT Support Helpdesk at
Petron Bataan Refinery from 2011 to 2015. He also served as Instructor I at Microcity
College of Business and Technology Inc. from 2015 to 2018. At present, he is a Teacher II
at Mariveles Senior High School - Sitio Mabuhay handling Empowerment Technologies
and Computer System Servicing Subjects.

EVALUATOR: MARY ANN S. VALDEZ- She is a graduate of Bachelor in Secondary Education


major in Mathematics from Bataan Polytechnic State College, year 2002. She earned
her units in Master of Arts in Education major in Mathematics at Bataan Peninsula State
University Institute of Graduate Studies. Serves as a Teacher at Nagbalayong National
High School from 2003 up to present. Currently taking up Master of Arts in Education,
major in Educational Management at Gordon College – Institute of Graduate Studies in
Olongapo City. At present, she is a Learning Resource Evaluator for Learning Resource
Management and Development Section (LRMDS) Bataan.

EVALUATOR: EMELITA G. PAGUIO Graduated with the degree Bachelor of Secondary


Education, major in English at Tomas del Rosario College. She has 33 units in MAEd-
Educational Management and complete academic requirements in MAEd-Language
Education. She taught for three years in private schools in Balanga City and 13 years
in Nagbalayong NHS in Morong. Presently, she serves as Assistant School Principal II of
Senior High School in E.C. Bernabe NHS, Bagac, Bataan.

SCHOOLS DIVISION OF BATAAN


LEARNING RESOURCES MANAGEMENT AND DEVELOPMENT SECTION (LRMDS)
Bataan Provincial Capitol Compound, Balanga City 2100

You might also like