1 / 17

Week 3 Lecture slides

Cosc 1P02. Week 3 Lecture slides. Birthdays are good for you. Statistics show that the people who have the most live the longest. (Rev. Larry Lorenzoni). Review. Memory Representation of: Value variables Reference variables Assignment of reference variables Tiling patterns.

Download Presentation

Week 3 Lecture slides

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the most live the longest. (Rev. Larry Lorenzoni)

  2. Review • Memory Representation of: • Value variables • Reference variables • Assignment of reference variables • Tiling patterns

  3. Dealing with Complexity • Limits on composition • Can only go so deep before the nesting becomes un-manageable. • 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.

  4. 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 (drawSquare) • Example

  5. Methods • Method declaration • syntax • name • verb • body • Method call • syntax • Method execution • current code suspended • body of method executed • current code continues

  6. Eight Squares RevisitedeightSquares2.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 EightSquares simpler • don’t have to worry about different index variables

  7. 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

  8. 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

  9. 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.

  10. Constructors, Methods & Objects • Methods are executed by an object • who is executing drawSquare? • 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 EightSquares 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 EightSquares object) • drawSquare() is shorthand for this.drawSquare()

  11. 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 EightSquares object created in main • E.g. new EightSquares3().draw(); • New EightSquares3() creates an object returning a reference • .draw then calls the behavior of the new object.

  12. 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

  13. 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

  14. 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

  15. Parameters • Allows a method to be generalized over one or more values • radius for drawHexagon • 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

  16. 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 nSides

  17. The end

More Related