Online Learning Module: Java
Programming 2
Module Overview
This module introduces key Java programming concepts, including decision-making
structures, loops, and arrays. By the end of this module, students will be able to implement
conditional statements, use looping constructs, and manipulate arrays effectively in Java.
Instructions:
1. Study the lesson first – Carefully read and understand the concepts covered in this
lesson. You may use other sources such as books, online articles, or videos to
enhance your understanding.
2. Answer the coding activity – Write your Java code based on the given problem.
3. Answer the essay activity – Explain the concepts learned in a well-structured essay.
4. Submission Guidelines:
o Your answers must be handwritten.
o Use a long bond paper for your answers.
o Write the lesson number and questions on your answer sheet before
answering.
o Ensure your handwriting is clean and readable.
o Not following instructions will result in non-acceptance of your work.
5. Deadline: February 18, 2025 – Late submissions will not be accepted.
Lesson 1: Decision-Making in Java
Discussion:
Decision-making structures allow a program to execute different actions based on
conditions. Java provides several decision-making statements:
1. if Statement - Executes a block of code if a condition is true.
2. if-else Statement - Executes one block if the condition is true; otherwise, executes
another block.
3. if-else-if Ladder - Tests multiple conditions sequentially.
4. switch Statement - Selects one of many code blocks based on a variable's value.
Example:
int num = 10;
if (num > 0) {
[Link]("Positive number");
} else {
[Link]("Non-positive number");
}
Coding Activity:
Car Loan Eligibility (if-else)
Problem Statement:
A bank approves car loans based on the following conditions:
Applicant must be at least 21 years old.
Applicant's monthly salary must be ₱20,000 or higher.
Write a Java program that asks for the applicant’s age and salary, then determines if they
qualify for a car loan.
Essay Activity:
Explain how decision-making structures are useful in everyday applications such as
banking systems, e-commerce, or traffic control.
Lesson 2: Loops in Java
Discussion:
Loops allow the repetition of a block of code until a specific condition is met. Java supports:
1. for Loop - Iterates a block of code a fixed number of times.
2. while Loop - Repeats as long as a condition is true.
3. do-while Loop - Executes at least once before checking the condition.
Example:
for (int i = 1; i <= 5; i++) {
[Link]("Iteration: " + i);
}
Coding Activity:
Employee Overtime Pay Calculation (Using Loop + if-else)
Problem Breakdown:
Employees work 40 hours per week as regular time.
If they work more than 40 hours, they earn 1.5 times the hourly rate for extra
hours.
The hourly rate is fixed at ₱150.
The program should continuously prompt the user for hours worked.
If the user enters -1 for hours worked, the program should stop.
Step-by-Step Logic:
1. Use a loop (while or do-while) to continuously ask for input.
2. Check the input:
o If hoursWorked == -1, exit the loop.
o If hoursWorked ≤ 40, calculate regular pay.
o If hoursWorked > 40, calculate regular pay + overtime pay.
3. Display the total salary for each employee.
Essay Activity:
Describe a real-life scenario where loops are useful, such as calculating bills,
managing user logins, or automating repetitive tasks.
Discuss the advantages and disadvantages of infinite loops. Provide an example of a
situation where an infinite loop might be useful.
Lesson 3: Introduction to Arrays
Discussion:
An array is a collection of variables of the same type stored in a contiguous memory
location. Arrays help manage multiple values efficiently.
Example:
int[] numbers = {10, 20, 30, 40, 50};
[Link](numbers[2]); // Output: 30
Coding Activity:
Declare an array to store 5 student grades.
Use a for-each loop to print all the grades.
Essay Activity:
Why would you use an array instead of separate variables? Share your thoughts .
List at least two advantages of using arrays and Explain .
Lesson 4: One-Dimensional Arrays
Discussion:
A one-dimensional array stores elements in a single row. It can be declared and initialized
as follows:
int[] arr = new int[5]; // Declaration
arr[0] = 10; // Initialization
Coding Activity:
Student Grades Management (1D Array)
Problem Statement:
A teacher needs a program to store and analyze student grades for a class of 5 students.
The program should store the grades in an array.
Calculate and display the average grade.
Identify the highest and lowest grades.
Step-by-Step Approach:
1. Initialize min and max: Set min and max to the first element of the array.
2. Loop through the array:
o If the current element is smaller than min, update min.
o If the current element is greater than max, update max.
3. Display the results.
Essay Activity:
Discuss the advantages and limitations of using one-dimensional arrays in
programming. Give a real-world example.