8000 Add solution to new OOP exercise · MrDP18/python-basics-exercises@f0a1cba · GitHub
[go: up one dir, main page]

Skip to content

Commit f0a1cba

Browse files
committed
Add solution to new OOP exercise
1 parent 956acd3 commit f0a1cba

File tree

3 files changed

+83
-12
lines changed

3 files changed

+83
-12
lines changed

ch10-primer-on-oop/3-inherit-from-other-classes.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ def speak(self, sound="Bark"):
2525

2626

2727
# Exercise 2
28-
# Modified Dog class
29-
class Dog:
30-
species = "Canis familiaris"
28+
# Rectangle and Square classes
29+
class Rectangle:
30+
def __init__(self, length, width):
31+
self.length = length
32+
self.width = width
3133

32-
def __init__(self, name, age):
33-
self.name = name
34-
self.age = age
34+
def area(self):
35+
return self.length * self.width
3536

36-
def __str__(self):
37-
return f"{self.name} is {self.age} years old"
3837

39-
def speak(self, sound):
40-
if not isinstance(sound, str):
41-
raise TypeError("sound should be a string")
42-
return f"{self.name} says {sound}"
38+
class Square(Rectangle):
39+
def __init__(self, side_length):
40+
super().__init__(side_length, side_length)
41+
42+
square = Square(4)
43+
print(square.area())
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
The Whistling Gypsy
3+
(An Irish folk song)
4+
Whistling Gypsy Rover The whistling gypsy came over the hill,
5+
6+
down through the valley so shady.
7+
8+
He whistled and he sang till the green woods rang ,
9+
10+
and he won the heart of a lady.
11+
12+
13+
Ref.:
14+
15+
Ah dee doo, ah dee doo da day,
16+
17+
ah dee doo, ah dee day-ee.
18< 10000 code class="diff-text syntax-highlighted-line addition">+
19+
He whistled and he sang till the green woods rang,
20+
21+
and he won the heart of a lady.
22+
23+
24+
2.
25+
She left her fathers castle gate,
26+
27+
she left her own true lover.
28+
29+
She left her pearls and her fine french lace
30+
31+
to follow the gypsy rover.
32+
33+
34+
3.
35+
Her father saddled up his fastest steed,
36+
37+
he roamed the valley all over.
38+
39+
He sought his daughter at great speed
40+
41+
and the whistling gypsy rover.
42+
43+
44+
Ref.:
45+
46+
47+
4.
48+
He came at last to a mansion fine
49+
50+
down by the river —Cladyfi.
51+
52+
And there was music and there was wine
53+
54+
for the gypsy and his lady.
55+
56+
57+
58+
5.
59+
He is no gypsy, father, she said,
60+
61+
but Lord of these lands all over,
62+
63+
and I will stay ‚till my dying day
64+
65+
with the whistling gypsy rover.
66+
67+
68+
Ref.:
69+
70+
(Leo Maguire)

0 commit comments

Comments
 (0)
0