Lecture 23
Lecture 23
Lecture 23
Lecture Outline
– CS1001 Lecture 23 –
Example: revised Circle class
import math
class Circle:
def __init__(self, x = 0, y = 0, radius = 0):
self.__x = x
self.__y = y
self.__radius = radius
def getX(self):
return self.__x
def getY(self):
return self.__y
def getRadius(self):
return self.__radius
def getPerimeter(self):
return 2 * self.__radius * math.pi
def getArea(self):
– CS1001 Lecture 23 – 1
return self.__radius * self.__radius * math.pi
– CS1001 Lecture 23 – 2
return 1
elif self.__radius < secondCircle.__radius:
return -1
else:
return 0
– CS1001 Lecture 23 – 3
Using the Circle class
x1 = float(input("Enter x1: "))
y1 = float(input("Enter y1: "))
radius1 = float(input("Enter radius1: "))
x2 = float(input("Enter x2: "))
y2 = float(input("Enter y2: "))
radius2 = float(input("Enter radius2: "))
c1 = Circle(x1, y1, radius1)
c2 = Circle(x2, y2, radius2)
print("Area for c1 is", c1.getArea())
print("Perimeter for c1 is", c1.getPerimeter())
print("Area for c2 is", c2.getArea())
print("Perimeter for c2 is", c2.getPerimeter())
print("c1 contains the center of c2?",
c1.containsPoint(c2.getX(), c2.getY()))
print("c1 contains c2?", c1.contains(c2))
print("c2 in c1?", c2 in c1)
print("c1 overlaps c2?", c1.overlaps(c2))
print("c1 < c2?", c1 < c2)
Sample output:
Enter x1: 3
Enter y1: 5
Enter radius1: 2
Enter x2: 8
Enter y2: 7
Enter radius2: 3
Area for c1 is 12.566370614359172
Perimeter for c1 is 12.566370614359172
– CS1001 Lecture 23 – 4
Area for c2 is 28.274333882308138
Perimeter for c2 is 18.84955592153876
c1 contains the center of c2? False
c1 contains c2? False
c2 in c1? False
c1 overlaps c2? False
c1 < c2? True
– CS1001 Lecture 23 – 5
Inheritance
– CS1001 Lecture 23 – 6
Inheritance
– CS1001 Lecture 23 – 7
Inheritance example
GeometricObject
- color: str the color of the object (def: green)
- filled: bool filled or not filled (def: True)
GeometricObject(color: Construct a GeometricObject
str, filled: bool) with specified color and fill
Rectangle
Circle - : float
- radius: float - float
Circle(radius: float, color: R :
str, filled: bool) float, color: str : ,
– CS1001 Lecture 23 – 8
Example: geometric objects
def getColor(self):
return self.__color
def isFilled(self):
return self.__filled
def __str__(self):
return "color: " + self.__color + \
" and filled: " + str(self.__filled)
– CS1001 Lecture 23 – 9
Defining inheritance
class subClassName(superClassName):
• For example,
class Circle(GeometricObject):
– CS1001 Lecture 23 – 10
Example: geometric objects
def getRadius(self):
return self.__radius
def getArea(self):
return math.pi*self.__radius**2
def getDiameter(self):
return 2*self.__radius
def getPerimeter(self):
return 2*math.pi*self.__radius
def printCircle(self):
print(self.__str__() + " radius: " + str(self.__radius))
– CS1001 Lecture 23 – 11
Example: geometric objects
def getWidth(self):
return self.__width
def getHeight(self):
return self.__height
def setWidth(self,width):
self.__width = width
def setHeight(self,height):
self.__height = height
def getArea(self):
return self.__width*self.__height
def getPerimeter(self):
return 2*(self.__width+self.__height)
– CS1001 Lecture 23 – 12
Example: geometric objects
– CS1001 Lecture 23 – 13
Example: geometric objects
circle = Circle(1.5)
print("A circle", circle)
print("The radius is", circle.getRadius())
print("The area is", circle.getArea())
print("The diameter is", circle.getDiameter())
print("The color is", circle.getColor())
print("The circle is"," not" if not circle.isFilled() else "",
" filled",sep="")
circle.printCircle()
rectangle = Rectangle(2,4,filled=False)
print("\nA rectangle", rectangle)
print("The area is", rectangle.getArea())
print("The perimeter is", rectangle.getPerimeter())
print("The color is", rectangle.getColor())
print("The rectangle is"," not" if not rectangle.isFilled() else "",
" filled",sep="")
Output:
A circle color: red and filled: True
The radius is 1.5
The area is 7.0685834705770345
The diameter is 3.0
The color is red
The circle is filled
color: red and filled: True radius: 1.5
– CS1001 Lecture 23 – 14
A rectangle color: blue and filled: False
The area is 8
The perimeter is 12
The color is blue
The rectangle is not filled
– CS1001 Lecture 23 – 15