45
Lecture 2 Calling and Defining Methods in Java

Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Embed Size (px)

Citation preview

Page 1: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Lecture 2Calling and Defining Methods in Java

Sam Squires
FOR THE AV TAS NEXT YEAR:Find "Safehouse" clip from movie Grand Hotel Budapest
Page 2: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Introduction

● Calling and defining methods

● Declaring and defining a class

● Instances of a class

● The this keyword

1 of 43

Page 3: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

● samBot is a robot who lives in a 2D grid world

● She knows how to do two things:o move forward any

number of steps

o turn right 90 degrees

Meet samBot (kudos to Sam Squires)

2 of 43

Page 4: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

● The TAs created samBot from a Robot blueprint they wrote as part of her program

● We will learn how to communicate with samBot using Java

Meet samBot

3 of 43

Page 5: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

samBot’s World

● This is samBot’s world

● samBot starts in the square at (0,0)

● She wants to get to the square at (1,1)

● Thick black lines are walls that samBot can’t pass through

0 1 2 3 4

0

1

2

4 of 43

Page 6: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Giving Instructions

● Goal: to move samBot from her starting position to her destination by giving her a list of instructions

● samBot only knows instructions “move forward n steps” and “turn right”

● What instructions to give her?

0 1 2 3 4

0

1

2

5 of 43

Page 7: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Giving Instructions

0 1 2 3 4

0

1

2

● “Move forward 4 steps.”

● “Turn right.”

● “Move forward 1 step.”

● “Turn right.”

● “Move forward 3 steps.”

Note samBot moves in the direction her outstretched arm is pointing; yes, she can move upside down in this 2D world… 6 of 43

avd
i realized it was confusing to say orientation doesn't matter; do you agree with this wording?
Sam Squires
NOTE: be careful with this animation when/if converting to pptx: 90 degree rotation seems to convert to 180
Page 8: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Giving Instructions

● Have to give samBot these instructions in a language she understands

● That’s where Java comes in!

● In Java, give instructions to an object by sending it messages

0 1 2 3 4

0

1

2

7 of 43

Page 9: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

“Calling Methods”: Sending Messages in Java

● The messages that samBot knows how to respond to are called methodso “method” is short for “method for responding to a message”

o objects cooperate by sending each other messages

● Object sending message is caller

● Object receiving message is receiver

8 of 43

Page 10: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

● samBot has one method for “move forward n steps” and another method for “turn right”

● When we send a message to samBot to “move forward” or “turn right” in Java, we are calling a method on samBot

“Calling Methods”: Sending Messages in Java

Hey samBot, turn right!

The caller The receiver (samBot)

The method call (message passed from caller to receiver)

9 of 43

Page 11: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Moving samBot forward

● When tell samBot to move forward, need to tell her how many steps to move

● samBot’s “move forward” method is named moveForward

● To call this method in Java:

samBot.moveForward(<number of steps>);

You substitute for anything in < >!

; ends Java statement

● This means that if want her to move forward 2 steps, say:samBot.moveForward(2);

10 of 43

Page 12: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Turning samBot right

● samBot’s “turn right” method is called turnRight

● To call the turnRight method on samBot:

samBot.turnRight();

● To call methods on samBot in Java, need to address her by name! Every command to samBot takes the form:

samBot.<method name>;

11 of 43

Page 13: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Calling Methods: Important Points

● Method calls in Java have parentheses after the method’s name

● If the method needs any information, include it between parentheses (e.g. samBot.moveForward(2);)

● If no extra information is needed, just leave the parentheses empty (e.g. samBot.turnRight();)

● Extra pieces of information passed to a method are called parameters. More on them next lecture

12 of 43

Page 14: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Guiding samBot in Java● Tell samBot to move forward 4 steps → samBot.moveForward(4);● Tell samBot to turn right → samBot.turnRight();● Tell samBot to move forward 1 step → samBot.moveForward(1);● Tell samBot to turn right → samBot.turnRight();● Tell samBot to move forward 3 steps → samBot.moveForward(3);

0 1 2 3 4

0

1

2

“pseudocode” Java code

