Announcements - PowerPoint PPT Presentation

About This Presentation
Title:

Announcements

Description:

Announcements & Review Last Time: methods logical reuseable parameterizable Announcements Lab 2 Due Thursday 10pm 1 file per pair Today More on methods in a class ... – PowerPoint PPT presentation

Number of Views:154
Avg rating:3.0/5.0
Slides: 14
Provided by: CSCF152
Category:

less

Transcript and Presenter's Notes

Title: Announcements


1
Announcements Review
  • Last Time
  • methods
  • logical
  • reuseable
  • parameterizable
  • Announcements
  • Lab 2 Due
  • Thursday 10pm
  • 1 file per pair

2
Today
  • More on methods in a class
  • Terminology
  • method song calls method chorus
  • song is the caller, chorus is the callee
  • Formal parameters
  • Actual parameters
  • Call-by-value
  • Call-by-reference

3
Formal Parametersin the callee method
  • // formal parameters width, length, s
  • // the variable parameter names in the
    callee
  • public void printBox(int width, int length,
    String s)
  • Shapes myshape new Shapes()
  • for (int i 0 i lt 3 i)
  • myshape.printBox(10, i, )

4
Actual Parametersin the caller method
  • public void printBox(int width, int length,
    String s)
  • Shapes myshape new Shapes()
  • for (int i 0 i lt 3 i)
  • // actual parameters 10, i,
  • // the parameters in the caller
  • myshape.printBox(10, i, )

5
Does this make sense?
  • public void printBox(int width, int length,
    String s)
  • width 9
  • ...
  • Shapes myshape new Shapes()
  • for (int i 0 i lt 3 i)
  • myshape.printBox(10, i, )

6
BlueJ Examples
  • Try out changing a parameter

7
Call-by-ValueJava Semantics
  • Method cannot make externally visible changes to
    parameters
  • Can change it internally, but generally this is a
    bad bad idea. -)

8
Call-by-ReferenceSemantics
  • Method can make externally visible changes to
    parameters!
  • Algol 60 - declare a parameter as
    call-by-reference
  • What does this feature mean for actual parameters?

9
Call-by-ReferenceSemantics
  • Method can make externally visible changes to
    parameters!
  • Algol 60 - declare a parameter as
    call-by-reference
  • What does this feature mean for actual
    parameters?
  • method calls may change them

10
Getting aroundCall-by-Value
  • Method cannot make externally visible changes to
    parameters
  • How do we change state with methods then?

11
Getting aroundCall-by-Value
  • Method cannot make externally visible changes to
    parameters
  • for primitive types objects
  • How do we change state with methods then?
  • return values, but Java only has one!
  • methods can make persistent changes to the fields
    of a parameter object
  • object.color pink

12
BlueJ Examples
  • Lets try a return value, and passing in an
    object as a parameter

13
More Questions?
Write a Comment
User Comments (0)
About PowerShow.com