shape6

shape6.py  [download]

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

#
# Definition of class Rect
#
class Rect:

	# Constructor
	def __init__ (self, xin, yin, widin, htin): #//*1 Constructor
		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):
		return self._wid * self._ht

#
# 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)
r2 = Rect (80, 10, 10, 100)
c1 = Circle (35, 90, 10)

# "Draw" them
r1.draw()
r2.draw()
c1.draw()

# Compute and print total area
print ("Total area = " + " " + str(r1.area() + r2.area() + c1.area()))