CMPU102 51 Spring 2020 Data Structures and Algorithms











- Slides: 11
CMPU-102 -51 Spring, 2020 Data Structures and Algorithms Object-Oriented Programming: methods Rui Meireles Peter Lemieszewski
More about Object-Oriented Programming 2
Recall Object-Oriented Programming • A program as a model of the real world • Object is the building block • An object: – Has state: instance variables – Has behavior: methods – Interacts with other objects: by calling their methods • Objects are grouped into classes – Shared behavior – Shared state structure (not content) 3
Class structure • Class name • Class members – Fields (static or instance) – Methods • UML (Unified Modeling Language) class representation: Class name fields methods 4
Inheritance • Reuse common state/behavior while introducing some new specific state/behavior • Promotes code reuse, modularity, scalability • Terminology, synonyms: – Superclass Subclass – Parent class Child class – Base class Derived class • Definition: indicates inheritance – class Subclass extends Superclass {. . . } – E. g. class Student extends Person {. . . } 5
Object-Oriented Development • My opinion: – Object Oriented Design and Development is variant of ‘Fusion Method’ • https: //www. hpl. hp. com/hpjournal/96 aug/aug 96 a 3 a. pdf – Let’s have a look at this pdf 6
More about methods 7
What is a method, really? • • A named code block that can be referenced by its name Input: specified by parameter list Output: optional return value Format: [access] <return. Type> <method. Name>(<param. List>){ // signa. … // method body } • E. g. compute the area of a rectangle: double area. Of. Rectangle(double h, double l){ return h*l; } • Calling a method changes the flow of the program – Execution at call location is suspended, resumed once called method returns – Think of the method call as pasting the code of the method over the call 8
Usefulness of methods • They hide complexity – Caller only needs to know what it does, not how it does it • They allow us to break down complex problems into multiple simpler ones that can be combined to obtain overall solution – This is called decomposition • Programs provide services to users – Methods provide services to programs Complete Task Subtask 1 Subtask 2 a Subtask 3 Subtask 2 b 9
Utility methods • Methods that implement well-defined algorithms whose output depends solely on its parameters – I. e. they don’t depend on additional state, e. g. instance variables • They are typically implemented as static methods – Called as Class. Name. method. Name(); • E. g. Math. random(); – And… class methods do not need an associated object! It’s true! 10
Useful utility methods in the Math class Math. abs(x) Returns the absolute value of x Math. min(x, y) Returns the smaller of x and y Math. max(x, y) Returns the larger of x and y Math. sqrt(x) Returns the square root of x Math. log(x) Returns the natural logarithm of x (loge x ) Math. exp(x) Returns the inverse logarithm of x (e x ) Math. pow(x, y) Returns the value of x raised to the y power (x y ) Math. sin(theta) Returns the sine of theta, measured in radians Math. cos(theta) Returns the cosine of theta Math. tan(theta) Returns the tangent of theta Math. asin(x) Returns the angle whose sine is x Math. acos(x) Returns the angle whose cosine is x Math. atan(x) Returns the angle whose tangent is x Math. to. Radians(degs) Converts an angle from degrees to radians Math. to. Degrees(rads) Converts an angle from radians to degrees 11