Methods - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Methods

Description:

So far our methods (or member functions) have done very little we really don't ... One thing we would lie to do is to pass information into and out of a method. ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 19
Provided by: geraldh6
Category:
Tags: methods | thewe

less

Transcript and Presenter's Notes

Title: Methods


1
Methods Simple Control Structures
2
Parameters
  • So far our methods (or member functions) have
    done very little we really don't know how to
    make them do much (yet)
  • One thing we would lie to do is to pass
    information into and out of a method.

3
Passing information to a Method
  • The parenthesis behind a function name can
    contain "formal parameters". A formal parameter
    is a sort of placeholder or local name for a
    value being passed in.
  • The type of the formal parameter must be
    specified and the item passed must be of the
    specified type. (We like to know what we are
    getting!)

4
Accessor and Mutator Methods(also known as
get/set methods)
  • We often want to keep some information private to
    an object. So, we declare it as private meaning
    only the object itself can use the information
    directly.
  • This means that we need to be able to initialize
    or set the value of the information and often to
    check on the value. This is what
    accessor/mutator methods are used for.

5
Another Sample Class
  • / _at_author Jerry Heuring A sample class
    with accessor/mutator routines. The class
    represents a simple mailing address.
    /public class MailingAddress private String
    street private String city private String
    state private String postalCode// An mutator
    function for street public void setStreet (
    String s ) street s

6
  • / A simple routine to print out the
    properties in this mailing address/public
    void printInfo( ) System.out.println(street)
    System.out.println(city) System.out.println(stat
    e) System.out.println(postalCode)
  • / The main program will try to use an
    instance of the class. We will expand main as
    we add more methods./public static void main
    (String arguments) MailingAddress
    address1 address1 new MailingAddress() addr
    ess1.setAddress("2801 West Bancroft") address1.p
    rintInfo() System.exit(0)

7
Notes
  • If we had tried to change the street
    directly address1.street "2801 West
    Bancroft"in the main it would not have allowed
    us to do so (because it is private).
  • We could allow all of the properties to have set
    routines. (actually we will eventually)

8
Returning a Value From a Method
  • You can return a value from a method as well. Up
    to now our methods have had a return type of
    "void" meaning they return nothing.
  • You can return 1 (one) item using a return
    statement in a method. The value is returned to
    the calling method and object.
  • Let's set up an accessor (get) for street

9
A Simple Accessor Function
  • / function to return the current value for
    street. _at_return the current value for
    street/public String getStreet( ) return
    street

10
Notes
  • By adding the get method the main program can now
    get the street value to use it. For
    example System.out.println(address1.street)
  • Why use Accessor/Mutator methods?
  • They are useful to hide details from the user.
    Also, if you need to make sure that something is
    within range it is nice to go through a mutator
    method.

11
A Completed Class
  • What follows is a completed class for the
    MailingAddress object. It contains accessor and
    mutator routines for all private properties or
    instance variables.
  • It looks long and complicated it isn't
    complicated!
  • all set and get routines will follow the same
    pattern in this case only the property or
    instance variable will change.

12
Finished MailingAddress
  • / _at_author Jerry Heuring Created Aug 17,
    2004 _at_version 0.0 Bugs Revisions
    Description This version of
    MailingAddress has the accessor/mutator
    functions (get/set functions) for all the private
    instance variables (or properties)./

13
  • public class MailingAddress private String
    streetprivate String stateprivate String
    cityprivate String postalCode/ Mutator
    routine to set the street property _at_param s
    value to use to set street /public void
    setStreet( String s) street s /
    Accessor routine to get the street property
    _at_return a string representing the street
    /public String getStreet ( ) return street

14
  • / Mutator routine to set the state
    property _at_param s value used to set the
    state Note The routine might want to
    check if it is a valid state name or
    abbreviation but we don't have the
    statements to do that yet. /public void
    setState( String s) state s
  • / Accessor method to get the state
    property _at_return a string representing the
    state /public String getState( ) return
    state

15
  • / Mutator method to set the city property
    _at_param c value that this city property is set
    to. /public void setCity(String c ) city
    c / Accessor method to get the city
    property _at_return a string representing the
    city /public String getCity( ) return
    city

16
  • / Mutator method to set the postalCode
    property _at_param pc the postal code value that
    this postalCode will be set to.
    /public void setPostalCode(String
    pc) postalCode pc/ Accessor method
    to get the postalCode property _at_return a
    string representing a postalCode for this
    object. /public String getPostalCode( )
    return postalCode

17
  • / Simple routine to print out all of this
    object's properties to System.out so that we
    can see them./public void printInfo()
    System.out.println(street) System.out.println
    (city) System.out.println(state) System.out.pr
    intln(postalCode) / The main program
    instantiates (creates) an instance of mailing
    address and then uses each of the methods to
    test whether or not they work. _at_param
    arguments the command line arguments -- unused.
    /public static void main(String arguments)
    MailingAddress address1 address1 new
    MailingAddress() address1.setStreet("2801
    West Bancroft") address1.printInfo( )

18
  • address1.setCity("Toledo") address1.setState("O
    hio") address1.setPostalCode("43606") address1
    .printInfo() System.out.println(address1.getSt
    reet() ) System.out.println(address1.getCity()
    ", " address1.getState(
    )) System.out.println(" "
    address1.getPostalCode( ))
  • System.exit(0)
Write a Comment
User Comments (0)
About PowerShow.com