13 of 43

Page 15: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Hand Simulation of This Code

samBot.moveForward(4);samBot.turnRight();samBot.moveForward(1);samBot.turnRight();samBot.moveForward(3);

0 1 2 3 4

0

1

2

14 of 43

Page 16: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Putting Code Fragment in a Real Program

public class RobotMover {

/* additional code elided */

public void moveRobot(Robot samBot) {samBot.moveForward(4);samBot.turnRight();samBot.moveForward(1);samBot.turnRight();samBot.moveForward(3);

}

}

● Let’s demonstrate this code for real

● First, need to put it inside real Java program

● Grayed-out code specifies context in which samBot executes these instructions

● It is part of the stencil code that has been written for you by the TAs

o the TAs have also given samBot the capability to respond to moveForward and turnRight − more on this later 15 of 43

avd
i changed to inital caps - style consistency!
Page 17: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Demo in our Interactive Development Environment, Eclipse

16 of 43

Page 18: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

A variation:public class RobotMover {

/* additional code elided */

public void moveRobot(Robot samBot) {// Your code goes here!// …

// …// …// …// …// …// …// …// …// …

// …// …// …

}}

0 1 2 3 4

0

1

2

17 of 43

Page 19: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

A variation:

0 1 2 3 4

0

1

2

public class RobotMover {/* additional code elided */

public void moveRobot(Robot samBot) {samBot.turnRight();

samBot.moveForward(2);samBot.turnRight();samBot.turnRight();samBot.turnRight();samBot.moveForward(3);samBot.turnRight();samBot.turnRight();samBot.turnRight();samBot.moveForward(2);samBot.turnRight();samBot.turnRight();samBot.turnRight();samBot.moveForward(2);

}} 18 of 43

Page 20: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

public class RobotMover {/* additional code elided */

public void moveRobot(Robot samBot) {samBot.turnRight();

samBot.moveForward(2);samBot.turnRight();samBot.turnRight();samBot.turnRight();samBot.moveForward(3);samBot.turnRight();samBot.turnRight();samBot.turnRight();samBot.moveForward(2);samBot.turnRight();samBot.turnRight();samBot.turnRight();samBot.moveForward(2);

}}

● Lots of code for a simple problem...

● samBot only knows how to turn right, so have to call turnRight three times to make her turn left

● If she understood how to “turn left”, would be much simpler!

● We can teach samBot to turn left by defining a method called turnLeft

● But before we start defining methods, we need to learn a bit about classes.

A variation:

19 of 43

Page 21: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

What is a class?

● A class is a blueprint for a certain type of object

● An object’s class defines its properties and capabilities (methods)

● So far, we’ve been working within the class RobotMover

● This is the code for our original problem

public class RobotMover {

/* additional code elided */

public void moveRobot(Robot samBot) {

samBot.moveForward(4);samBot.turnRight();samBot.moveForward(1);samBot.turnRight();samBot.moveForward(3);

}

}

20 of 43

Page 22: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Declaring and then Defining a Class● As with dictionary entry, first declare

term, then provide definition

● First line declares RobotMover class

● public and class are Java “reserved words”

● Breaking it down:

o public indicates that anyone can use this class

o class indicates to Java that we are about to define a new class

o RobotMover is the name that we have chosen for our class

public class RobotMover {

/* additional code elided */

public void moveRobot(Robot samBot) {

samBot.moveForward(4);samBot.turnRight();samBot.moveForward(1);samBot.turnRight();samBot.moveForward(3);

}

}

21 of 43

Page 23: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

● Classes take the form:

<visibility> class <name> {

<code that defines class>

}

Declaring and then Defining a Class

public class RobotMover {

/* additional code elided */

public void moveRobot(Robot samBot) {

samBot.moveForward(4);samBot.turnRight();samBot.moveForward(1);samBot.turnRight();samBot.moveForward(3);

}}

declaration of the RobotMover class

declaration

● Each class goes in its own file, where name of file matches name of class

● RobotMover class is contained in file “RobotMover.java”

22 of 43

Page 24: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

