cmsc 150 methods and classes n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CMSC 150 Methods and Classes PowerPoint Presentation
Download Presentation
CMSC 150 Methods and Classes

Loading in 2 Seconds...

play fullscreen
1 / 32

CMSC 150 Methods and Classes - PowerPoint PPT Presentation


  • 67 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CMSC 150 Methods and Classes' - maine


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cmsc 150 methods and classes

CMSC 150Methods and Classes

CS 150: Wed 25 Jan 2012

remember the length method
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
why write a method
Why write a method?
  • 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.
why write a method1
Why write a method?
  • 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

writing methods
Writing methods

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

statements;

return <expression>; // Details later

}

writing methods1
Writing methods

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

statements;

return <expression>; // Details later

}

What “kind” of

Method this is –

More on this later.

writing methods2
Writing methods

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

statements;

return <expression>; // Details later

}

Does this method produce

a value, and if so, what

type?

writing methods3
Writing methods

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

statements;

return <expression>; // Details later

}

What name is used to “invoke”

this method?

writing methods4
Writing methods

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

writing methods5
Writing methods

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

statements;

return <expression>; // Details later

}

The first line of a method is called

the “method header” or “method signature”

writing methods6
Writing methods

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

statements;

return <expression>; // Details later

}

What’s inside is called

the “method body”

writing methods7
Writing methods

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

statements;

return <expression>; // Details later

}

Statements executed when

this method is called.

writing methods8
Writing methods

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.

example back to funbrain1
Example: Back to FunBrain…

Hmm… looks suspiciously

like a method…

example back to funbrain2
Example: Back to FunBrain…

Check the guess against

the secret number and

generate a message to

the user. Let’s make it

into a method.

parameter passing
Parameter passing

Formal parameter list

parameter passing1
Parameter passing

Actual parameter list

parameter passing2
Parameter passing

Can be any expression

that evaluates to the

type of the matching

formal parameter.

what happens in memory
What happens in memory?

25

userGuess

… = checkGuess( userGuess,

randomNumber,

guesses );

47

randomNumber

2

guesses

Memory used for

variables in main()

what happens in memory1
What happens in memory?

25

userGuess

… = checkGuess( userGuess,

randomNumber,

guesses );

public static String

checkGuess ( intcurrentGuess,

int secret,

intnumGuesses ){

47

randomNumber

2

guesses

Memory used for

formal parameters

in checkGuess

25

currentGuess

secret

47

numGuesses

2

what happens in memory2
What happens in memory?

25

userGuess

… = checkGuess( userGuess,

randomNumber,

guesses );

public static String

checkGuess ( intcurrentGuess,

int secret,

intnumGuesses ){

47

randomNumber

2

guesses

When method call is made,

values stored in the arguments…

…are copied into the

formal parameters

25

currentGuess

secret

47

numGuesses

2

what happens in memory3
What happens in memory?

25

userGuess

… = checkGuess( userGuess,

randomNumber,

guesses );

public static String

checkGuess ( intcurrentGuess,

int secret,

intnumGuesses ){

47

randomNumber

2

guesses

Arguments & formal parameters

initially have the same values…

but occupy different spaces

in memory

25

currentGuess

secret

47

numGuesses

2

method facts
Method facts
  • 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”)
classes in java
Classes in Java
  • 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
example string
Example: String
  • 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(intpos)
    • indexOf(char c)
    • substring(int begin, intpastEnd)
    • … it goes on and on …
example string1
Example: String
  • 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(intpos)
    • indexOf(char c)
    • substring(int begin, intpastEnd)
    • … it goes on and on …
methods with different jobs
Methods with different jobs
  • 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
in order to use a class
In order to use a class…
  • … 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
lab setup
Lab setup
  • 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
your methods
Your methods
  • public void setupGame()
    • Sets up the initial game configuration (similar to Wumpus)
  • public booleanhandleMove( 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 booleancheckForLoss()
    • Determine if the Ghostbuster encountered Slimer or a portal
  • public void checkForGhostTrap()
    • Determine if the Ghostbuster found a desirable ghost trap