3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Write and execute Java programs.
- Perform arithmetic operations in Java.
- Work with arrays and loops.
- Process arrays of data objects.
- Apply conditional logic in Java programs.
3.2 Overview
Java programming exercises help learners strengthen core programming concepts through practical implementation and problem-solving. Learners will apply:
- variables,
- loops,
- arrays,
- conditions,
- objects,
- and arithmetic operations.
These concepts are important in:
- enterprise software,
- financial systems,
- inventory management,
- and application development.
This lesson introduces learners to practical Java programming activities that reinforce software development fundamentals.
Understanding these concepts is important because they form the foundation for advanced Java programming and software engineering.
Case Study / Scenario
You are a junior Java developer at a South African fintech start-up. Your mentor gives you a set of practice exercises to confirm your understanding of:
- Java application execution,
- arithmetic operations,
- arrays,
- object arrays,
- and conditional logic.
You are required to:
- write Java applications,
- compile and execute programs,
- and solve practical data-processing problems.
PA0101 — Write a Hello World Programme Using Java
A Hello World program is commonly used as the first Java application.
Java Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello, World!”);
}
}
Expected Output:
Hello, World!
Practical Activity
Learners must:
- create Java source files,
- write Java syntax correctly,
- and display output to the console.
PA0102 — Run a Java Application
Java applications must be compiled and executed correctly.
Java Commands:
javac HelloWorld.java
java HelloWorld
Expected Output:
Hello, World!
Practical Activity
Learners must:
- compile Java programs,
- execute Java applications,
- and identify compilation results.
PA0103 — Add Numbers
Java can perform arithmetic calculations using variables.
Java Example:
public class AddNumbers {
public static void main(String[] args) {
int a = 5;
int b = 7;
int sum = a + b;
System.out.println(“Sum = “ + sum);
}
}
Expected Output:
Sum = 12
Practical Activity
Learners must:
- declare variables,
- perform calculations,
- and display arithmetic outputs.
PA0104 — Add All Elements in Array
Arrays store multiple values inside a single variable.
Java Example:
public class ArraySum {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int n : numbers) {
sum += n;
}
System.out.println(“Total = “ + sum);
}
}
Expected Output:
Total = 15
Practical Activity
Learners must:
- create arrays,
- iterate using loops,
- and calculate totals.
PA0105 — Sum Numbers From Array of Data Objects
Objects can store data and be processed in arrays.
Java Example:
class Product {
int price;
Product(int price) {
this.price = price;
}
}
public class ObjectArraySum {
public static void main(String[] args) {
Product[] products = {
new Product(100),
new Product(200),
new Product(300)
};
int sum = 0;
for (Product p : products) {
sum += p.price;
}
System.out.println(“Total Price = “ + sum);
}
}
Expected Output:
Total Price = 600
Practical Activity
Learners must:
- create object arrays,
- access object properties,
- and calculate totals.
PA0106 — Sum Numbers From Array of Data Objects Conditionally
Conditional logic controls which values are processed.
Java Example:
class Student {
String name;
int marks;
Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
}
public class ConditionalSum {
public static void main(String[] args) {
Student[] students = {
new Student(“Alice”, 85),
new Student(“Bob”, 45),
new Student(“Charlie”, 70)
};
int sum = 0;
for (Student s : students) {
if (s.marks >= 50) {
sum += s.marks;
}
}
System.out.println(“Sum of Passing Marks = “ + sum);
}
}
Expected Output:
Sum of Passing Marks = 155
Practical Activity
Learners must:
- apply conditions,
- filter values,
- and process object arrays.
PA0107 — Sum Numbers From Array of Data Objects by Code
Objects can be filtered using specific identifiers.
Java Example:
class Employee {
int id;
int salary;
Employee(int id, int salary) {
this.id = id;
this.salary = salary;
}
}
public class CodeBasedSum {
public static void main(String[] args) {
Employee[] employees = {
new Employee(101, 5000),
new Employee(102, 7000),
new Employee(101, 6000)
};
int sum = 0;
for (Employee e : employees) {
if (e.id == 101) {
sum += e.salary;
}
}
System.out.println(“Sum of Salaries for ID=101 = “ + sum);
}
}
Expected Output:
Sum of Salaries for ID=101 = 11000
Practical Activity
Learners must:
- process object arrays,
- apply conditional filtering,
- and calculate totals using codes.
AK0101 — Java Programming Functionalities
Java Programming Functionality — Purpose
Variables — Store values
Loops — Repeat instructions
Conditions — Apply decision-making
Arrays — Store multiple values
Objects — Represent real-world entities
Arithmetic Operations — Perform calculations
Practical Activity
Learners must:
- identify Java functionalities,
- explain programming concepts,
- and apply core Java principles.
IAC0101 — Expected Results are Achieved
Expected Results
- Java applications compile successfully.
- Programs execute without runtime errors.
- Arithmetic operations function correctly.
- Arrays and loops process data successfully.
- Conditions filter values correctly.
- Object arrays store and retrieve data successfully.
3.5 Key Notes / Summary
- Java programs begin with the main() method.
- Variables store data values.
- Arrays store collections of values.
- Loops simplify repetitive operations.
- Objects represent structured data.
- Conditions control program flow.
- Java applications must compile before execution.