程序代写 INFO1113.

Problem Solving using Objects
Sue Inn Chng
The University of Credit:
https://www.mathsisfun.com/algebra/linear-equations.html https://en.wikipedia.org/wiki/Blackjack#/media/File:Blackjack_game_1.JPG

Copyright By PowCoder代写 加微信 powcoder

Linear Equation
– Let’sdefineaclasstorepresentlinearequations.
– Identifytheattributesthatalllinearequationspossess.
– Canyoulistcommonactionsthatareperformedonalinearequation?
The University of 2

Linear Equation
– Classisanabstractrepresentationofalllinearequations. – Alllinearequationshasaslopeandintercept.
– Mathematicalformula:y=mx+c
– Givenalinearequation,wecandetermine:
• If a given Cartesian coordinate (x, y) is part of a line.
• The y-coordinate of a Cartesian coordinate that is on the line.
+slope: float
+intercept: float
+get_slope(self) +get_intercept(self) +check_on_line(self, point) +calc_y(self, x)
The University of 3

Application: Points and Line
– Review the codes in Ed Lesson. – Classdefinition:
– Drawtheclassdiagramtorepresentalltheuser-defineddatatypes that are present in this lesson.
– Arethereanyinstancevariablesandinstancemethodspresentinthe codes?
– Arethereanyclassvariablesandclassmethodspresentinthecodes? – Objectinstantiationandmethodcalls
– Predicttheoutputoftheprogram.Runtheprogramtocheckyour answer.
– Explainthetraceoftheprogramtoanotherhuman. The University of 4

Class Methods: Points and Line
– Assumingthattwopointsareconnectedbyastraightline,can we calculate the slope given two points?
– Define a method called calc_slope() that receives two arguments, pointA and pointB, to perform this calculation.
– Does this method require access to a Linear object’s instance variables to successfully perform the calculation?
– Canthismethodaccessitsowninstancevariables?
– GotoEdLesson.
The University of 5

Class Variables
– Class body can contain variables (attributes) AND methods (behaviour/functions).
– Two types of variables:
– Instance variables (previous example)
• Eachobjecthasitsowncopy
• Initializethrough__init__method(constructor)
• Touseinstancevariable,mustusekeywordselfwithdotoperator: self.attribute
– Class variables
All objects share the same copy
Must be define outside other methods without using self
Touseclassvariable,mustusefullyqualifiednamewithdotoperator:
.attribute
The University of 6

Class variables: World Population
– Go to Ed Lesson and review the modified codes for class Person.
– Can you spot the instance variables and class variables in this program?
– What value would be returned when these line of codes are executed:
The University of 7

Application: Blackjack Card Game
– There are many variations of . Write a Python program that allows the user to play a simplified black jack (one deck of cards with a single player) game with the computer as the dealer.
– Gameplay:
– Theplayerwhohastheclosestnumberto21wins. – Duringthegame,playerscanoptto:
• Hit – Draw another card
• Hold – Take no more cards
– If the dealer and the player opt to hold, both sides show their cards to
determine the winner.
The University of 8

Summary – Instance Method and Variables
• Must have self as its first parameter.
• Function call syntax: objectName.method_name(args)
• Do NOT have self as its first parameter.
• Function call syntax: ClassName.method_name(args)
• All objects have its own value (unique to each object)
• Defined within body of __init__ or setter method
• Initialize during object instantiation through __init__ or modified through setter method
• Syntax to use variable: objectName.variable
• All objects share same value
• Defined outside all other
methods within the class body
without using self
• Exist without an object
• Syntax to use variable:
ClassName.variable
The University of 9

Summary – Problem Solving using Objects
– Introductiontotheapproachtoday.MoreinINFO1113.
– Forthisunit,youmustbeableto:
– Implementthemethodsforagivenuser-defineddatatype.
– Performmethodcallsforbothinstanceandclassmethods
– ExplainbasicOOPterminologies–classes,objects,self,instance variables/methods, class variables/methods
The University of 10

Reading This Week
– Python Official Tutorial: https://docs.python.org/3/tutorial/classes.html#a-first-look-at- classes
– Chapter15,16,17.Downey,A.B.(2015).ThinkPython:How to Think Like a Computer Scientist (2e ed.). O’ , Incorporated.
The University of 11

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com