Python Lab Assignment Solutions
Python Lab Assignment Solutions
The 'total_books' attribute in the 'Book' class acts as a class variable that keeps track of the number of book instances created. With each instantiation of a 'Book' object, 'total_books' is incremented, reflecting shared data across all instances. This implementation exemplifies a class-level attribute's role in managing shared state and providing a mechanism to track collective information about all objects of the class, rather than individual instances .
Inheritance is used in the 'Employee' and 'Manager' classes to establish a hierarchical relationship where 'Manager' is a subclass of 'Employee'. The 'Manager' class inherits properties like 'name', 'id', and 'salary' from 'Employee', and additionally introduces the 'department' attribute. This implementation facilitates code reuse, allowing managers to share common employee traits while expanding functionality with department-specific attributes .
The 'BankAccount' class uses encapsulation by hiding its attributes '_account_holder' and '_balance' with a leading underscore. This convention suggests that these attributes shouldn't be accessed directly from outside the class. Methods like 'deposit', 'withdraw', and 'get_balance' provide controlled access to these attributes, allowing operations to be performed while maintaining internal data security and integrity. This encapsulation helps prevent unintended modifications that could compromise account balances .
The class 'Shape' supports polymorphic behavior via its abstract 'area' method, which is meant to be implemented by subclasses like 'Circle' and 'Rectangle'. Each subclass provides a concrete implementation of 'area', computing the area specific to its geometric formula — 'Circle' uses πr², and 'Rectangle' uses length × width. This allows instances of 'Circle' and 'Rectangle' to be used interchangeably wherever a 'Shape' type is expected, with each providing its specific area computation .
The 'Student' class implements conditional logic through the 'check_pass_fail' method. This method evaluates whether a student's marks are greater than or equal to 35 by using an 'if' statement. If true, the method returns 'Passed'; otherwise, it returns 'Failed'. This conditional check allows the 'Student' class to determine and convey the pass or fail status of a student based on their marks, illustrating the application of simple conditional statements to make logical decisions .
The inheritance structure among 'LivingBeing', 'Animal', and 'Dog' is a simple multi-level hierarchy. 'Animal' inherits from 'LivingBeing', and 'Dog' inherits from 'Animal'. This setup allows a 'Dog' object to access methods defined in both 'Animal' and 'LivingBeing', such as 'breathe'. The 'Dog' class also introduces a specific 'bark' method unique to its subclass. This hierarchy facilitates method sharing across levels and encourages code reuse, enabling specific behaviors like 'bark' while maintaining basic functionalities from parent classes .
The '__init__' method in Python classes serves as a constructor to initialize an object's state upon creation. In the 'Person' class, '__init__' takes parameters 'name' and 'age' to assign the initial values to the corresponding object attributes 'self.name' and 'self.age'. This method is crucial for setting up initial conditions and ensuring objects have the necessary attributes to perform functions immediately after being instantiated .
The classes 'Car' and 'ElectricCar' demonstrate method overriding through their 'start' methods. In the base class 'Car', the 'start' method outputs 'Car starting with engine...'. The 'ElectricCar' class, which inherits from 'Car', overrides this method to output 'Electric car starting with battery...'. This shows how a subclass can provide a specific implementation for a method that is already defined in its superclass .
The 'Calculator' class implements basic error handling in its 'divide' method to prevent division by zero, which is a common source of runtime errors. By checking if the divisor 'b' is not zero before performing the division, the method avoids generating an exception and returns a meaningful message 'Division by zero error' instead. This preventative measure ensures that the program handles potential errors gracefully, maintaining stability and providing users with prompt feedback on invalid operations .
Polymorphism in the 'Animal', 'Dog', and 'Cat' classes is demonstrated through the 'sound' method. The base class 'Animal' defines a generic 'sound' method. Both 'Dog' and 'Cat' subclasses override this method to provide specific implementations ('Bark' for dogs and 'Meow' for cats). This allows objects of these subclasses to be treated as instances of their parent class, enabling them to exhibit behavior specific to their subclass automatically .