COMP 14 Introduction to Programming - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

COMP 14 Introduction to Programming

Description:

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL. Jason Jerald. COMP 14 ... I suggest to turn in today even if you are not 100% done. Assignment 5. Questions? ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 29
Provided by: jasonj3
Category:

less

Transcript and Presenter's Notes

Title: COMP 14 Introduction to Programming


1
COMP 14Introduction to Programming
  • User-Defined Methods
  • Wednesday, July 12, 2006

2
Announcements
  • Assignment 4
  • 20 deduction per day
  • I suggest to turn in today even if you are not
    100 done
  • Assignment 5
  • Questions?

3
Today
  • Scope
  • Reference variables as parameters
  • Overloading Methods
  • Constructors
  • Unified Modeling Language Class Diagrams

4
Scope
  • The scope of data is
  • The area in a program in which that data can be
    used (referenced)
  • Key to determining scope
  • Look for blocks of code (surrounded by )
  • Variables declared inside cannot be used
    outside
  • Variables declared outside can be used inside

5
Scope
  • Data declared at the class level
  • Can be used by all methods in that class
  • Data declared within a method
  • Can be used only in that method
  • Also called local data
  • Local identifier
  • An identifier that is declared within a method or
    block and that is visible only within that method
    or block.
  • Multiple blocks
  • A methods definition can contain several blocks
  • The body of a loop or an if statement also forms
    a block

6
ScopeExample
public class Rectangle // variables declared
here are class-level // available in all methods
in Rectangle class public int computeArea()
// variables declared here are
method-level // only available in
computeArea() public void print() //
variables declared here are method-level //
only available in print()
7
Scope
  • Within a method, an identifier that is used to
    name a variable in the outer block of the method
    cannot be used to name any other variable in an
    inner block of the method.
  • For example, in the following method definition,
    the second declaration of the variable x is
    illegal
  • public static void illegalIdentifierDeclaration()
  • int x
  • //block
  • double x //illegal declaration,
  • //x is already declared
  • ...

8
Static identifiers
  • Suppose X is an identifier that is declared
    within a class and outside of every methods
    definition (block).
  • If X is declared without the reserved word static
    (such as a named constant or a method name), then
    it cannot be accessed in a static method.
  • If X is declared with the reserved word static
    (such as a named constant or a method name), then
    it can be accessed within a method (block)
    provided the method (block) does not have any
    other identifier named X.

9
Parameters
  • Formal parameters
  • variable declarations in the method header
  • automatic local variables for the method
  • Actual parameters
  • actual values that are passed to the method
  • can be variables, literals, or expressions

printStars (35) printStars (30 5) int num
35 printStars (num)
10
Primitive Data Type Variables as Parameters
  • A formal parameter receives a copy of its
    corresponding actual parameter.
  • If a formal parameter is a variable of a
    primitive data type
  • Value of actual parameter is directly stored.
  • Cannot pass information outside the method.
  • Provides only a one-way link between actual
    parameters and formal parameters.

11
Primitive Data Type Parameters
  • PrimitiveDataTypeParameters.java

12
Reference Variables as Parameters
  • If a formal parameter is a reference variable
  • Copies value of corresponding actual parameter.
  • Value of actual parameter is address of the
    object where actual data is stored.
  • Both formal and actual parameters refer to same
    object.

13
Reference Variable Parameter
  • Example7_8.java

14
Uses of Reference Variables as Parameters
  • Can return more than one value from a method.
  • Can change the value of the actual object.
  • When passing an address, saves memory space and
    time, relative to copying large amount of data.

15
Caution!
  • Some objects cannot be changed by a method
  • Wrapper classes (e.g., Integer, Double)
  • Strings
  • Why not?
  • Some objects are immutable (values can not change)

16
Reference Variables as Parameters type String
17
Immutable Reference Variables as Parameters
  • StringObjectsAsParameters.java

18
So what can we do?
  • Use mutable classes
  • StringBuffer
  • Mutable wrapper classes provided by book
  • IntClass
  • DoubleClass

19
Overloading Methods
  • Overloading
  • the process of using the same method name for
    multiple methods
  • The signature of each overloaded method must be
    unique
  • At least one of these must be different
  • number of parameters
  • type of the parameters
  • NOT
  • the return type of the method
  • The compiler determines which version of the
    method is being invoked by analyzing the
    parameters

20
Overloading Methods
Invocation result tryMe (25, 4.32)
21
Overloaded Methodsprintln Example
  • The println method is overloaded
  • println (String s)
  • println (int i)
  • println (double d)
  • and so on...
  • The following lines invoke different versions of
    the println method
  • System.out.println ("The total is")
  • System.out.println (total)

22
Constructors
  • Constructor
  • A method of a class that is automatically
    executed when an object of the class is created
  • Two types of constructors
  • With parameters
  • Without parameters (default constructor)

23
Constructors
public class Rectangle private int
length private int width
Rectangle r2 new Rectangle (5, 10)
public Rectangle () length 0
width 0
public Rectangle (int l, int w) length
l width w
24
Constructor Properties
  • Constructors have the following properties
  • The name of a constructor is the same as the name
    of the class
  • A constructor, even though it is a method, has no
    type
  • A class can have more than one constructor
  • If a class has more than one constructor, any two
    constructors must have different signatures
  • If there are multiple constructors, which
    constructor executes depends on the type of
    values passed to the class object when the class
    object is instantiated

25
Creating and Using Objects
r
  • Create an object
  • Rectangle r
  • r new Rectangle(2, 3)
  • OR
  • Rectangle r new Rectangle(2, 3)
  • Use the object and the dot operator to access
    methods
  • r.setLength(5)
  • r.setWidth(10)

2500
2500
3
2
r
2500
2500
3
2
5
10
26
Unified Modeling Language Class Diagrams
27
  • class Clock
  • Data Members (Instance Variables)
  • private int hr //store hours
  • private int min //store minutes
  • private int sec //store seconds
  • Methods
  • public void setTime(int hours, int minutes,
  • int seconds)
  • public int getHours()
  • public int getMinutes()
  • public int getSeconds()
  • public void printTime()
  • public void incrementSeconds()
  • public void incrementMinutes()
  • public void incrementHours()
  • public boolean equals(Clock otherClock)
  • public void makeCopy(Clock otherClock)
  • public Clock getCopy()

28
To do
  • Start reading chapter 8
  • Pages 445-471
  • Finish Assignment 4 if you havent yet done so
  • Get started on Assignment 5
Write a Comment
User Comments (0)
About PowerShow.com