CS 108 Lab 1 - User-defined Classes

Preamble

A class serves to encapsuate data using a a set of variables and methods to manipulate this data. We have previously used predefined classes like the graphics and time. In today's lab, we will define our own class to represent a simple graphics object.

Objectives

  1. Understand the concept of encapsulation using classes.
  2. To define a class and associated methods to display a fish.
  3. Create multiple instances of the Fish class and manipulate them with methods.

Background

Here is an example from the textbook illustrating a simple class,

# The Student class encapsulates information pertaining to a student and methods to manipulate this information.

class Student:

      # method to initialize data within the class
      def __init__(self, name, address, year):
            self.name = name
            self.address = address
            self.year = year

      # method to display student infromation
      def displayInfo(self):
            print self.name, ";", self.address, ";", self.year

      # method to extract student's name contained within the class
      def getName(self):
            return self.name

      # methods to extract address and year can be defined similarly
      def getAddress(self):
            return self.address

      # method to change student's address
      def setAddress(self, address):
            self.address = address

      # method to change student's year
      def setYear(self, year):
            self.year = year

# The client method
def main():

      # Lets create two instances of the Student class
      s1 = Student("John Wayne","Boston, Ma", "Freshman")
      s2 = Student("Jack Nicholson","Brookline, Ma", "Senior")

      s1.displayInfo()
      s2.displayInfo()

      # Now lets change Jack's address and John's year.
      s1.setYear("Sophomore")
      s2.setAddress("Cambrdidge, Ma")

      s1.displayInfo()
      s2.displayInfo()

# Call main method.
main()
################################

Required Tasks :

Save graphics.py in the same directory as Fish.py below.

############################################################################
#
# fish.py
# A class which illustrates a class , using John Zelle's graphics.py module.
# Author: Aaron Stevens (azs@bu.edu)
#

import time
from graphics import *

############################################################################
class Fish:

  # init method is used to initialize class member variables, similar in spirit to init method in our Student example

  def __init__(self, pt, color, win):

      # 'pt' is the coordinate for the Fish within the window
      self.pt = pt
      self.color = color
      self.win = win
      self.size = 40
      # The 'direction' variable serves to point the Fish in the correct direction.
      self.direction = "left"
     
  ############################################################################
  # The draw method displays a Fish in the window at coordinate specified by 'self.pt'.
  # The fish is defined by three parts, self.eye, self.body, self.fin.
  # We also use 'self.direction' to flip the fish to point in the correct direction.
  # Nothing to change in this method unless you want to change how the fish looks.

  def draw(self):

      # Flip the fish based on direction.
      if self.direction == "right":
          eye_offset = self.size/3
          fin_offset1 = -self.size/3
          fin_offset2 = -1.2*self.size
      else:
          eye_offset = -self.size/3
          fin_offset1 = self.size/3
          fin_offset2 = 1.2*self.size
         
      # Setup the fin coordinates as a polygon and display it.
      self.fin = Polygon([ Point(self.pt.getX()+fin_offset1, self.pt.getY()),
              Point(self.pt.getX()+fin_offset2, self.pt.getY()+self.size),
              Point(self.pt.getX()+fin_offset2, self.pt.getY()-self.size)
              ])
      self.fin.setFill(self.color)
      self.fin.draw(self.win)

      # Setup the body as circle and display it.
      self.body = Circle(self.pt, self.size)
      self.body.setFill(self.color)
      self.body.draw(self.win)
     
      # Setup the eye as circle and display it.
      self.eye = Circle(Point(self.pt.getX()+eye_offset,self.pt.getY()-self.size/3),self.size/8)
      self.eye.setFill("black")
      self.eye.draw(self.win)


  ############################################################################
  # The undraw() method erases all parts of the Fish.

  # TASK TO DO
  # Write the prototype for undraw() method. This method needs to call undraw()
  # for each part of the fish (i.e. for self.eye, self.fin and self.body).
 
  ############################################################################
  # The setColor() method sets the color for various parts of the Fish.

  def setColor(self, color):

      #*** TASK TO DO ***
      # Set the class member variable 'set.color' to the input 'color' value.
      # Also set colors for the 'self.fin' and 'self.body' using their setColor() method.
      return
     
  ############################################################################
  # The setSize() method changes size of the Fish.

  def setSize(self, size):

      #*** TASK TO DO ***
      # Set the class member variable 'set.size' to the input 'size' value.
      # Then invoke 'undraw()' followed by 'draw()' on self to redisplay the fish.
      return

 
  ############################################################################
  # move method serves to move the fish in the window.


      #*** TASK TO DO ***
      # Define the prototype for move() method. The input arguments are displacement in x (dx) and displacement in y (dy).
      # See the invocation of move() within main() to understand how it is used.
      # Set self.direction to "left" or "right" based on whether 'dx' is positive or negative.
      # Set self.pt to the new Point after moving current coordinates by (dx, dy).
      # Hint: use self.pt.getX() to get current x coordinate.
      # Then invoke 'undraw()' followed by 'draw()' on self to redisplay the fish.
      return
 
############################################################################
def main():
  ## create our graphical window first
  win = GraphWin("Fish", 800, 600)

  # Create two instances of Fish.
  s1 = Fish(Point(300, 400), "pink", win)
  s1.draw()
  s2 = Fish(Point(400, 200), "blue", win)
  s2.draw()
  s2.setSize(100)
 
  # Now lets move the fish around.
  for i in range(1, 10):
      time.sleep(0.1)
      s1.move(20, 0)
      s2.move(-20, 0)
 
  s1.setColor("green")
  s2.setColor("yellow")

  for i in range(1, 10):
      time.sleep(0.1)
      s2.move(20, 0)
      s1.move(-20, 0)
 
  win.close()

############################################################################
main()

WebSubmit

  • Use WebSubmit to upload these files to the homework repository location.
  • Log on to WebSubmit by the appropriate URL:

    CS108 A1: http://azs.bu.edu/websubmit?COURSE=cs108
  • After entering your username and password, click the "Go to WebSubmit" button.
  • Find the link "Submission page for lab07" and click on it.
  • Finally click the "Upload Files" button and submit Fish.py.
  • Check your email to confirm that your (correct) files were successfully submitted.

CS108