public class RobotMover {

/* additional code elided */

public void moveRobot(Robot samBot) {

samBot.moveForward(4);samBot.turnRight();samBot.moveForward(1);samBot.turnRight();samBot.moveForward(3);

}

}

Declaring and then Defining a Class

● class definition defines properties and capabilities of class

● Class definition (aka “body”) is contained within curly braces that follow class declaration

● A class’s capabilities are defined by its methods

● A class’s properties are defined by its instance variables - more on this next week definition of the RobotMover class

23 of 43

Page 25: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Classes and Instances

● We’ve been saying samBot is a Robot

● We’ll now refer to her as an instance of class Robot

● This means samBot is a particular Robot built using Robot class as a blueprint

● Everything samBot can do is spelled out in Robot class

● All Robots (all instances of the class Robot) have the exact same capabilities: the methods defined in the Robot class

24 of 43

Page 26: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

The Robot class is like a blueprint

Classes and Instances

25 of 43

Page 27: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

We can use the Robot class to build actual Robots - instances of the class Robot

Classes and Instances

26 of 43

Page 28: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

instance instance instance instance

Classes and Instances

27 of 43

Page 29: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

The Robot classpublic class Robot {

public void turnRight() {// code that turns robot right

}

public void moveForward(int numberOfSteps) {

// code that moves robot forward}

/* other code elided-- if you’re curious, check out Robot.java in the stencil code!*/

}

● public class Robot declares that we are about to define a class called Robot

28 of 43

Page 30: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

● Information about the properties and capabilities of Robots (the class definition) goes within these curly braces

The Robot classpublic class Robot {

public void turnRight() {// code that turns robot right

}

public void moveForward(int numberOfSteps) {

// code that moves robot forward}

/* other code elided-- if you’re curious, check out Robot.java in the stencil code!*/

}29 of 43

Page 31: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Methods of the Robot class● public void turnRight()

and public void moveForward(int numberOfSteps) each declare a method

● Since moveForward needs to know how many steps to move, we put int numberOfSteps within the parentheses

● This is called a parameter - more on this next lecture; int is Java’s way of saying “integer”

public class Robot {

public void turnRight() {// code that turns robot right

}

public void moveForward(int numberOfSteps) {

// code that moves robot forward}

/* other code elided-- if you’re curious, check out Robot.java in the stencil code!*/

}30 of 43

Page 32: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

● Almost all methods take on this general form:

<visibility> <type> <name>(<parameters>) {<list of statements within method>

}

public class Robot {

public void turnRight() {// code that turns robot right

}

public void moveForward(int numberOfSteps) {

// code that moves robot forward}

/* other code elided-- if you’re curious, check out Robot.java in the stencil code!*/

}

Methods of the Robot class

31 of 43

Page 33: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

● When we call turnRight or moveForward, all code between method’s curly braces is executed

public class Robot {

public void turnRight() {// code that turns robot right

}

public void moveForward(int numberOfSteps) {

// code that moves robot forward}

/* other code elided-- if you’re curious, check out Robot.java in the stencil code!*/

}

Methods of the Robot class

32 of 43

Page 34: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Defining our own method

● Let’s add a new method: turnLeft

● To make a Robot turn left, tell her to turn right three times

public class Robot {

public void turnRight() {// code that turns robot right

}

public void moveForward(int numberOfSteps) {

// code that moves robot forward}

public void turnLeft() {// our code goes here!

}}

33 of 43

Page 35: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

The this keyword

● When working with RobotMover, we were talking to samBot, an instance of class Robot

● To tell her to turn right, we said “samBot.turnRight();”

● Why do we now write “this.turnRight();”?

public class Robot {

public void turnRight() {// code that turns robot right

}

public void moveForward(int numberOfSteps) {

// code that moves robot forward}

public void turnLeft() {this.turnRight();this.turnRight();this.turnRight();

}} 34 of 43

Page 36: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

● The this keyword is how an instance (like samBot) can call a method on itself

● Use this to call a method of Robot class from within another method of Robot class

● When samBot is told by, say, RobotMover to turnLeft, she responds by telling herself to turnRight three times

● this.turnRight(); means “hey me, turn right!”

public class Robot {

public void turnRight() {// code that turns robot right

}

public void moveForward(int numberOfSteps) {

// code that moves robot forward}

public void turnLeft() {this.turnRight();this.turnRight();this.turnRight();

}}

The this keyword

35 of 43

Sam Squires
DEAR FUTURE TAS,MAKE SURE ANDY DOES THE FOXTROT WHEN HE PRESENTS THIS SLIDE.
Page 37: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

We’re done!

● We just defined our first method!

● Now that the Robot class has the method turnLeft, we can call turnLeft on any instance of Robot

public class Robot {

public void turnRight() {// code that turns robot right

}

public void moveForward(int numberOfSteps) {

// code that moves robot forward}

public void turnLeft() {this.turnRight();this.turnRight();this.turnRight();

}} 36 of 43

Page 38: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Simplifying our code using turnLeftpublic class RobotMover {

public void moveRobot(Robot samBot) {

samBot.turnRight();samBot.moveForward(2);samBot.turnRight();samBot.turnRight();samBot.turnRight();samBot.moveForward(3);samBot.turnRight();samBot.turnRight();samBot.turnRight();samBot.moveForward(2);samBot.turnRight();samBot.turnRight();samBot.turnRight();samBot.moveForward(2);

}}

public class RobotMover {public void moveRobot(Robot samBot)

{samBot.turnRight();

samBot.moveForward(2);samBot.turnLeft();samBot.moveForward(3);samBot.turnLeft();samBot.moveForward(2);samBot.turnLeft();samBot.moveForward(2);

}}

We’ve saved a lot of lines of code by using turnLeft!

37 of 43

Page 39: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

turnAround public class Robot {

public void turnRight() {// code that turns robot right

}

public void moveForward(int numberOfSteps) {

// code that moves robot forward}

public void turnLeft() {this.turnRight();this.turnRight();this.turnRight();

}

// your code goes here!// …// …// …

}

● Let’s define a method that turns the Robot around 180 degrees

● See if you can declare and define the method turnAround

38 of 43

Page 40: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

turnAround public class Robot {

public void turnRight() {// code that turns robot right

}

public void moveForward(int numberOfSteps) {

// code that moves robot forward}

public void turnLeft() {this.turnRight();this.turnRight();this.turnRight();

}

public void turnAround() {this.turnRight();this.turnRight();

}}

● Now that the Robot class has the method turnAround, we can call the method on any Robot

● There are other ways of implementing this method that are just as correct

39 of 43

Page 41: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

turnAround ● Instead of calling turnRight,

we could call our newly created method, turnLeft

● Both of these solutions are equally correct, in that they will turn the robot around 180 degrees

● How do they differ? When we try each of these implementations with samBot, what will we see in each case?

public class Robot {public void turnRight() {

// code that turns robot right}

public void moveForward(int numberOfSteps) {

// code that moves robot forward}

public void turnLeft() {this.turnRight();this.turnRight();this.turnRight();

}

public void turnAround() {this.turnLeft();this.turnLeft();

}}

40 of 43

Page 42: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

DEMO

41 of 43

Page 43: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

That’s it!

Important concepts:

● Calling methodso an object sends a message to another object

o example: samBot.turnRight();

● Defining methodso how we describe a capability of a class

o example: public void turnLeft() { … }

42 of 43

Page 44: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

That’s it!

● Classes and instanceso a class is a blueprint for a certain type of object

o an instance of a class is a particular member of that class

o example: samBot is an instance of Robot

● The this keywordo how an instance calls a method on itself

o example: this.turnRight()

Important concepts:

43 of 43

Page 45: Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The

Announcements• Labs start today at 5PM in the Sunlab. Bring signed

collaboration contract and a computer to set up remote access. You must attend the session for which you signed up every week.

• TA Hours start today at 4:30PM in the Fishbowl (2nd floor of CIT).

• HW1 and Andybot assignments go out Thursday. HW1 is due at 1PM on Sunday, and Andybot is due by 11:59PM on Wednesday, September 17th.