Programowanie obiektowe zadania

Zadanie 1: Klasa Book z metodą getSummary()

Stwórz klasę Book, która ma pola title, author, year. Dodaj metodę getSummary(), która zwraca tekst:

Tytuł (Autor, Rok)”


Kroki rozwiązania:

  1. Utwórz klasę Book
  2. Dodaj właściwości: title, author, year
  3. Zbuduj konstruktor
  4. Zaimplementuj metodę getSummary()
  5. Przetestuj tworząc obiekt i wyświetlając podsumowanie

Przykład Rozwiązania:

 class Book {
    public $title;
    public $author;
    public $year;

    public function __construct($title, $author, $year) {
        $this->title = $title;
        $this->author = $author;
        $this->year = $year;
    }

    public function getSummary() {
        return "{$this->title} ({$this->author}, {$this->year})";
    }
 }

 // Test
 $book = new Book("1984", "George Orwell", 1949);
 echo $book->getSummary(); // 1984 (George Orwell, 1949)


Zadanie 2: Klasa Rectangle z metodami getArea() i getPerimeter()

Treść:

Zbuduj klasę Rectangle, która przechowuje długość i szerokość prostokąta oraz liczy pole i obwód.


Kroki:

  1. Utwórz klasę Rectangle
  2. Dodaj pola width, height
  3. Napisz metody getArea() i getPerimeter()
  4. Przetestuj obiekt

Rozwiązanie:

 class Rectangle {
    public $width;
    public $height;

    public function __construct($width, $height) {
        $this->width = $width;
        $this->height = $height;
    }

    public function getArea() {
        return $this->width * $this->height;
    }

    public function getPerimeter() {
        return 2 * ($this->width + $this->height);
    }
 }

 // Test
 $r = new Rectangle(5, 10);
 echo $r->getArea(); // 50
 echo $r->getPerimeter(); // 30


Zadanie 3: Dziedziczenie – Vehicle i Car

Treść:

Utwórz klasę bazową Vehicle z metodą getInfo(), a następnie klasę Car, która rozszerza Vehicle i dodaje pole brand.


Kroki:

  1. Utwórz klasę Vehicle z polem type i metodą getInfo()
  2. Stwórz klasę Car dziedziczącą po Vehicle
  3. Dodaj pole brand i przesłoń getInfo()
  4. Utwórz obiekt i przetestuj

Rozwiązanie:

 class Vehicle {
public $type;

public function __construct($type) {
$this->type = $type;
}

public function getInfo() {
return "Typ pojazdu: {$this->type}";
}
}

class Car extends Vehicle {
public $brand;

public function __construct($type, $brand) {
parent::__construct($type);
$this->brand = $brand;
}

public function getInfo() {
return "Samochód: {$this->brand} ({$this->type})";
}
}

// Test
$car = new Car("osobowy", "Toyota");
echo $car->getInfo(); // Samochód: Toyota (osobowy)



Zadanie 4: Licznik z inkrementacją

Treść:

Zbuduj klasę Counter, która ma metodę increment() i getValue(). Inicjalna wartość to 0.


Kroki:

  1. Utwórz klasę Counter
  2. Dodaj prywatne pole $value
  3. Stwórz metodę increment() zwiększającą wartość
  4. Dodaj getValue() zwracającą aktualną wartość

Rozwiązanie:

 class Counter {
    private $value = 0;

    public function increment() {
        $this->value++;
    }

    public function getValue() {
        return $this->value;
    }
 }

 // Test
 $c = new Counter();
 $c->increment();
 $c->increment();
 echo $c->getValue(); // 2

Zadanie 5: Magazyn produktów – tablica obiektów

Treść:

Utwórz klasę Product z nazwą i ceną. Następnie klasę Warehouse, która przechowuje tablicę produktów i metodę getTotalValue() – zwracającą sumę cen.


Kroki:

  1. Stwórz klasę Product z polami name, price
  2. Zbuduj klasę Warehouse z polem products (tablica)
  3. Dodaj metodę addProduct() do magazynu
  4. Dodaj metodę getTotalValue()
  5. Utwórz kilka produktów, dodaj do magazynu i oblicz sumę

Rozwiązanie:

 class Product {
    public $name;
    public $price;

    public function __construct($name, $price) {
        $this->name = $name;
        $this->price = $price;
    }
 }

 class Warehouse {
    private $products = [];

    public function addProduct(Product $product) {
        $this->products[] = $product;
    }

    public function getTotalValue() {
        $total = 0;
        foreach ($this->products as $product) {
            $total += $product->price;
        }
        return $total;
    }
 }

 // Test
 $p1 = new Product("Mysz", 50);
 $p2 = new Product("Klawiatura", 120);

 $w = new Warehouse();
 $w->addProduct($p1);
 $w->addProduct($p2);

 echo $w->getTotalValue(); // 170