CS52 - Fall 2015 - Class 13

Example code in this lecture

   number_and_bit.py
   2to1mux.py

Lecture notes

  • admin
       - assignment 3 graded and returned
       - assignment 5
          - out
          - should be able to finish it after today's class

  • high level
       - inheritance in python: look at number_and_bit.py code

       - gates in python
          - overview of Node, Gate, Gate1 and Gate2
          - Look at AndGate and NotGate
          - Look at TwoToOneMultiplexer
             - look at testTwoToOne function

  • camelcasing vs. underscore
       - in python, we use underscores to separate multi-word variables and function names (anything lowercase
       - but use camelcasing for uppercase things (like classes)

  • inheritance in python: look at number_and_bit.py code
       - What does the Number class do?
          - like the Bit class from last time, but just stores a value (any value, in fact)
          - __str__ method:
             - like toString in Java
       
       - To inherit from a class (i.e. extend in Java) we put parenthesis after the class name and put the class we're extending from
          - NotSafeBit(Number)
             - NotSafeBit inherits from Number
             - We get all of the methods defined in Number, e.g. the constructor, set, get and __str__
       
       - When inheriting we can also define our own methods
          - if they don't exist already, they're added to the class
          - if they do exist already, they override the parent class's method

       - bit_and in NotSafeBit is a new method

       - Why is the class called NotSafeBit?
          - It's almost like the Bit class from last time
          - However, because we inherit from Number, we can't assume that the value will always be 0 and 1!

       - Look at the Bit class in number_and_bit.py code : How does his solve it?
          - override the constructor and the set methods to enforce 0 and 1 values

       - What will the different_types function print?
          Num: 10
          nsBits: 10, 1
          bits: 1, 1
          ns AND: 0
          bit AND: 1

          - declares a Number with value 10
          - declares two lists
          - in the nsBits list, adds two NotSafeBits constructed with 10 and 1
          - in the bits list, adds two Bits constructed with 10 and 1
          - Because the NotSafeBits are just numbers, the 10 stays as a 10 and gives us the wrong answer!
       

  • what does the following circuit do (there's a better picture in the slides posted for today)?

       s ----- not --- AND1
       in0 -|--------- AND1 ---- OR
        ------ AND2 -------- OR --- OUT
       in1 ------- AND2

       (or written in boolean logic: (!s && in0) || (s && in1) )

       - This is called a multiplexer (in particular, a 2-to-1 multiplexer)
          - The s bit the select or control bit
             - if s is 0, then whatever value is in in0 is output
             - if s is 1, then whatever value is in in1 is output
          - the truth table is:

          in0 in1 s | out
          0 0 0 | 0
          0 0 1 | 0
          0 1 0 | 0
          0 1 1 | 1
          1 0 0 | 1
          1 0 1 | 0
          1 1 0 | 1
          1 1 1 | 1

  • look at assign5-starter.py

  • look at 2to1mux.py code
       - requires that assign5-starter.py and and OrGate2 are defined
       - constructs