COSC 1 P 02 Cosc 1 P 02

  • Slides: 17
Download presentation
COSC 1 P 02 Cosc 1 P 02 Week 3 Lecture slides Birthdays are

COSC 1 P 02 Cosc 1 P 02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the most live the longest. (Rev. Larry Lorenzoni) Introduction to Computer Science 3. 1

COSC 1 P 02 Review · Memory Representation of: - Value variables - Reference

COSC 1 P 02 Review · Memory Representation of: - Value variables - Reference variables - Assignment of reference variables · Tiling patterns Introduction to Computer Science 3. 2

COSC 1 P 02 Dealing with Complexity · Limits on composition - Can only

COSC 1 P 02 Dealing with Complexity · Limits on composition - Can only go so deep before the nesting becomes unmanageable. - Requires separate index variables for each loop · Repetition of code - Duplicating code in a program is a bad thing ° Increases complexity unnecessarily ° More work. · Abstraction - Divide and conquer - Break problems into chunks - Let the process (code) be independent from operation of calling the code. ° E. g. Yertle. forward(40); runs magic code to move yertle. Introduction to Computer Science 3. 3

COSC 1 P 02 Two Squares · Consider drawing two squares at different places

COSC 1 P 02 Two Squares · Consider drawing two squares at different places on the screen - repeated code ° increased possibility of error · Drawing a square could be considered an operation (like drawing a line) - drawing a line done by a method (forward) - write a method to draw a square (draw. Square) · Example Introduction to Computer Science 3. 4

COSC 1 P 02 Methods · Method declaration - syntax ° name × verb

COSC 1 P 02 Methods · Method declaration - syntax ° name × verb ° body · Method call - syntax · Method execution - current code suspended - body of method executed - current code continues Introduction to Computer Science 3. 5

COSC 1 P 02 Eight Squares Revisited eight. Squares 2. java · Composition via

COSC 1 P 02 Eight Squares Revisited eight. Squares 2. java · Composition via methods - use method call instead of nesting · Example · Reduced complexity - Can think of drawing a square as a basic operation ° don’t have to worry about how it is done - code for Eight. Squares simpler - don’t have to worry about different index variables Introduction to Computer Science 3. 6

COSC 1 P 02 Scope · Declarations occur at different places - in class

COSC 1 P 02 Scope · Declarations occur at different places - in class - in method - in for statement · Scope - Where are names defined in declarations useable? - Rule ° in class – from { in class header to } at end of class × includes methods names ° in method – from { in method header to } at end of method ° in for – body of for (statements between { }) · Example · Composition via nesting vs via methods - non-interference Introduction to Computer Science 3. 7

COSC 1 P 02 What is an Object · An Object has 2 parts

COSC 1 P 02 What is an Object · An Object has 2 parts - Memory ° What it remembers ° State information ° Data - Behaviour ° What an object can do ° Actions which can be performed · A Class defines what an object is: - Instance Variables represent the memory - Methods define the behaviour Introduction to Computer Science 3. 9

COSC 1 P 02 Object Creation · New causes an object to be created

COSC 1 P 02 Object Creation · New causes an object to be created from the template (class) - E. g. new Turtle(); · An Object is initialized when it is created. - Initial memory and behaviour are set - New causes the constructor to run to create the object · Constructor - Is a method (initial behaviour) - Has the same name as the class - New causes the constructor to execute. Introduction to Computer Science 3. 10

COSC 1 P 02 Constructors, Methods & Objects · Methods are executed by an

COSC 1 P 02 Constructors, Methods & Objects · Methods are executed by an object - who is executing draw. Square? ° no object specified ° Turtle (yertle) doesn't know how · main method - is a method declaration - every Java program must have ate least one class (called the main class) that has a main method · Execution begins in body of main method - body includes object creation (new) ° creates a new Eight. Squares object ° on creation object executes its constructor · Local method call - when no object specified method is executed by same object as is executing the method call (e. g. the Eight. Squares object) - draw. Square() is shorthand for this. draw. Square() Introduction to Computer Science 3. 11

COSC 1 P 02 Eight Squares One More Time · Constructor is meant to

COSC 1 P 02 Eight Squares One More Time · Constructor is meant to initialize an object · Separate initialization from operation - Puts object into a known state. - write operations as methods - e. g. Turtle · Example - constructor simply creates display, turtle and connects them - method draw performs the operation to draw the eight squares - draw is executed by the Eight. Squares object created in main - E. g. new Eight. Squares 3(). draw(); ° New Eight. Squares 3() creates an object returning a reference ×. draw then calls the behavior of the new object. Introduction to Computer Science 3. 13

COSC 1 P 02 Hexagon Revisited · Complex scene with many figures - hard

COSC 1 P 02 Hexagon Revisited · Complex scene with many figures - hard to keep track of where drawing begins & ends · Centre figure on a point with a size of the figure (rather than a side) - method to draw figure starts from center & leaves pen in center - i. e. leaves turtle in same state as it started ° Turtle state becomes independent from drawing. · Geometry of a hexagon · Drawing a hexagon - move out from center - position down first side - draw sides - return to center & pen orientation · Example Introduction to Computer Science 3. 14

COSC 1 P 02 Expressions · Computations specified by expressions - like algebraic expressions

COSC 1 P 02 Expressions · Computations specified by expressions - like algebraic expressions in Mathematics - written on one line · Composed of - operators - constants - variables - functions · Evaluation order - by priority, left to right - parentheses to force order Introduction to Computer Science 3. 16

COSC 1 P 02 Two Hexagons · Consider drawing two hexagons of different size

COSC 1 P 02 Two Hexagons · Consider drawing two hexagons of different size · Size of hexagon fixed in method - different methods for different sizes? ° essentially identical code ° differs only by value of radius · Allow the method to vary depending on value of radius · Example - radius is parameter Introduction to Computer Science 3. 17

COSC 1 P 02 Parameters · Allows a method to be generalized over one

COSC 1 P 02 Parameters · Allows a method to be generalized over one or more values - radius for draw. Hexagon · Parameter (formal parameter) - declared in method header - behaves like a local variable within method body ° scope is method body - is initialized to value of argument at call · Argument (actual parameter) - an expression that produces a value of the declared type ° constant ° variable ° expression · Method execution - value of argument computed - calling code suspended - value of argument copied to parameter - body of method executed - calling code continues Introduction to Computer Science 3. 20

COSC 1 P 02 Polygons · Code for drawing all regular closed figures (polygons)

COSC 1 P 02 Polygons · Code for drawing all regular closed figures (polygons) similar - move out from center - rotate to facing down first side - for each side ° draw side ° rotate to next side - move back to center · Geometry of a polygon · Code would depend on two values: radius and number of sides - method with two parameters · Example · Note: number of times in for loop now a variable - index i counts from 1 to value of n. Sides Introduction to Computer Science 3. 21

COSC 1 P 02 The end Introduction to Computer Science 3. 23

COSC 1 P 02 The end Introduction to Computer Science 3. 23