Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engineering Computing

Similar presentations


Presentation on theme: "Engineering Computing"— Presentation transcript:

1 Engineering Computing
Week 4

2 Object Oriented Programming
Types like int and bool are common and useful in almost every program Other data types like File are useful, but its not practical for a language to include any and every data type Programmers can devise their own data type: Programmers devise classes They can then create instances of that class called objects

3 Classes A class is similar to a species of animal
Every class has a name Every class has certain attributes Properties and values that are important to that class Every class has methods Certain functions and blocks of code that the class knows how to execute

4 Example Class class Model(object): def __init__(self, NAME= ‘Zoolander): self.name = NAME def turnLeft(self): print "I cant turn left" def say(self,string): print string Constructor- The code executed to construct a new object Methods The different functions each object knows how to execute

5 How to use a class class Model(object): def __init__(self, NAME= ‘model’): self.name = NAME def turnLeft(self): print "I cant turn left" def say(self,string): print string We can instantiate objects of a particular class Every object will have the same type of attributes and will know the same functions Because each object is an instance of the class, they will have unique values for their attributes

6 String as Objects Strings are something we call an object
There are lots of things we might want to do with Strings Capitalization Reversal Split Objects have: Attributes Properties of the object Methods These are functions that the object knows how to execute that can do something or modify the object 9/18/2018 Lecture 1

7 String objects >>> ‘HITHERE’.lower() ‘hithere’
Method calls from objectives have the following syntax >>>Object.function(arguments) Example: >>> example = ‘Thisisastring’ >>>example.upper() THISISASTRING >>> ‘HITHERE’.lower() ‘hithere’ Functions can also take arguments >>> “This is GREAT”.replace(‘GREAT’,BAD’) “This is BAD” 9/18/2018 Lecture 1

8 Constructor class DNA(object): def __init__(self,sequence='AT'*5):
if sequence.strip('ATCG') == '': self.sequence = sequence.upper() else: self.sequence = 'ATCG' print sequence +' is not a valid sequence‘ def __init__(self) self.sequence = ‘GCTA’

9 Methods Just like functions, except these functions are specific to handle information and attributes for the object. Be sure to pass (self) as an argument def replaceSequence(self,string): if sequence.strip('ATCG') == '': self.sequence = string.upper else: self.sequence = 'ATCG' print string +' is not a valid sequence'

10 How to use classes >>>gene1 = DNA()
>>>gene2 = DNA(‘ATAGTA’) >>>gene1.sequence ‘GCTA’ >>>gene2.sequence >>>gene2.compliment() ‘TATCAT’ You need to instantiate an object before you can use it You can access properties with the dot notation You can access methods in the standard way and pass the appropriate arugments

11 Inheritance class Chromosome(DNA): def printGeneName(self): print ‘X’ Chromosome inherits all of the methods and properties associated with DNA We can add and specific different methods and attributes


Download ppt "Engineering Computing"

Similar presentations


Ads by Google