>>     < >




In-class notes for 04/09/2014

CS 121B (CS1), Spring 2014

Submitted questions on assignments and technology

  • Seeing Python3 return values.

    • Four ways to run Python3: book ActiveCode windows (can use print() to see return values); idle3 on a Link machine (interactive, i.e., return values are shown for each instruction before you enter the next instruction); using python3 interactively on a link machine (also shows return values); using python3 with your program as a command-line argument (can use print() to see return values)

  • ______

Upcoming

  • Homework 23

  • Science Symposium

    • Four events, all in Tomson 280

      • Thursday 7pm panel -- 4 points EC for a half-page response

      • Friday 3:00pm speaker -- 2 point EC for a half-page response

      • Friday 4:30pm speaker -- 2 point EC for a half-page response

      • Friday 7:30pm CS speaker -- 4 points EC for a half-page response

Submitted questions on readings

Defining classes

  • review of OO terminology: object, method, state variable, class.

  • Example of spec and definition of a class (Account)

    1. State variables are implicitly declared. (We will list them in comments.)

          class Account:
              # state variables:
              #   balance -- float, represents the account balance
              #   initBal -- float, represents the initial account balance
      
    2. Methods include an extra initial argument usually called self, representing the object whose method is being called.

          class Account:
              ...
          
              def getBalance(self):
                  return self.balance
          
              def getInitBal(self):
                  return self.initBal
          
              def deposit(self, amt):
                  self.balance = self.balance + amt
                  return self.balance
          
              def withdraw(self, amt):
                  self.balance = self.balance - amt
                  return self.balance
      
      
      These methods are called as follows:
          a1 = Account(...)  # fill in argument for constructor
          a1.deposit(100.50)
          print( a1.getBalance() )
      
    3. A class's constructor is a function for initializing new objects.

      In Python, a class's constructor is called __init__, and the constructor is defined like a method.

          class Account:
      	...
      
              def __init__(self, bal):
                  self.balance = bal
                  self.initBal = bal
      



< >