Computer Science  ·  Level 6
Fundamentals Of Programming

Chapter 5: Use methods

📚 3 Topics
⚑ What you will be able to do

By the end of this chapter, you will be able to:

  • correctly apply procedures, functions, and methods in your programming projects
  • implement methods accurately while following proper programming standards
  • develop programs that use methods to organize and simplify your code effectively

Mastering these skills will help you write cleaner, more efficient programs that are easier to understand and maintain in your future work as a programmer.

Use of methods in programming is fundamental to creating organized, reusable, and maintainable code. In Kenya's growing software development sector, from fintech startups in Nairobi to e-governance projects in county offices, mastering methods enhances productivity and collaboration among computer science professionals. This chapter focuses on Java methods, a cornerstone of Java programming, providing a foundation for writing modular programs that are easier to debug and extend.

5.1 Java Methods

Java methods encapsulate blocks of code designed to perform specific tasks within a program. Understanding methods is crucial for Kenyan developers working on complex applications such as hospital management systems or SACCO financial software, where modularity and clarity are paramount.

5.1.1 Definition

The concept of a method in Java is central to procedural and object-oriented programming paradigms. It allows programmers to define a set of instructions that can be executed repeatedly without rewriting code.

What is a Java Method

A Java method is a named block of code that performs a particular operation and can be invoked when needed. It may accept input parameters, perform computations or actions, and return a result. Methods help reduce code duplication by encapsulating logic that can be reused across different parts of a program.

Role of Methods in Software Development

Methods enable programmers to break down complex problems into manageable pieces. This modular approach is essential in environments such as Kenya’s banking sector, where software systems like those used by Equity Bank require clear, maintainable, and testable code modules.

Difference Between Methods and Functions

In Java, methods are associated with classes and objects, unlike functions in some other languages which can exist independently. This association supports encapsulation and inheritance, key principles in building robust systems such as county government e-services.

Parameters and Return Types

Methods can accept zero or more parameters, which act as inputs, and can return a value of a specified type. For instance, a method calculating loan interest in a SACCO application would accept parameters like principal amount and interest rate and return the computed interest.

Importance of Method Naming Conventions

Clear and descriptive method names improve code readability and maintainability. Kenyan software developers working collaboratively benefit from adhering to conventions such as camelCase, which is widely accepted in Java development practices.

5.1.2 Structure

The structure of a Java method defines its components and syntax, which must be understood to write effective code. Kenyan programmers developing applications for educational institutions or healthcare providers rely on well-structured methods to ensure clarity and functionality.

Method Signature

The method signature includes the method name and parameter list. It uniquely identifies the method within a class and is critical for method overloading, allowing multiple methods with the same name but different parameters in systems like university management software.

Access Modifiers

Access modifiers such as public, private, and protected control the visibility of methods. For example, a method marked private in a hospital management system restricts access to sensitive operations like patient data processing, enhancing security.

Return Type Declaration

Every Java method declares the type of value it returns, or void if it returns nothing. This specification informs the compiler and developers about the kind of result to expect, such as an integer representing the number of items processed in inventory management software for retail shops.

Method Body

The method body contains the executable code enclosed in braces. It defines the specific actions the method performs, such as calculating totals or updating records in a county revenue collection system.

Example of a Java Method Structure

public int calculateTotal(int quantity, int price) {
    int total = quantity * price;
    return total;
}

This example demonstrates a method named calculateTotal that takes two integer parameters, multiplies them, and returns the result as an integer. Such methods are common in financial applications used by Kenyan cooperatives.

Practice Questions

  1. Define a Java method and explain its significance in software development. (6 marks)
  2. Describe the components of a Java method’s structure with examples. (8 marks)
  3. Explain how access modifiers affect method visibility in Java. Provide examples relevant to Kenyan public sector software. (6 marks)
  4. Write a Java method that calculates the area of a rectangle given its length and width. Include parameters and return type in your method signature. (5 marks)
The rest of this chapter
🔒

Create a free account to open more of this chapter.

Free: practical guides, quick cards, workplace scenarios and more.

Create a free account
🔒5.2 Demonstration of methods

This topic demonstrates how methods are created and used, one feature at a time. Every example is a small complete program that you can type into Eclipse or any other Java editor, compile and run. The definition of a method and the parts of its header are alre…

🔒5.3 Creation programs to implement methods

A useful program is rarely one long main method. It is a set of small methods, each doing one task, and a main method that calls them in order. This topic puts the features from Demonstration of methods to work in three complete programs: a hire billing progra…

Chapter Summary

This chapter focused on the concept of Java methods, starting with a clear definition that explained methods as reusable blocks of code designed to perform specific tasks within a program. The discussion highlighted how methods help organize code, making it more modular and easier to maintain. The structure of Java methods was then examined, detailing the essential components such as the method signature, return type, method name, parameters, and the body of the method enclosed in braces. Understanding this structure is crucial for writing effective methods that can be called upon to execute particular functions. The chapter emphasized the importance of method declarations and how they define the behavior and interaction within Java programs. Overall, the chapter provided foundational knowledge necessary for creating, using, and managing methods in Java programming.

Self-Assessment

🔒 PDFDownload this self-assessment, with answers

A. Written Assessment

  1. What is a method in Java? (2 marks)
  2. Identify the correct syntax for defining a method that returns an integer and takes two integer parameters. (2 marks)
🔒20 more in this section.

Chapter Examination Questions

🔒 PDFDownload these examination questions, with worked answers

