You are on page 1of 3

Saba University

College of Engineering ‫جامعة سبأ‬


Department of Electrical ‫كلية الهندسة‬
Engineering ‫قسم الهندسة الكهربائية‬

Python
Task 1

‫الطالب‬
‫أسامة حسن البهواني‬
Saba University
College of Engineering ‫جامعة سبأ‬
Department of Electrical ‫كلية الهندسة‬
Engineering ‫قسم الهندسة الكهربائية‬

code
class Car:
def __init__(self, brand, model, color):
self.brand = brand
self.model = model
self.color = color
self.speed = 0

def accelerate(self, increment):


self.speed += increment

def brake(self, decrement):


if self.speed >= decrement:
self.speed -= decrement
else:
self.speed = 0

def change_color(self, new_color):


self.color = new_color

def print_details(self):
print("Brand:", self.brand)
print("Model:", self.model)
print("Color:", self.color)
print("Speed:", self.speed, "km/h")

def change_brand(self, new_brand):


self.brand = new_brand

def change_model(self, new_model):


self.model = new_model

def get_speed(self):
return self.speed

def stop(self):
self.speed = 0

def set_speed(self, new_speed):


self.speed = new_speed

def honk(self):
print("Honk honk!")

# Usage of the class


car = Car("Toyota", "Camry", "Blue")
car.accelerate(50)
car.brake(20)
car.print_details()
car.change_color("Red")
car.print_details()
‫‪Saba University‬‬
‫‪College of Engineering‬‬ ‫جامعة سبأ‬
‫‪Department of Electrical‬‬ ‫كلية الهندسة‬
‫‪Engineering‬‬ ‫قسم الهندسة الكهربائية‬

‫وظيفة الكود‬
‫‪(Class): Car‬‬
‫‪ -‬الخصائص (‪:)Properties‬‬
‫‪ :brand -‬يمثل عالمة السيارة (مثال‪.)Toyota :‬‬
‫‪ :model -‬يمثل نموذج السيارة (مثال‪.)Camry :‬‬
‫‪ :color -‬يمثل لون السيارة‪.‬‬
‫‪ :speed -‬يمثل سرعة السيارة بالكيلومترات في الساعة‪.‬‬

‫‪ -‬الوظائف (‪:)Methods‬‬
‫‪ :init__(self, brand, model, color)__ -‬وظيفة البناء (‪ )Constructor‬التي تُستدعى عند إنشاء‬
‫كائن من الصنف‪ .‬تستقبل الوظيفة معلومات عن العالمة التجارية‪ ،‬النموذج‪ ،‬واللون‪ ،‬وتعين القيمة االفتراضية للسرعة‬
‫على ‪.0‬‬
‫‪ :accelerate(self, increment) -‬تزيد السرعة بقيمة محددة (الزيادة المحددة في الوحدات المحددة‪ ،‬على‬
‫سبيل المثال كم‪/‬ساعة)‪.‬‬
‫‪ :brake(self, decrement) -‬تقلل السرعة بقيمة محددة (االنخفاض المحدد في الوحدات المحددة‪ ،‬على‬
‫سبيل المثال كم‪/‬ساعة)‪ .‬إذا كانت السرعة أقل من القيمة المطلوبة لإلبطاء‪ ،‬فإنها تضبط السرعة على الصفر‪.‬‬
‫‪ :change_color(self, new_color) -‬تغيير لون السيارة إلى اللون المحدد‪.‬‬
‫‪ :print_details(self) -‬طباعة تفاصيل السيارة بما في ذلك العالمة التجارية‪ ،‬النموذج‪ ،‬اللون‪ ،‬والسرعة‬
‫الحالية‪.‬‬
‫‪ :change_brand(self, new_brand) -‬تغيير عالمة السيارة إلى العالمة المحددة‪.‬‬
‫‪ :change_model(self, new_model) -‬تغيير نموذج السيارة إلى النموذج المحدد‪.‬‬
‫‪ :get_speed(self) -‬استرجاع السرعة الحالية للسيارة‪.‬‬
‫‪ :stop(self) -‬إيقاف السيارة عن طريق ضبط السرعة على الصفر‪.‬‬
‫‪ :set_speed(self, new_speed) -‬تعيين السرعة الجديدة للسيارة‪.‬‬
‫‪ :honk(self) -‬طباعة عبارة "‪ "!Honk honk‬لتمثيل صوت البوق‪.‬‬

You might also like