Documente Academic
Documente Profesional
Documente Cultură
Ralph Morelli
Trinity College
Hartford, CT
published by Prentice
Hall
Java, Java, Java
Object Oriented Problem Solving
Chapter 3 Methods:
Communicating with Objects
Objectives
• Understand the role that methods play in an
object-oriented program.
• Know how to use parameters and arguments
to pass data to an object.
• Understand how constructor methods are
used to instantiate objects.
• Know the difference between passing a value
and passing a reference to an method.
• Know how to design your own methods.
• Be able to use the selection control structure.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Outline
• Passing Information to an Object
• Constructor Methods
• Retrieving Information from an Object
• Passing a Value and Passing a Reference
• Flow of Control: Selection Control Structures
• The Improved CyberPet
• From the Java Library: Object
• Object-Oriented Design: Class Inheritance
• In the Laboratory: Feeding CyberPet
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Passing Information to an Object
• Let’s add a name variable to CyberPet:
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Parameter Scope
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Parameter
Scope
Drawing boxes
around modules
helps visualize
scope.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Arguments and Parameters
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Arguments and Parameters (cont)
Create a CyberPet instance
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Parameters and the Generality Principle
• Parameters make methods more general.
• A poor design for a setName() method.
This version can only set the pet’s name to
“Socrates”:
public void setName()
{
name = "Socrates";
}
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Parameters and the Generality Principle
• We can use setName(String) to create two
CyberPets with different names:
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Constructor Methods
• Used to create an instance (object) of a
class
• Not inherited by subclasses
• Used to initialize instance variables
• Defined as public or private
• Do not return public
a value
CyberPet(String str)
{
• Example: }
name = str;
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Default Constructors
• If no constructor is coded, Java provides a
default constructor.
• If a class is public, the default constructor
will also be public.
• CyberPet: Invoking the default constructor:
CyberPet socrates = new CyberPet();
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Overloading and Method Signatures
• A method name is overloaded if there is more
than one method with the same name:
public CyberPet () { } // Constructor #1
public CyberPet (String str) // Constructor #2
{
name = str;
}
Constructor
invocations
CyberPet pet1 = new CyberPet();
pet1.setName("Pet1");
CyberPet pet2 = new CyberPet("Pet2");
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Retrieving Information from an Object
• Methods with non-void return types can be used
to extract information from an object.
• A method that returns a value may or may not
have a formal parameter list.
Return Type Parameters
Return Value
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
The CyberPet.getName() Method
public String getName() getName() takes no parameters,
{
return name; and returns the value of the
} name instance variable
pet1.setName("Socrates");
pet1.getName() has the
pet1.getName(); value “Socrates”
System.out.println(pet1.getName());
Nested Method Call: We use
println() to print “Socrates”
We can assign “Socrates”
to a String variable
String s = pet1.getName();
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
The Expanded CyberPet Class
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Passing a Value vs. Passing a Reference
• Passing a primitive value differs from
passing a reference value
• Values of type int, boolean, float, and
double are examples of primitive types. A
primitive argument cannot be changed by a
method call.
• All objects (String, CyberPet) are reference
types. Reference arguments can be changed
by a method call.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Passing a Primitive Value
• For primitive type parameters, a copy of the
argument’s value is passed to the method.
public void primitiveCall(int n)
{ primitiveCall() will be
n = n + 1; passed an int value
}
5
int x = 5; x stores the value 5
primitiveCall(x);
• In (b), p.setName() changes pet1’s name to “Mary”. This change will persist.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Flow of Control: Selection Structures
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
The Simple If Statement
if ( boolean expression )
statement ;
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
The getState() Method
public String getState()
{
if (isEating)
return “Eating”;
if (isSleeping)
return “Sleeping”;
return “Error in State”;
}
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
The If-Then-Else Statement
if ( boolean expression )
statement1
else
statement2 ;
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Multiway Selection
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
The Dangling Else Problem
• Rule: An else clause
matches the closest previous
unmatched if clause.
• Indentation (which the
compiler ignores) should
reflect the statement’s logic.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
The Switch/Break Structure
• Multiway selection can also be done with
the switch/break structure.
switch ( integralExpression )
{
case integralValue2 :
statement1;
break;
case integralValue2 :
statement2;
break;
…
case integrealValueN :
statementN;
break;
default:
statementDefault;
}
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Switch/Break Examples
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Inheritance: Object.toString()
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Overriding Object.toString()
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Polymorphic Object.toString()
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
In the Laboratory: Feeding CyberPet
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
In the Laboratory: Feeding CyberPet
Problem Statement
Modify the CyberPet and TestCyberPet
classes to create a simulation in which pets
can eat different kinds of food. The
completed program should be capable of
producing the following output:
pet1's name is Socrates
Socrates is eating an apple
Socrates is sleeping
pet2's name is Cleopatra
Cleopatra is eating beans
Cleopatra is sleeping
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Object Design
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Technical Terms
• accessor method • multiway selection
• argument • mutator method
• boolean expression • parameter
• class scope • primitive type
• constructor • reference type
• dangling else • scope
• dynamic binding • selection structure
• local scope • side effect
• method definition • stub method
• method invocation • switch/break structure
• method overloading
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Summary Of Important Points
• A formal parameter is a place holder in a
method declaration and it always consists of
a type followed by variable identifier.
• An argument is a value that is passed to a
method via a formal parameter when the
method is invoked.
• A method's parameters constrain the type of
information that can be passed to a method.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Summary of Important Points (cont)
• Parameter Passing. When an argument of
primitive type is passed to a method, it
cannot be modified within the method.
• When an argument of reference type is
passed to a method, the object it refers to
can be modified within the method.
• Except for void methods, a method
invocation or method call is an expression
which has a a value of a certain type.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Summary of Important Points (cont)
• The signature of a method consists of its
name, its return type, and the number and
type of its formal parameters.
• A class may not contain more than one
method with the same signature.
• A constructor is a method that is invoked
when an instance object is created. If a class
does not contain a constructor method, the
Java compiler supplies a default constructor.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods
Summary of Important Points (cont)
• The if statement executes a statement only
if its boolean condition is true.
• The if-else statement statement executes
one or the other of its statements depending
on the value of its boolean condition.
• Multiway selection allows one and only one
of several choices to be selected depending
on the value of its boolean condition.
Java, Java, Java, 2E by R. Morelli Copyright 2002. All rights reserved. Chapter 3: Methods