-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmethods.py
More file actions
162 lines (133 loc) · 4.57 KB
/
methods.py
File metadata and controls
162 lines (133 loc) · 4.57 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""
Instance Methods Exercises - Complete the classes below
Run this file with: python topic.py
Or work interactively with: python3 -i topic.py
"""
# Exercise 1: Basic Instance Methods
class Calculator:
"""A simple calculator class demonstrating instance methods."""
def __init__(self, initial_value=0):
"""Initialize calculator with starting value."""
# Your code here
pass
def add(self, value):
"""Add value to current result."""
# Your code here
pass
def subtract(self, value):
"""Subtract value from current result."""
# Your code here
pass
def multiply(self, value):
"""Multiply current result by value."""
# Your code here
pass
def divide(self, value):
"""Divide current result by value."""
# Your code here - handle division by zero
pass
def get_result(self):
"""Return current result."""
# Your code here
pass
def reset(self):
"""Reset calculator to zero."""
# Your code here
pass
# Exercise 2: Methods with Self Parameter
class Counter:
"""A counter that can increment, decrement, and track state."""
def __init__(self, start=0, step=1):
"""Initialize counter with starting value and step size."""
# Your code here
pass
def increment(self):
"""Increase counter by step size."""
# Your code here
pass
def decrement(self):
"""Decrease counter by step size."""
# Your code here
pass
def get_value(self):
"""Return current counter value."""
# Your code here
pass
def set_value(self, value):
"""Set counter to specific value."""
# Your code here
pass
def is_positive(self):
"""Return True if counter is positive."""
# Your code here
pass
# Exercise 3: Methods that Modify Object State
class TodoList:
"""A todo list that manages tasks."""
def __init__(self):
"""Initialize empty todo list."""
# Your code here
pass
def add_task(self, task):
"""Add a new task to the list."""
# Your code here
pass
def complete_task(self, task):
"""Mark a task as completed."""
# Your code here
pass
def remove_task(self, task):
"""Remove a task from the list."""
# Your code here
pass
def get_pending_tasks(self):
"""Return list of tasks not yet completed."""
# Your code here
pass
def get_completed_tasks(self):
"""Return list of completed tasks."""
# Your code here
pass
def get_task_count(self):
"""Return total number of tasks."""
# Your code here
pass
def run_tests():
"""Test all the classes to verify they work correctly."""
print("Testing Calculator:")
calc = Calculator(10)
calc.add(5)
print(f"10 + 5 = {calc.get_result()}") # Should print 15
calc.multiply(2)
print(f"15 * 2 = {calc.get_result()}") # Should print 30
calc.divide(3)
print(f"30 / 3 = {calc.get_result()}") # Should print 10.0
calc.reset()
print(f"After reset: {calc.get_result()}") # Should print 0
print("\nTesting Counter:")
counter = Counter(0, 2) # Start at 0, step by 2
counter.increment()
print(f"After increment: {counter.get_value()}") # Should print 2
counter.increment()
print(f"After increment: {counter.get_value()}") # Should print 4
counter.decrement()
print(f"After decrement: {counter.get_value()}") # Should print 2
print(f"Is positive: {counter.is_positive()}") # Should print True
print("\nTesting TodoList:")
todo = TodoList()
todo.add_task("Buy groceries")
todo.add_task("Walk the dog")
todo.add_task("Study Python")
print(f"Total tasks: {todo.get_task_count()}") # Should print 3
print(f"Pending: {todo.get_pending_tasks()}") # Should show 3 tasks
todo.complete_task("Buy groceries")
print(f"After completing one task:")
print(f"Pending: {len(todo.get_pending_tasks())}") # Should print 2
print(f"Completed: {len(todo.get_completed_tasks())}") # Should print 1
if __name__ == "__main__":
print("Python Instance Methods Exercises")
print("=" * 45)
print("Complete the classes above, then run this file to test them.")
print("Uncomment the line below when you're ready to test:")
print()
# run_tests() # Uncomment this line to run tests