-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivity1.py
More file actions
50 lines (42 loc) · 1.94 KB
/
Copy pathactivity1.py
File metadata and controls
50 lines (42 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Define the Smartphone class
class Smartphone:
# Constructor (__init__ method) to initialize attributes
def __init__(self, brand, model, storage, battery_life):
self.brand = brand # Attribute: Brand of the smartphone
self.model = model # Attribute: Model of the smartphone
self.storage = storage # Attribute: Storage capacity in GB
self.battery_life = battery_life # Attribute: Battery life in hours
# Method to display smartphone details
def display_details(self):
print(f"Brand: {self.brand}, Model: {self.model}, Storage: {self.storage}GB, Battery Life: {self.battery_life} hours")
# Method to simulate using the smartphone
def use(self, hours):
if hours <= self.battery_life:
self.battery_life -= hours
print(f"Used {hours} hours. Remaining battery: {self.battery_life} hours.")
else:
print("Not enough battery!")
# Create an object (instance) of the Smartphone class
my_phone = Smartphone("Apple", "iPhone 15", 128, 24)
# Call methods on the object
my_phone.display_details()
my_phone.use(5)
my_phone.display_details()
# Define the GamingPhone subclass
class GamingPhone(Smartphone):
# Constructor for GamingPhone
def __init__(self, brand, model, storage, battery_life, graphics_card):
super().__init__(brand, model, storage, battery_life) # Call the parent class constructor
self.graphics_card = graphics_card # New attribute
# New method specific to GamingPhone
def play_game(self, hours):
if hours <= self.battery_life:
self.battery_life -= hours
print(f"Played game for {hours} hours. Remaining battery: {self.battery_life} hours.")
else:
print("Not enough battery!")
# Create an object of the GamingPhone class
my_gaming_phone = GamingPhone("Asus", "ROG Phone 6", 256, 36, "Adreno 660")
# Call methods
my_gaming_phone.display_details()
my_gaming_phone.play_game(3)