Sonzaschool
Rudi

Sekondari ya Kawaida · Kidato cha Tatu · Amali · Electronics and Computer

Computer Programming

Constructing real-world models using classes and objects

takriban dakika 3 kusoma

Majadiliano
Mada za sehemu hiiCreating basic programs using classes and objectsMada 3
  1. Defining classes and creating objects
  2. Organising programs using constructors
  3. Constructing real-world models using classes and objects

Modelling Real-World Entities

Object-Oriented Programming (OOP) allows us to represent real-world things as objects in a program. A class serves as a blueprint or template that defines what an object knows (its attributes) and what it can do (its methods). This process of turning real-life entities into program code is called modelling.

Attributes and Methods

  • Attributes (also called properties) describe the characteristics of an object, such as a student's name, age, or a bank account's balance.
  • Methods (also called behaviours) define the actions an object can perform, such as depositing money, withdrawing cash, or displaying student information.

Building a Real-World Model: Bank Account

The following activity demonstrates how to model a real-world bank account using a class.

Step 1: Define the Class

A class named BankAccount is created with two properties ($owner and $balance) and three methods:

<?php
class BankAccount {
    public $owner;
    public $balance;

    // Constructor: initializes the account when created
    public function __construct($accountOwner, $initialBalance) {
        $this->owner = $accountOwner;
        $this->balance = $initialBalance;
    }

    // Method to deposit money
    public function deposit($amount) {
        $this->balance += $amount;
        echo "Deposited TZS $amount. New balance: TZS $this->balance <br>";
    }

    // Method to withdraw money
    public function withdraw($amount) {
        if ($amount > $this->balance) {
            echo "Insufficient balance! <br>";
        } else {
            $this->balance -= $amount;
            echo "Withdrew TZS $amount. Remaining balance: TZS $this->balance <br>";
        }
    }

    // Method to check the current balance
    public function checkBalance() {
        echo "Account Balance: TZS $this->balance <br>";
    }
}
?>

Step 2: Create an Object

An object is created from the class using the new keyword:

$myAccount = new BankAccount("Rehema", 10000);

Step 3: Use the Object's Methods

$myAccount->deposit(2500);    // Adds money to account
$myAccount->withdraw(3000);   // Takes money out
$myAccount->checkBalance();   // Shows current balance

The output will be:

Deposited TZS 2500. New balance: TZS 12500
Withdrew TZS 3000. Remaining balance: TZS 9500
Account Balance: TZS 9500

Another Example: Student Profile

This activity shows how to model a student with a constructor that sets initial values.

Step 1: Define the Student Class

<?php
class Student {
    public $name;
    public $age;
    public $form;

    // Constructor sets initial values
    public function __construct($studentName, $studentAge, $studentForm) {
        $this->name = $studentName;
        $this->age = $studentAge;
        $this->form = $studentForm;
    }

    // Method to display student information
    public function displayProfile() {
        echo "Student Name: $this->name <br>";
        echo "Age: $this->age <br>";
        echo "Form: $this->form <br>";
    }
}
?>

Step 2: Create and Use a Student Object

$student1 = new Student("Asha", 16, "Form Three");
$student1->displayProfile();

This outputs:

Student Name: Asha
Age: 16
Form: Form Three

Key Points to Remember

  • A class is a blueprint; an object is a real thing created from that blueprint.
  • The new keyword creates a new object from a class.
  • A constructor (__construct) automatically runs when an object is created, usually to set initial values.
  • The $this keyword refers to the current object inside the class.
  • The arrow operator -> is used to access an object's properties or call its methods.

Real-life application

In Tanzania, shops and market vendors can use simple class-based programs to manage their daily sales. For example, a small shopowner in Dar es Salaam could create a Product class with attributes like $name, $price, and $quantity, and methods to update stock after each sale. This helps track inventory and calculate profits without needing expensive software, making everyday business operations easier and more organized.

Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa

Tathmini ya mchakato

The student should be able to:

  • Identify and translate real-world entities into classes and objects.
  • Design classes with appropriate attributes and methods representing real-world functionality.
  • Implement relationships between classes, such as inheritance and associations.

Tathmini ya matokeo

Classes and objects that represent real-world entities accurately per object-oriented principles are constructed.

Tathmini ya maarifa

Methods used: The trainee should explain how to:

  • Design class diagrams for real-world models.
  • Implement attributes and methods representing real-world properties and behaviors.
  • Build relationships between classes to represent associations and inheritance.

Principles: The student should explain the principle of:

  • Object-oriented programming (OOP) concepts.
  • Designing reusable code using classes and objects.
  • Real-world problem-solving through object modeling.

Theories: The trainee should explain: The trainee should explain:

  • How real-world entities can be abstracted into classes and objects.

Circumstantial knowledge:

Detailed knowledge about:

  • Common design patterns for modeling real-world scenarios in OOP.
  • Applications of OOP in real-world industries.

Swali

What is a class in Object-Oriented Programming?

Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.

Ingia ili kufanya mazoezi

Mwalimu

Umekwama? Niulize chochote kuhusu mada hii.

Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.

Ingia ili kuuliza

Majadiliano

Hakuna maswali bado

Ingia ili kuuliza
Inapakia majadiliano…