Professional Documents
Culture Documents
Method execution;
Method implementation principles – SRP,
DRY
Last class
◦ Storing state – data members
◦ Constructing an object
◦ Constructors
Upcoming Schedule
◦ SI meeting this week
◦ Lab 3 Due Friday August 30
◦ Lab 4 Assigned today, Due Wednesday
September 4 at 10:00PM
◦ Project 1 Assigned Wed Sept 4, Due Wed Sept 11
◦ Exam 1 Monday September 9
◦ Bring a pencil.
Method declaration
Method specification
◦ A comment header that tells what the method
does.
◦ Example from Turtle::turnLeft90 method.
Method header
◦ Specifies method accessibility modifier, name, method
parameters, and method return type.
◦ Examples:
◦ public void raiseTail()
◦ public boolean isTailDown()
◦ public void setDirection(double direction)
◦ public void setLocation(int x, int y)
◦ private void drawBorder()
Method body
◦ The code within the {}’s that specifies what the method does.
◦ Example DoodleTurtle::raiseTail method.
Purpose of methods
◦ Code that defines the behavior that an object of a class
needs to have.
◦ Implement the steps of an algorithm.
◦ Benefit
◦ Allow same behavior to be called over and over again
without having to rewrite the same code multiple times.
Order of execution within a
method
◦ When a method is called, it begins by
executing the first line of the method and
continues sequentially (line-by-line) until
reach the last line of the method at which
point it returns from the method.
◦ Note: There are constructs to alter the line-by-line
execution order within a method that we will learn
later in this course.
Order of execution within a
method example
public void moveInBox() {
1. this.move();
2. this.move();
3. this.turnLeft();
4. this.move()
5. this.move();
6. this.turnLeft();
7. this.move()
8. this.move();
9. this.turnLeft();
10. this.move();
11. this.move();
}
Calling other methods
◦ A method call calls another method.
◦ A method call will transfer control to the called
(callee) method.
◦ When the called method finishes execution it will
return control to the calling (caller) method at
the point the method call was made.
What is the execution sequence
given an initial call to
drawFigure?
public class MethodCalls {
◦ DRY principle
◦ Lots of redundant code in the methods
that draws a box.
◦ When you have redundant code add a
method to replace the redundant code.