Making our own Classes and objects As in real life, we’re now creating classes of objects. a class that defines basic characteristics and functions that apply to all objects of that class Think of a class as a set of functions and variables that belong together to define a particular thing. E.g., Savings Account classes would have: A balance #variable, or attribute An owner’s name #variable, or attribute The ability to add money to the account #function, or method The ability to withdraw money from the account #function, or method An individual’s Savings Account might have: $542.79 as a balance Roderick Feckelbocker as the owner’s name
Classes: creating our own Types Making more complex Types Simple types: Int Float Boolean String Double Think of lists: Lists are a more complex type. They have methods (functions) associated with them E.g., alist.append(x) alist.index(x) alist.pop() Append(), index(), and pop() are examples of methods (functions) associated with lists. You can’t use the pop() method (function) with something of type int, right?
Making new classes Examples of things we might want to make classes for: Students: Properties: lastname, firstname, Methods (functions) –calculate gpa, calculate grade in class, calculate when you’ll graduate, calculate how much you owe Bank account: Properties: name, account#, balance Methods (functions addmoney, removemoney, calculateinterest,etc.) Anything in which you want to group together different properties and functions into one element
Let’s make a class for Rectangles: What methods do we want our rectangle class to have? What properties? Class Rectangle(object): def __init__(self,w,h): self.height = h self.width = w def getArea(self): return(self.height * self.width)
Let’s make a class for Rectangles: What methods do we want our rectangle class to have? What properties? class Rectangle(object): def __init__(self,h,w): self.height = h self.width = w def area(self): return(self.height * self.width) x = Rectangle(10, 10) y = Rectangle(20, 5) print(x.area()) print(y.area())
class Rectangle(object): def __init__(self, width, height): #[CONSTRUCTOR] self.width = width self.height = height def area(self): return self.width * self.height def boxvol(self,x): return self.width * self.height * x x = Rectangle(10, 10) #[EXAMPLES] y = Rectangle(20, 5) print(x.area()) print(y.boxvol(5)) #weird one, but you can do it! print(Rectangle(6,8).area()) class 1 (started matlab)
Classes Does the problem involve data that is related to a single concept? (e.g., a student, an airline ticket, a car) Write down the kinds of data in the problem and organize them into classes (some problems may need multiple classes). Write: class definition a list of properties belonging to objects of that class a constructor (__init__) An example function some example class objects.
Employee Class class Employee: def __init__(self, n, s): #Again,2 underscores on both sides!! self.name = n self.salary = s def displayEmployee(self): print("Name : "+self.name+", Salary: "+ self.salary) x = Employee(“bob”,32000) x.displayEmployee()
Employee Class class Employee: def __init__(self, n, s): self.name = n self.salary = s def displayEmployee(self): print("Name : "+self.name+", Salary: "+ self.salary) def getRaise(self,x): self.salary = self.salary + x x = Employee(“bob”,32000) x.displayEmployee() x.getRaise(2000)
Student class? Properties: Methods: calcGrade? first name, last name, lab score, exam score, Project score Methods: calcGrade?
Student Class? class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project def calcgrade(self): x= self.lab * 0.25 + self.exam * 0.5 + self.project * 0.25 return(x)
Write a class for a circle? Functions? Properties?
class Circle(object): def __init__(self, x,y,z): self.radius = x self.area=y self.circumference=z x= Circle(3,7,8) Problem here: area and circumference are dependent on the radius Can you write methods to calculate the area and the circumference?
What type should it return? class Circle(object): def __init__(self,k): self.radius = k self.circumference = self.getcirc() self.area = self.calcarea() def getcirc(self): return(self.radius * 2 * pi) def calcarea(self): return(self.radius **2 * pi) def changerad(self,k): x = Circle(3) Print(x.area) circ1.changerad(4) Print(circ1.area) Can we include in the class a function that checks if this circle is bigger than another circle? What type should it return?
from math import * class Circle(object): def __init__(self,k): self.radius = k self.circumference = self.getcirc() self.area = self.calcarea() def getcirc(self): return(self.radius * 2 * pi) def calcarea(self): return(self.radius **2 * pi) def isbigger(self, circ2): return(self.area > circ2.area) x = Circle(3) y = Circle(4) z = Circle(2) print(x.area) print(x.isbigger(y)) print(x.isbigger(z))
Student – what if we wanted a grade property? class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project self.grade = self.getgrade() def getgrade(self): x= self.lab * 0.25 + self.exam * 0.5 + self.project * 0.25 return(x) Now make a student. Print his/her grade.