Subject/Title: TVE 9 – Computer Programming (JAVA)
Quarter/Module #: Q2-M2
Topic: Using Methods – Make Code Cleaner by Using Methods
Lesson Objectives
By the end of this lesson, students should be able to:
1. Define what a method is and explain its importance in
programming. 2. Create and call simple methods in Java.
3. Use parameters in methods to make them flexible and reusable.
4. Return values from methods and apply them in programs.
5. Appreciate how methods make programs cleaner, more organized, and easier
to maintain.
A. LESSON PREVIEW
Introduction
Imagine giving instructions to a friend to prepare coffee. You might
say: 1. Boil water.
2. Put coffee in a cup.
3. Pour hot water.
4. Stir and serve.
If you had to repeat these instructions every single time someone wanted coffee, it
would be tiring. Instead, you can just say: “Make coffee” — a short phrase that
represents all those steps.
This is exactly what methods do in programming. Instead of writing the same steps
(code) multiple times, we create a method that contains those steps, and simply call it
whenever needed.
Just like in real life where one action may require different inputs (e.g., sugar, no sugar,
3-in 1), methods can also accept parameters to customize their behavior. And
sometimes, after completing their task, they give something back — like returning the
finished coffee to you. In Java, methods can return values to the program.
B. MAIN LESSON CONTENT
1. What is a Method?
• A method is a block of code designed to perform a specific task.
• Instead of writing the same set of instructions again and again, we put them in
a method and call it whenever needed.
• Methods make our code modular, organized, and reusable.
Analogy:
Think of a washing machine. It has methods such as:
• washClothes() → washes the clothes.
• rinse() → rinses them.
• spinDry() → dries them.
Instead of manually performing every single step, you just press a button (call the
method).
2. Structure of a Method in Java
The general syntax is:
returnType methodName(parameters) {
// method body
}
• returnType → what the method will give back (e.g., int, double, String, void
if nothing).
• methodName → name of the method (should describe what it
does). • parameters → input values (optional).
• method body → the actual instructions.
3. Creating and Calling Simple Methods
Example 1: Without Parameters
class Greeting {
void sayHello() {
[Link]("Hello, welcome to Java!");
}
}
public class Main {
public static void main(String[] args) {
Greeting g = new Greeting(); // create object
[Link](); // call method
}
}
Output:
Hello, welcome to Java!
Here, sayHello() is reusable — no need to write the print statement repeatedly.
4. Methods with Parameters
Parameters allow methods to accept input, making them more
flexible. Example 2: Method with Parameters
class MathOperation {
void addNumbers(int a, int b) {
int sum = a + b;
[Link]("Sum: " + sum);
}
}
public class Main {
public static void main(String[] args) {
MathOperation m = new MathOperation();
[Link](5, 10); // Output: Sum: 15
[Link](20, 30); // Output: Sum: 50
}
}
Instead of writing multiple [Link](a+b);, we just call addNumbers()
with different inputs.
5. Methods with Return Values
Some methods don’t just do an action; they also send back a
result. Example 3: Method with Return Value
class Calculator {
int square(int num) {
return num * num; // returns the result
}
}
public class Main {
public static void main(String[] args) {
Calculator c = new Calculator();
int result = [Link](6);
[Link]("Square: " + result);
}
}
Output:
Square: 36
Here, square() returns an integer which we stored in result.
6. Combining Parameters and Return Values
Example 4:
class MathTools {
int multiply(int x, int y) {
return x * y;
}
}
public class Main {
public static void main(String[] args) {
MathTools tool = new MathTools();
int product = [Link](7, 8);
[Link]("Product: " + product);
}
}
Output:
Product: 56
This shows how methods can accept input and produce output, making
them powerful building blocks.
7. Benefits of Using Methods
• Reusability – Write once, use many times.
• Readability – Code is easier to understand.
• Maintainability – Errors can be fixed in one place.
• Modularity – Program is divided into smaller, manageable parts.
C. ACTIVITIES
Activity 1: Think Like a Programmer
• List 3 real-life tasks (e.g., cookRice(), sendText(), payBills()).
• For each, identify possible parameters (inputs) and return values
(outputs). Activity 2: Code It Yourself
• Create a class StudentHelper with:
o a method printName(String name) → prints the student’s name.
o a method getGrade(int score) → returns "Pass" if score ≥ 75, otherwise
"Fail". Activity 3: Refactor Challenge
Given this repeated code:
[Link]("Area: " + (5 * 10));
[Link]("Area: " + (7 * 3));
[Link]("Area: " + (4 * 4));
Rewrite it using a method printArea(int length, int width).
Activity 4: Reflection Pair Work
In pairs, discuss how methods in Java are similar to instructions in daily life. Share at
least one example.
D. LESSON WRAP-UP
Key Takeaways:
• Methods group code into reusable blocks.
• Parameters let methods receive input values.
• Return values allow methods to give back results.
• Methods improve readability, reusability, and organization of code.
Reflection Questions:
1. In your own words, why are methods useful in programming?
2. Write a method multiplyThreeNumbers(int a, int b, int c) that returns the
product. 3. How are methods similar to recipes or step-by-step instructions in real
life?