Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Object-Oriented Programming in Python: Class Implementation and Inheritance Patterns

Tech 2

Task 1: Student Profile with Grade Analysis

Implement a class representing student records with name, age, and subject scores. Include methods to retrieve name, age, and highest score.

class Scholar:
    def __init__(self, full_name, years, marks):
        self.full_name = full_name
        self.years = years
        self.marks = marks
    
    def retrieve_name(self):
        return self.full_name
    
    def retrieve_age(self):
        return self.years
    
    def top_score(self):
        return max(self.marks)

prototype = Scholar('zhangming', 20, [69, 88, 100])
print(prototype.retrieve_name(), prototype.retrieve_age(), prototype.top_score())

Task 2: Geometric Circle Operations

Create a class modeling circular shapes with position, radius, and color attributes. Implement methods for perimeter and area calculations.

import math

class CircularShape:
    def __init__(self, coordinates, radius_val, hue):
        self.coordinates = coordinates
        self.radius_val = radius_val
        self.hue = hue
    
    def calculate_perimeter(self):
        return round(2 * math.pi * self.radius_val, 2)
    
    def compute_area(self):
        return round(math.pi * self.radius_val ** 2, 2)

circular = CircularShape((3, -2), 4, "red")
print(f"Center: {circular.coordinates}, Radius: {circular.radius_val}, Color: {circular.hue}")
print(f"Perimeter: {circular.calculate_perimeter()}, Area: {circular.compute_area()}")

Task 3: Academic Performance Tracker

Design a student class with personal attributes and academic records. Calculate aggregate scores and generate performance reports.

class Learner:
    def __init__(self, identity, years, gender_id, academic_scores):
        self.identity = identity
        self.years = years
        self.gender_id = gender_id
        self.academic_scores = academic_scores
    
    def total_points(self):
        return sum(self.academic_scores)
    
    def mean_score(self):
        return sum(self.academic_scores) / len(self.academic_scores)
    
    def display_record(self):
        print(f"ID: {self.identity}, Age: {self.years}, Gender: {self.gender_id}")
        print(f"Scores: English {self.academic_scores[0]}, Math {self.academic_scores[1]}, Chinese {self.academic_scores[2]}")
        print(f"Total: {self.total_points()}, Average: {self.mean_score():.2f}")

candidate = Learner('lisi', 20, 'male', [77, 88, 99])
candidate.display_record()

Task 4: Hierarchical Person-Student Model

Develop a base Person class and derived Student class with etxended attributes. Demonstrate method overriding and super() invocation.

class Individual:
    def __init__(self, name, gender_id, years):
        self.name = name
        self.gender_id = gender_id
        self.years = years
    
    def profile_summary(self):
        print(f"Name: {self.name}, Age: {self.years}, Gender: {self.gender_id}")

class Undergraduate(Individual):
    def __init__(self, name, gender_id, years, section, faculty):
        super().__init__(name, gender_id, years)
        self.section = section
        self.faculty = faculty
    
    def profile_summary(self):
        print(f"Name: {self.name}, Age: {self.years}, Gender: {self.gender_id}")
        print(f"Section: {self.section}, Faculty: {self.faculty}")

person_obj = Individual('Li Si', 'male', 20)
person_obj.profile_summary()
student_obj = Undergraduate('Zhang San', 'male', 20, '22-10', 'Software College')
student_obj.profile_summary()

Task 5: Vehicle Dynamics Simulator

Model transportation vehicles with kinematic properties. Implement acceleration/deceleration mechanics and state reporting.

class Automobile:
    def __init__(self, velocity, dimension):
        self.velocity = velocity
        self.dimension = dimension
    
    def status_report(self):
        print(f"Velocity: {self.velocity} km/h, Size: {self.dimension} m³")
    
    def adjust_velocity(self, new_velocity):
        self.velocity = new_velocity
    
    def initiate_movement(self):
        self.adjust_velocity(10)
        print(f"Moving at {self.velocity} km/h")
    
    def halt_movement(self):
        self.adjust_velocity(0)
        print("Stopped")
    
    def increase_speed(self, delta):
        new_vel = min(self.velocity + delta, 120)
        self.adjust_velocity(new_vel)
        print(f"Accelerated to {new_vel} km/h")
    
    def decrease_speed(self, delta):
        new_vel = max(self.velocity - delta, 0)
        if new_vel == 0:
            self.halt_movement()
        else:
            self.adjust_velocity(new_vel)
            print(f"Decelerated to {new_vel} km/h")

vehicle = Automobile(0, 8)
vehicle.status_report()
vehicle.initiate_movement()
vehicle.adjust_velocity(50)
vehicle.increase_speed(20)
vehicle.increase_speed(60)
vehicle.decrease_speed(20)
vehicle.halt_movement()

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.