Before talking more about objects and classes, I want a little time for you to do some practice problems with classes and objects so you have a little experience with objects and classes. Therefore, I am going to talk about a different topic and we will come back to classes in a few days.
To implement a CPU we create electronic circuits using transistors and resistors. Physics tells us that to understand any circuit we must compute the voltage as a function of time at all points on the circuit, and physics gives formulas for this. When designing the circuits such as those appear inside a CPU, we will design the circuit to make sure each wire is only in one of two possible states: at zero voltage or high voltage (which is usually around 3 volts). We make sure that all points in the circuit are either at zero voltage or high voltage and no other values. (The actual voltage might not be exactly zero or exactly 3 volts, but the design is such that during the operation the voltage will be near one of these two values and nowhere else.) Therefore, when reasoning about these circuits we can just assume that each wire is either zero or high (which we interpret as zero or one). These types of circuits are called digital circuits.
Any boolean function can be implemented using logic gates
In the simulation from last time, the Bear starves at 20 turns and this is hard-coded into the live_a_little
function. Update the Bear class to instead of having a fixed number of turns before it starves, instead each Bear starves after a random number of turns between 16 and 24. You will need to create a property to store the number of turns before starving, set that property from the __init__ method with a call to the random module, and update the live_a_little
function to use your new property.
Design a class to represent a bank account. It should store a customer name, account ID, and balance. The __init__
method should take parameters for the name and ID and initialize the balance to zero. You should have the following methods. You should come up with sensible names for the properties and methods yourself (keep PEP8 section on Naming Conventions in mind).
A method to print out the name, ID, and balance to the screen.
Two methods to deposit or withdraw an amount of money which modifies the balance. If someone tries to withdraw more money than there is balance, print an error message to the screen.
A transfer method which takes two parameters: a balance and another account object. The money should be transfered from the self
account to the account passed as a parameter. Again, if there is not enough money then don't transfer anything and instead print an error message.
Write a few lines of test code to show your class is working properly. Create a couple accounts, add and remove money by calling the methods, and then transfer a few times, printing out the balances between transfers.