Download method

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts

Go (programming language) wikipedia, lookup

Smalltalk wikipedia, lookup

Falcon (programming language) wikipedia, lookup

Design Patterns wikipedia, lookup

Monitor (synchronization) wikipedia, lookup

Java syntax wikipedia, lookup

C syntax wikipedia, lookup

Class (computer programming) wikipedia, lookup

Expectation–maximization algorithm wikipedia, lookup

Name mangling wikipedia, lookup

Least squares wikipedia, lookup

Object-oriented programming wikipedia, lookup

C++ wikipedia, lookup

C Sharp (programming language) wikipedia, lookup

C Sharp syntax wikipedia, lookup

Transcript
Chapter 3:
Developing Class
Methods
Object-Oriented Program
Development Using Java:
A Class-Centered
Approach
Method Development: Algorithms

Algorithm



Must clearly understand difference
between algorithmic and intuitive
commands


Step-by-step set of instructions
Describes how data are to be processed to
produce desired result
Computers do not understand intuitive
commands
Coding the algorithm

Writing in programming language
2
Method Development: Algorithms
(continued)

Pseudocode


Formulas


English-like phrases used to describe
algorithm
Mathematical equations are used
Flowcharts

Diagrams that employ symbols are used
3
4
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
5
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
6
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
7
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.
8
Static variable
static 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;
9
10
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.
11
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
12
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
13
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
14
Scope

Section of program within which an
identifier is valid

Defines portion of class where
variable can be used

Scopes in Java:

Local

Class
15
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
16
Visibility

Determines whether a member can be
accessed from outside a class in which
it is declared

Modifiers:


Public

Protected

Private
Visibility rules
17
18
Values and Identities

Identity


Every object has a unique identity
Literal values do not have
identities
19
final Variables


final variables

Have initial value that cannot
subsequently be changed

Also called:

Named constants

Symbolic constants
Use final reserved word
20
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
21
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 …
22
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
}
}
23
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
24
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
25
Summary (continued)

Algorithm:



Scope:


Step-by-step procedure
Describes how a single computation or
task is to be performed
Section of a program within which an
identifier is valid
Static variables can be accessed
without being referenced as a data
26