06 Oct Object-Oriented Programming

 06 Oct Object-Oriented Programming



LAB 1:


class Book:
    def __init__(self, title, author):
        
        self.title = title
        self.author = author

    def display_info(self):
        
        print(f"Title: {self.title}, Author: {self.author}")
        
my_book = Book("1984", "George Orwell")

LAB 2: Create a class named Employee that contains a private attribute called _salary (a float). Implement a method get salary that prints and returns the current salary, and a method set salary that updates the salary, but prints the updated salary. You should create an object of the Employee class and first call the get method, and then the set method. Expected Output: get salary should print the current salary. set salary should only update and print the new salary if it's greater than 0. Constraint The salary should always be a positive number. The setter should prevent negative values and zero from being set as the salary. The getter should return the salary as a floating-point number.

class Employee:
    def __init__(self, salary):
        
        if salary > 0:
            self.__salary = float(salary)
        else:
            self.__salary = 0.0  

    def get_salary(self):
        
        print(f"Current salary: {self.__salary}")
        return self.__salary

    def set_salary(self, new_salary):
        if new_salary > 0:
            self.__salary = float(new_salary)
            print(f"Updated salary: {self.__salary}")
        else:
            print("Error: Salary must be greater than 0")


employee = Employee(5000.0)


employee.get_salary()

employee.set_salary(6000.0)


employee.set_salary(-1000.0)
LAB 3: Create a base class named Vehicle with a method move that prints "Vehicle is moving". Then, create a subclass Car that overrides the move method to print “Car is driving". Instantiate an object of the Vehicle class and call the move method. Also, instantiate an object of the Car class and call the move method. Expected Output: The move method of Vehicle should print “Vehicle is moving. The move method of Car should print “Car is driving". Constraint The move method in both the base class and the subclass should not return any values, only print the output. Ensure that the subclass method properly overrides the base class method.
class Vehicle:
    def move(self):
        print("Vehicle is moving")


class Car(Vehicle):
    def move(self):
        print("Car is driving")


vehicle = Vehicle()
vehicle.move()


car = Car()
car.move()
LAB 4: Create two classes, Dog and Cat, each having a method named sound. In the Dog class, sound should print "Bark", and in the Cat class, sound should print "Meow". Write a function make_sound that takes an animal object (either a Dog or Cat) and calls its sound method. Test your function with both a Dog and a Cat object. Expected Output: The sound method of the Dog class should print "Bark". The sound method of the Cat class should print "Meow". The make_sound function should correctly call the sound method of either class. Constraint The sound method in both classes should not return any values, only print the output. The make_sound function should work for any object that has a sound method (i.e., it should be polymorphic).
class Dog:
    def sound(self):
        print("Bark")

class Cat:
    def sound(self):
        print("Meow")


def make_sound(animal):
    animal.sound()


dog = Dog()
cat = Cat()


make_sound(dog)  
make_sound(cat)
LAB 5: Write a Python class called Laptop that has two attributes: brand (a string) and ram (an integer representing RAM in GB). Implement a method upgrade_ram that takes an additional RAM value (integer) and adds it to the existing RAM. After upgrading, the method prints the updated RAM. Expected Output: The upgrade_ram method should print the updated RAM value in the format: "RAM upgraded to GB". Constraint The ram attribute must be a positive integer. The upgrade_ram method should accept only positive integers and should not allow negative or zero values.
class Laptop:
    def __init__(self, brand, ram):
        
        self.brand = brand
        if ram > 0:
            self.ram = ram
        else:
            self.ram = 0
            print("Error: RAM must be a positive integer.")

    def upgrade_ram(self, additional_ram):
    
        if additional_ram > 0:
            self.ram += additional_ram
            print(f"RAM upgraded to {self.ram}GB")
        else:
            print("Error: Additional RAM must be greater than 0.")

laptop = Laptop("Dell", 8)


laptop.upgrade_ram(4)  


laptop.upgrade_ram(-2)