python--Object-oriented

Source: Internet
Author: User

First, process-oriented, in accordance with the problem-solving steps, step by step, the idea is relatively simple, but difficult to maintain, object-oriented programming is to divide the problem, each module to build the final total solution

1. Object-oriented programming for shot putting problem

1  fromMathImportsin, cos, radians2  #Create Class3 classProjectile:4     def __init__(self, angle, velocity, height):5         6         #Create a state to create a projectile object based on the given emission angle, initial velocity, and position7Self.xpos = 0.08Self.ypos =Height9theta =radians (angle)TenSelf.xvel = Velocity *cos (theta) OneSelf.yvel = Velocity *sin (theta) A  -     #methods for creating classes -     defUpdate (self, time): the         #update the state of the projectile -Self.xpos = Self.xpos + Time *Self.xvel -Yvell = self.yvel-9.8 * Time -Self.ypos = Self.ypos + Time * (Self.yvel + Yvell)/2.0 +Self.yvel =Yvell -   +     defGetY (self): A         #returns the angle of the projectile body at         returnSelf.ypos -   -     defGetX (self): -         #returns the distance of the projectile body -         returnSelf.xpos

Call class

1  fromProjectileImport*2  3 defgetinputs ():4A = eval (input ("Enter the launch angle (in degrees):"))5v = eval (input ("Enter the initial velocity (in meters/sec):"))6H = eval (input ("Enter The initial height (in meters):"))7t = eval (input ("Enter The time interval:"))8     returna,v,h,t9  Ten defMain (): OneAngle,vel,h0,time = Getinputs ()#Input Parameters AShot = Projectile (angle,vel,h0)#Call class, note that only three parameters are used -      whileShot.gety () >=0: - shot.update (Time) the     Print("\ndistance traveled:{0:0.1f}meters.". Format (Shot.getx ())) -     - if __name__=="__main__": -Main ()

2. GPA

1 #find the highest GPA students2 3  #Defining Classes4 classStudent:5     def __init__(self, name, hours, qpoints):6Self.name =name7Self.hours =float (hours)8Self.qpoints =float (qpoints)9         Ten     #Defining Methods One     defGetName (self): A         returnSelf.name -       -     defgetHours (self): the         returnself.hours -       -     defgetqpoints (self): -         returnself.qpoints +       -     defGPA (self): +         returnself.qpoints/self.hours A  at #define functions to get each row of data - defmakestudent (INFOSTR): -Name, hours, qpoints = Infostr.split ("\ t") -     returnStudent (name, hours, qpoints) -       - defMain (): in     #Open Input File -filename = input ("Enter Name the grade file:") toinfile = open (filename,'R') +     #the first student's record in the settings file is best -Best =makestudent (Infile.readline ()) the   *     #working with remaining line data in a file $      forLineinchinfile:Panax Notoginseng         #convert each row of data to a single record -s =makestudent (line) the         #If the student is currently the highest GPA, then record it down +         ifS.gpa () >Best.gpa (): ABest =s the infile.close () +   -     #print the highest GPA student information $     Print("The best student is:", Best.getname ()) $     Print("Hours:", Best.gethours ()) -     Print("GPA:", Best.gpa ()) -   the if __name__=='__main__': -Main ()

python--Object-oriented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.