Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing Methods Mrs. C. Furman October 9, 2008. Drawing a Square World worldObj = new World(); Turtle turtle1 = new Turtle(100, 100, worldObj); turtle1.forward.

Similar presentations


Presentation on theme: "Writing Methods Mrs. C. Furman October 9, 2008. Drawing a Square World worldObj = new World(); Turtle turtle1 = new Turtle(100, 100, worldObj); turtle1.forward."— Presentation transcript:

1 Writing Methods Mrs. C. Furman October 9, 2008

2 Drawing a Square World worldObj = new World(); Turtle turtle1 = new Turtle(100, 100, worldObj); turtle1.forward (30); turtle1.turnRight(); turtle1.forward (30); turtle1.turnRight(); turtle1.forward (30); turtle1.turnRight(); turtle1.forward (30); turtle1.turnRight();

3 World worldObj = new World(); Turtle turtle1 = new Turtle(100, 100, worldObj); int sideLength = 30; turtle1.forward (sideLength); turtle1.turnRight(); turtle1.forward (sideLength); turtle1.turnRight(); turtle1.forward (sideLength); turtle1.turnRight(); turtle1.forward (sideLength); turtle1.turnRight();

4 void drawSquare(int sideLength) { this.forward (sideLength); this.turnRight(); this.forward (sideLength); this.turnRight(); this.forward (sideLength); this.turnRight(); this.forward (sideLength); this.turnRight(); }

5 this operator We needed to change turtle1 to this, because turtle1 is a specific Turtle object, which is declared in the client class (in this case, the interactions pane). this – a keyword that refers to the calling object. So, turtle1.drawSquare(30); --turtle1 is the calling object, so this gets replaced with turtle1. turtle2.drawSquare (50); --turtle2 is the calling object, so this gets replaced with turtle2.

6 Scope of a Variable turtle1 is not defined in the drawSquare() method. Variable names are known in a context (area that they apply). Variables declared in the interactions pane are only known in the interactions pane. Variables declared inside methods are only known in the method.

7 Object Methods methods that work on a particular object. they are implicitly passed a reference to the object the method was invoked on. In this case, when we call drawSquare, we have sideLength sent explicitly (we see it right there in the parameter list), but turtle1 is sent implicitly and can be reference by using this.

8 Method Overloading Create a drawSquare method that does not take any parameters, and only draws squares with side lengths of 100.

9 Method OverLoading Cont Now we have: public void drawSquare(int sideLength) and public void drawSquare () How will the compiler know which method to use?

10 Abstraction The more general we can make a method, the more it can be used in multiple cases. Which one of our methods is more general? We call this generalization, abstraction! Abstraction leads to general solutions that work in lots of situations!

11 Pass by Value turtle1.drawSquare(20); actual parameter input value argument Each formal parameter (ex: int sideLength), is set to a copy of the actual parameter. This is called pass by value.

12 Write a drawRectangle method The method should have parameters for length and width.


Download ppt "Writing Methods Mrs. C. Furman October 9, 2008. Drawing a Square World worldObj = new World(); Turtle turtle1 = new Turtle(100, 100, worldObj); turtle1.forward."

Similar presentations


Ads by Google