You are on page 1of 19

CS 1301 – COMPUTER SCIENCE I

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 {

public void drawFigure() {


this.myTurtle.lowerTail();
this.moveThree();
this.turnLeftAndMoveFive(); public void turnLeftAndMoveFive() {
this.moveThree(); this.myTurtle.turnLeft();
} this.myTurtle.stepFoward();
this.moveThree();
public void moveThree() { this.myTurtle.stepFoward();
this.myTurtle.stepForward(); }
this.myTurtle.stepForward();
this.myTurtle.stepForward();
}
}
Order of exection
public class MethodCalls {

public void drawFigure() {


1.this.myTurtle.lowerTail();
2.this.moveThree();
this.turnLeftAndMoveFive(); private void turnLeftAndMoveFive() {
6.
this.moveThree(); 7. this.myTurtle.turnLeft();
14.
} 8. this.myTurtle.stepFoward();
9. this.moveThree();
private void moveThree() { 13. this.myTurtle.stepFoward();
this.myTurtle.stepForward(); }
3., 10., 15.this.myTurtle.stepForward();
4., 11., 16.this.myTurtle.stepForward();
5., 12., 17.
}
}
Principles in writing methods
◦ Single Responsibility principle
◦ Helper methods (private)
◦ Don’t Repeat Yourself
Single Responsibility Principle
◦ SRP
◦ Each method should only implement one
behavior.
◦ If a method does multiple things, it should put
each responsibility in its own method and call
the methods to help it do its job.
Helper methods
◦ So far the methods we have seen have had a
public access (visibility) modifier.
◦ public methods are considered to be part of the
interface of the class.
◦ Sometimes, you'll want to create a method that
should not be part of the interface; instead it just
supports the operation of other methods within its
own class.
◦ Those methods are called helper methods and
they should use the private access modifier.
◦ private helper methods
16
Private helper methods
◦ A method that helps another method do its
job.
◦ The method is not “seen” (allowed to be
called) via an object instance, but can be
called within the class.
◦ Example
◦ Creating a private helper method for the
code draw in
ScribbleController::draw method.
In-class exercise
◦ Introducing private helper methods for
the ScribbleController::draw
method.
◦ One for drawing the oblong plus
◦ One for drawing the rectangle
Don’t Repeat Yourself

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

You might also like