SECTION A (40 Marks) - Answer ALL Questions

  1. Explain the concept of a method in Java and its role in modular programming within software development projects at institutions such as Kenya Revenue Authority. (4 marks)
  2. Identify and describe the main components that make up the structure of a Java method. (4 marks)
🔒18 more in this section.

Chapter Practical Activities

Practical 1: Define and Compile a Simple Java Method

Computer Science · Level 6
Fundamentals Of Programming
PRACTICAL ASSESSMENT
TIME: 4 HOURS
⬇ PDFCandidate Instructions (Candidate Tool)

Type: Individual

INSTRUCTIONS TO CANDIDATE:
1.  You are required to perform the following task:
i.  Write and compile a Java program defining a simple method named displayMessage that prints a welcome message, with the class named WelcomeClass.
2.  You have been provided with the following resources for the practical task:
Tools & EquipmentMaterials
Functional computer installed with Java IDE (NetBeans/Eclipse/IntelliJ)Plain paper
⬇ PDFResources Required (Cutting List)
S/NItemQuantity
1Functional computer installed with Java IDE (NetBeans/Eclipse/IntelliJ)1 Pc per Candidate
2Plain paper1 Pc per Candidate
⬇ PDFAssessor Guide
Items to be EvaluatedMarks AvailableMarks ObtainedComments
TASK 1: Define Java Method
Launched the Java IDE successfully
(Award 2 marks or zero)
2
Declared the class named WelcomeClass correctly
(Award 3 marks or zero)
3
Defined the method displayMessage with correct signature and no parameters
(Award 1 mark for method name, 2 marks for correct return type void, 2 marks for no parameters)
5
Implemented the method body to print the welcome message: "Welcome to Java Programming!"
(Award 5 marks or zero)
5
Declared the main method correctly
(Award 3 marks or zero)
3
Created an object of WelcomeClass inside main method
(Award 2 marks or zero)
2
Called the displayMessage method using the created object
(Award 3 marks or zero)
3
Compiled the Java program without errors
(Award 5 marks or zero)
5
Sub-Total28
PRODUCT CHECKLIST
Program runs and outputs the exact message: "Welcome to Java Programming!"
(Award 7 marks or zero)
7
Sub-Total7
GRAND TOTAL35
ASSESSMENT OUTCOME:   ☐ Competent    ☐ Not Yet Competent (competent if at least 50%)

Practical 2: Create a Java method with parameters and return value

Computer Science · Level 6
Fundamentals Of Programming
PRACTICAL ASSESSMENT
TIME: 4 HOURS
⬇ PDFCandidate Instructions (Candidate Tool)

Type: Individual

INSTRUCTIONS TO CANDIDATE:
1.  You are required to perform the following task:
i.  Create a Java method named calculateArea that accepts two double parameters (length and width) and returns their product as a double value.
2.  You have been provided with the following resources for the practical task:
Tools & EquipmentMaterials
Functional computer installed with Java IDE (NetBeans/Eclipse/IntelliJ)Plain paper
⬇ PDFResources Required (Cutting List)
S/NItemQuantity
1Functional computer installed with Java IDE (NetBeans/Eclipse/IntelliJ)1 Pc per Candidate
2Plain paper1 Pc per Candidate
⬇ PDFAssessor Guide
Items to be EvaluatedMarks AvailableMarks ObtainedComments
TASK 1: Method Implementation
Launched the Java IDE
(Award 2 marks or 0)
2
Declared the calculateArea method with two double parameters
(Award 1 mark for method declaration, 2 marks for correct parameters, 2 marks for correct return type)
5
Implemented method logic to return the product of length and width
(Award 5 marks for correct calculation and return statement)
5
Called the calculateArea method from main with example arguments
(Award 3 marks for correct method invocation with arguments)
3
Displayed the returned area value on the console
(Award 3 marks for correct output display)
3
Used appropriate variable types and names
(Award 2 marks or 0)
2
Sub-Total20
PRODUCT CHECKLIST
Method calculateArea correctly accepts two double parameters and returns their product as double
(Award 10 marks for correct method signature, logic, and return type)
10
Sub-Total10
GRAND TOTAL30
ASSESSMENT OUTCOME:   ☐ Competent    ☐ Not Yet Competent (competent if at least 50%)
🔒

Free: practical guides, quick cards, workplace scenarios and more.

Create a free account
🔒Java Program Demonstrating Method OverloadingPractical 3
🔒Write and explain the structure of a Java methodPractical 4
🔒Implement and Run a Void Java Method to Display a Welcome MessagePractical 5
🔒Develop a Java method using return statement to calculate the square of a numberPractical 6
🔒Java Program Calling User-Defined Methods from Main MethodPractical 7
🔒Organize and Access Methods in a Java ClassPractical 8
🔒Java Methods Parameter Passing DemonstrationPractical 9
🔒Debug Java method syntax errors in a source code filePractical 10
Flashcards 20 cards Study deck ▾
Question
1

↻ Tap card to reveal answer
🔒

18 more in this section.

Create a free account
Test Yourself 11 questions Start quiz ▾
0%
0 / 2
🔒

9 more in this section.

Create a free account
Am I competent?

At the start of this chapter we promised you would be able to:

  • correctly apply procedures, functions, and methods in your programming projects
  • implement methods accurately while following proper programming standards
  • develop programs that use methods to organize and simplify your code effectively

Tick each one you can genuinely do.

So, are you there yet?

You're competent when you can confidently do 50% or more of what this chapter promised.

Sign in to record how you're doing.