chapter 3: developing class methods object-oriented program development using java: a class-centered...
Post on 18-Jan-2016
231 Views
Preview:
TRANSCRIPT
Chapter 3: Developing Class
Methods
Object-Oriented Program Development Using Java:
A Class-Centered Approach
2
Method Development: Algorithms
Algorithm Step-by-step set of instructions Describes how data are to be processed to
produce desired result Must clearly understand difference between
algorithmic and intuitive commands Computers do not understand intuitive
commands Coding the algorithm
Writing in programming language
3
Method Development: Algorithms (continued)
Pseudocode
English-like phrases used to describe algorithm
Formulas
Mathematical equations are used
Flowcharts
Diagrams that employ symbols are used
4
5
Using Pseudocode Most commonly used method for
developing algorithms
Short English phrases
Example: Input three numbers into computer
Calculate average by adding numbers and dividing the sum by 3
Display average
6
Application: Swapping Values Swapping:
Exchanging data that are either stored or referenced by two variables
Commonly seen in programming Algorithm:
Store first variable’s value into temporary location
Store second variable’s value into first variable
Store temporary value into second variable
7
Application: Swapping Values (continued)
How should two variables be made available to a method that will swap their values?
Procedure: Encapsulate two variables within a single
object Use class method to implement swap
algorithm Switch values in instance variables
8
static and final Variables
So far, we’ve seen local variable ( declared inside method)
instance variable ( declared inside class but outside method)
Instance variable – term used because each instance of the class has its own version of the variable.
Each object has its own memory place so it can have a distinct value for that class.
9
Static variablestatic variable (or class variable) Created only once for each class Shared by all objects created from class – only one copy
of a static variable for all objects of the class Changing the value of a static variable in one object
changes it for all the other objects Declared in same way as instance variables
Except static reserved word is used in declaration
Example static declaration: private static int numEmployees = 125;
10
11
Static or Class Method
Invoked through the class name – don’t have to instantiate an object of the class to invoke the method. e.g. all method of the Math class are
static methods so Math.sqrt(16); or Math.pow(5,3);
These methods perform basic calculations based on values passed as parameters. No object state to maintain in these situations.
12
Static Methods (cont.)
Java main method must be static so that main can be executed without instantiating an object from the class containing main.
Static methods do not operate in the context of a particular object.
Can only reference static variables
13
Summary of static methods or class methods
Provide means of accessing static variables in absence of any specific object
Restricted to using:
Static variables
Other static methods
Additional values and objects that are passed as arguments into method
Include static reserved word in method’s header
14
static Methods (continued)
Can also be used for constructing general-purpose methods
Perform functions using only data passed to them as arguments at time of call
i.e. main() method
15
Scope
Section of program within which an identifier is valid
Defines portion of class where variable can be used
Scopes in Java:
Local
Class
16
Scope (continued)
Local scope:
Variables declared within a method
All parameters of a method
Class scope:
All instance and static variables declared in a class’s variable declaration section
All methods contained within a class’s method definition section
17
Visibility Determines whether a member can be
accessed from outside a class in which it is declared
Modifiers: Public Protected Private
Visibility rules
18
19
Values and Identities
Identity
Every object has a unique identity
Literal values do not have identities
20
final Variables
final variables Have initial value that cannot
subsequently be changed
Also called: Named constants
Symbolic constants
Use final reserved word
21
final Variables (continued) Example:
public final static int NUMEMPS = 100;
Blank final: No initial value is provided
Value initialized later in program
final float DENSITY;
Java convention: Use uppercase letters for identifiers
22
Placement of Statements A variable must be declared before it
can be used Proper placement:
public class className
{
// declaration section
final static variable declarations
static variable declarations
instance variable declarations
continued …
23
Placement of Statements Proper placement:
// methods section - example of one method
method header line(parameter list)
{
final variable declarations
variable declarations
object declarations
other Java statements
}
}
24
Common Programming Errors Attempting to pass incorrect data types Declaring the same variable locally within
both calling and called methods and assuming the change in one variable affects the other variable
Forgetting to include the data type of a method’s parameters within the header line
Attempting to alter an object’s private variables by passing an object into a non-class method as an argument
25
Summary
A method is called by: Giving its name Passing any data to it in parentheses following
the name A method’s return type declares the data type of
the value returned by the method Methods can directly return at most a single
value A called method cannot alter either a primitive
data type argument’s value or a reference variable’s value
26
Summary (continued)
Algorithm: Step-by-step procedure Describes how a single computation or task is to be
performed Scope:
Section of a program within which an identifier is valid
Static variables can be accessed without being referenced as a data member of a specific object
top related