CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

32
CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012

description

Why write a method? 3  Code can get very long and unwieldy  Methods let us break up code into logical chunks  Readability – with appropriately named methods, your code is easier to read and follow  Reuse – make repeated code into a method. Write once, use many times.

Transcript of CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Page 1: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

CMSC 150METHODS AND

CLASSES

CS 150: Wed 25 Jan 2012

Page 2: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Remember the length “method”?

String message = “Hello, Watson”; System.out.println( message.length() ); // prints 13

Method – useful code packaged with a name In Java, methods belong to classes

With an object like message, use the object variable to “call” the method works on information inside the object message

refers to

2

Page 3: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Why write a method?3

Code can get very long and unwieldy

Methods let us break up code into logical chunks

Readability – with appropriately named methods, your code is easier to read and follow

Reuse – make repeated code into a method. Write once, use many times.

Page 4: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Why write a method?4

Code can get very long and unwieldy

Methods let us break up code into logical chunks

Readability – with appropriately named methods, your code is easier to read and follow

Reuse – make repeated code into a method. Write once, use many times.

MODULARITY

Page 5: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Writing methods5

public static <return-type> method-name ( parameters ) { statements;

return <expression>; // Details later}

Page 6: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Writing methods6

public static <return-type> method-name ( parameters ) { statements;

return <expression>; // Details later}

What “kind” of Method this is – More on this later.

Page 7: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Writing methods7

public static <return-type> method-name ( parameters ) { statements;

return <expression>; // Details later}

Does this method producea value, and if so, what type?

Page 8: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Writing methods8

public static <return-type> method-name ( parameters ) { statements;

return <expression>; // Details later}

What name is used to “invoke”this method?

Page 9: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Writing methods9

public static <return-type> method-name ( parameters ) { statements;

return <expression>; // Details later}

What information is expected from the caller in order to run this method? (Can be empty.)

Page 10: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Writing methods10

public static <return-type> method-name ( parameters ) { statements;

return <expression>; // Details later}

The first line of a method is calledthe “method header” or “method signature”

Page 11: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Writing methods11

public static <return-type> method-name ( parameters ) { statements;

return <expression>; // Details later}

What’s inside is called the “method body”

Page 12: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Writing methods12

public static <return-type> method-name ( parameters ) { statements;

return <expression>; // Details later}

Statements executed when this method is called.

Page 13: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Writing methods13

public static <return-type> method-name ( parameters ) { statements;

return <expression>; // Details later}

If the method “returns a value,”this is how we make that happen.

Page 14: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Example: Back to FunBrain…

14

Page 15: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Example: Back to FunBrain…

15

Hmm… looks suspiciouslylike a method…

Page 16: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Example: Back to FunBrain…

16

Check the guess againstthe secret number and generate a message tothe user. Let’s make itinto a method.

Page 17: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Parameter passing17

Formal parameter list

Page 18: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Parameter passing18

Actual parameter list

Page 19: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Parameter passing19

Can be any expressionthat evaluates to thetype of the matchingformal parameter.

Page 20: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

What happens in memory?20

… = checkGuess( userGuess, randomNumber, guesses );

userGuess

randomNumber

guesses

25

47

2

Memory used forvariables in

main()

Page 21: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

What happens in memory?21

… = checkGuess( userGuess, randomNumber, guesses );

public static String checkGuess ( int currentGuess, int secret, int numGuesses ){ …

userGuess

randomNumber

guesses

25

47

2

currentGuess

secret

numGuesses

25

47

2

Memory used for formal

parametersin checkGuess

Page 22: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

What happens in memory?22

… = checkGuess( userGuess, randomNumber, guesses );

public static String checkGuess ( int currentGuess, int secret, int numGuesses ){ …

userGuess

randomNumber

guesses

25

47

2

currentGuess

secret

numGuesses

25

47

2

When method call is made,values stored in the

arguments…

…are copied into the formal parameters

Page 23: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

What happens in memory?23

… = checkGuess( userGuess, randomNumber, guesses );

public static String checkGuess ( int currentGuess, int secret, int numGuesses ){ …

userGuess

randomNumber

guesses

25

47

2

currentGuess

secret

numGuesses

25

47

2

Arguments & formal parameters

initially have the same values…

but occupy different spacesin memory

Page 24: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Method facts24

In Java, parameters are “passed by value” (AKA “pass by copy”)

Each call to a method executes in its own memory space

Function: a method that creates and “returns” a value

Procedure: a method that only does work and returns no value (i.e., return type “void”)

Page 25: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Classes in Java25

Classes contain two things: methods (also called “behavior”) data (also called “state”)

Encapsulate related data & methods A good way of structuring large programs

e.g,. the Java libraries String class Random class Scanner class

Page 26: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Example: String26

What’s the data for a String object? The sequence of characters it was initialized with The length of the sequence

What behavior is available? length() charAt(int pos) indexOf(char c) substring(int begin, int pastEnd) … it goes on and on …

Page 27: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Example: String27

What’s the data for a String object? The sequence of characters it was initialized with The length of the sequence

What behavior is available? length() charAt(int pos) indexOf(char c) substring(int begin, int pastEnd) … it goes on and on …

Page 28: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Methods with different jobs28

Some methods let you request information from the class “Accessor” methods names often start with “get” (but not always) e.g., charAt method in String class

Some methods cause a change to the state (i.e., data) held by the class “Mutator” methods names may start with “set” (but not always) e.g., setSeed method in Random class

Page 29: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

In order to use a class…29

… need to know what methods it provides

… need to know what parameters they expect

… need to know how the methods behave

… don’t need to know how they were written

Application Programming Interface (API)

Again, need a variable of that class type Use that variable to call methods in the

class

Page 30: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Next lab, who you gonna call?

30

Page 31: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Lab setup31

What we will give you: GameBoard Class Room Class Ghostbusters skeleton API documentation for GameBoard and Room

What you will write: Several methods in Ghostbusters needed by

GameBoard to run the game Your methods will use methods from GameBoard

and Room

Page 32: CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.

Your methods32

public void setupGame() Sets up the initial game configuration (similar to Wumpus)

public boolean handleMove( String direction ) Logic to control Ghostbuster moving from room to room

public void handleFire( String direction ) Logic to control Ghostbuster firing proton pack @ Slimer

public boolean checkForLoss() Determine if the Ghostbuster encountered Slimer or a portal

public void checkForGhostTrap() Determine if the Ghostbuster found a desirable ghost trap