23. OOP Pillars Exercises - MantsSk/CA_PTUA14 GitHub Wiki

1. Create a class "Person"

  • Private attributes for name and age. Implement the following methods:
  • set_name(name): A public method to set the person's name.
  • get_name(): A public method to get the person's name.
  • set_age(age): A public method to set the person's age. This method should include logic to check if the age is valid (e.g., between 0 and 120). If the age is invalid, it should not update the attribute and print an error message.
  • get_age(): A public method to get the person's age.
  • can_vote(): A public method that returns True if the person is 18 years or older, indicating they are eligible to vote, and False otherwise.

This exercise demonstrates encapsulation by restricting direct access to the name and age attributes and providing controlled access through public methods, including validation logic in the set_age method. This requires to know about encapsulation which you learned here

2. Design a basic class "Vehicle"

  • With attributes like brand and model.
  • Then create two derived classes, Car and Bike, which inherit from Vehicle.
  • Add specific attributes to these derived classes, like number_of_doors for Car and has_sidecar for Bike.

This shows how inheritance promotes code reusability and relationship between classes. This requires to know about inheritance which you learned here

3. Create a class "Shape"

  • with a method area.
  • Then create two derived classes, Circle and Rectangle, which override the area method:
  • The area method in Circle should calculate the area of a circle (π * radius^2).
  • The area method in Rectangle should calculate the area of a rectangle (width * height).

This task demonstrates polymorphism through method overriding, where different classes provide a specific implementation of a method defined in a base class. This requires to know about inheritance which you learned here This requires to know about encapsulation which you learned here

4. Create a base class "Animal"

  • With a method make_sound. Then create three derived classes, Dog, Cat, and Bird. Each derived class should override the make_sound method:
  • The make_sound method in Dog should return "Bark" and the dog's name.
  • The make_sound method in Cat should return "Meow" and the cat's name.
  • The make_sound method in Bird should not only return "Tweet" but also include a unique behavior method fly, which returns a statement about how the bird flies.
  • Each animal's name should be passed when the object is created.

This exercise aims to demonstrate polymorphism through method overriding and also show how derived classes can have additional methods. This requires to know about inheritance which you learned here This requires to know about encapsulation which you learned here

5. Create a small library management system to demonstrate the concepts of encapsulation, inheritance, and polymorphism. The system should include the following classes and functionalities:

Base Class - Item:

  • Attributes: title (string), author (string), both should be private.
  • Methods: Constructor to initialize the attributes, getters and setters for both attributes.

Derived Classes:

  • Book: Inherits from Item. Includes an additional private attribute page_count (integer). Implement getters and setters.
  • DVD: Inherits from Item. Includes an additional private attribute duration (in minutes, integer). Implement getters and setters.
  • Journal: Inherits from Item. Includes an additional private attribute issue_number (string). Implement getters and setters.

Polymorphism:

  • Add a method get_details in the Item class that returns a string with title and author.
  • Override this method in each derived class to include their specific attributes (page count, duration, issue number).
  • This task combines encapsulation (through private attributes and public methods), inheritance (base class and derived classes), and polymorphism (overriding methods).