WEEK12: defining our own classes/object-oriented programming
---------------------------------------------------------------
 W: more classes...


PLANET CLASS:

 - look at worksheet.planetclass and answer the questions
   at the bottom:

import math

earthmass = 5.97e24 # kg
au = 1.50e11        # meters

class Planet(object):

  def __init__(self,n,r,m,x,y,vx,vy):
    self.name = n
    self.radius = r
    self.mass = m
    self.x = x
    self.y = y
    self.vx = vx
    self.vy = vy

  def __str__(self):
    str = ""
    str = str + "    Name: %s\n" % (self.name)
    str = str + "  Radius: %0.1f (km)\n" % (self.radius)
    str = str + "    Mass: %0.1f (EM) = %0.2e (kg) \n" % (self.mass, self.massKG())
    str = str + " Density: %0.2e (kg/m^3) \n" % (self.density())
    str = str + "Distance: %0.1f (AU) = %0.2e (m) \n" % (self.distance(),\
        self.distanceM())
    return str

  def getX(self):
    return self.x

  def getY(self):
    return self.y

  def getVx(self):
    return self.vx

  def getVy(self):
    return self.vy

  def getName(self):
    return self.name

  def getRadius(self):
    return self.radius

  def getMass(self):
    return self.mass

  def massKG(self):
    return self.mass*earthmass

  def distance(self):
    r = math.sqrt(self.x**2 + self.y**2)
    return r

  def distanceM(self):
    return self.distance()*au

  def volume(self):
    v = (4.0/3.0)*math.pi*(self.radius**3)
    return v

  def surfaceArea(self):
    sa = 4.0 * math.pi * (self.radius**2)
    return sa

  def density(self):
    rho = self.massKG()/self.volume()
    return rho

  def setVx(self, vx):
    self.vx = vx

  def setVy(self, vy):
    self.vy = vy

  def moveTo(self, newx, newy):
    self.x = newx
    self.y = newy

if __name__ == '__main__':
  # ............name, radius, mass, x, y, vx, vy
  p = Planet("Tatooine", 5685, 0.78, 2.7, 0.0, 0.0, 2.0)
  print p
  earth = Planet("Earth", 6378, 1.0, 1.0, 0.0, 1.0, 4.0)
  print earth
  naboo = Planet("Naboo", 3397, 0.61, 1.57, 0.0, 0.0, 5.0)
  print naboo

###############################################################

* which method is the constructor? and how many arguments do you 
  need to specify?

* how many mutators are there? what are they called?

* what data is stored inside each planet object?

* how many accessor methods are there?

* what function is executed when I type "print earth"?

* what is the first parameter of the moveTo method?

* what is self in the moveTo method for earth.moveTo(0,0)?

* what does self.massKG() do in the density method??

PIZZA class:

 * let's write a silly example together: the Pizza class


class Pizza(object):
  """the pizza class"""

  def __init__(self, topping):
    """constructor for pizza class"""

    self.topping = topping
    self.slices = 8

  def __str__(self):
    """should return a string"""

    return "This %s pizza has %d slices left" % (self.topping, self.slices)

  def eatSlice(self):
    """eat 1 slice of pizza"""

    if self.slices > 0:
      self.slices = self.slices - 1

#############################################

def main():
  """test out our class here"""

  p = Pizza("cheese")
  print p
  p2 = Pizza("mushroom and onion")
  print p2
  p3 = Pizza("unicorn")
  print p3
  p3.eatSlice()
  print p3

#############################################

if __name__ == "__main__": main()


Your Turn...write a SHOPPINGCART class:

 * suppose you are working for amazon.com and have been asked to
   write a shopping cart class that works for code like this:

  cart = ShoppingCart()
  cart.setName("Jeffrey M. Knerr")
  cart.setEmail("knerr@cs.swarthmore.edu")
  cart.addItem("Python Programming: Intro to Comp Sci", 35.55)
  cart.addItem("Data Structures and Algorithms in C++", 72.85)
  cart.addItem("Samsung Galaxy S III", 589.99)
  cart.addItem("Webkinz Pink Pony", 7.54)
  print cart
  cart.removeItem(2)
  print cart


 * How else could we use classes/objects in the above example?
 * How about a Book class, or a Phone class, etc


CLASS WRITER vs CLASS USER:

 - notice how we are now the class USER
 - the only way to access or change a shopping cart is through the
   methods provided by the class WRITER