shape6: shape6.py

#
# Just like shape6 java example, shows simple python class syntax
#

#
# Definition of class Rect
#
class Rect: #//*1 Constructor

	# Constructor
	def __init__ (self, xin, yin, widin, htin): #//*1
		self._x = xin #//*2 Instance variables
		self._y = yin #//*2
		self._wid = widin #//*2
		self._ht = htin #//*2

	def setPos (self, xin, yin):  #//*3 First argument = self
		self._x = xin #//*3
		self._y = yin #//*3

	def setSize (self, widin, htin):
		self._wid = widin
		self._ht = htin

	def draw (self):
		print ("Drawing rectangle at " + " " + str(self._x) + " " + str(self._y) + " " + str(self._wid) + " " + str(self._ht))

	def area (self): #//*3
		return self._wid * self._ht #//*3

#
# Definition of class Circle
#
class Circle:

	def __init__ (self, xin, yin, radin):
		self._x = xin
		self._y = yin
		self._rad = radin

	def setPos (self, xin, yin):
		self._x = xin
		self._y = yin

	def setSize (self, radin):
		self._rad = radin

	def draw (self):
		print ("Drawing circle at " + " " + str(self._x) + " " + str(self._y) + " " + str(self._rad))

	def area (self):
		return self._rad * self._rad * 3.14159

#
# A main program for testing our objects
# (it is just inline code, no Main object)
#

# Instantiate and initialize the objects
r1 = Rect (10, 10, 50, 50) #//*4 Code to test Rect and Circle
r2 = Rect (80, 10, 10, 100) #//*4
c1 = Circle (35, 90, 10) #//*4

# "Draw" them
r1.draw() #//*4
r2.draw() #//*4
c1.draw() #//*4

# Compute and print total area
print ("Total area = " + " " + str(r1.area() + r2.area() + c1.area())) #//*4
[download file]