chapter 3: developing class methods object-oriented program development using java: a class-centered...

26
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class- Centered Approach

Upload: noel-caldwell

Post on 18-Jan-2016

231 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

Chapter 3: Developing Class

Methods

Object-Oriented Program Development Using Java:

A Class-Centered Approach

Page 2: 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

Page 3: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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

Page 4: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

4

Page 5: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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

Page 6: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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

Page 7: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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

Page 8: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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.

Page 9: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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;

Page 10: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

10

Page 11: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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.

Page 12: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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

Page 13: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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

Page 14: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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

Page 15: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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

Page 16: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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

Page 17: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

17

Visibility Determines whether a member can be

accessed from outside a class in which it is declared

Modifiers: Public Protected Private

Visibility rules

Page 18: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

18

Page 19: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

19

Values and Identities

Identity

Every object has a unique identity

Literal values do not have identities

Page 20: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

20

final Variables

final variables Have initial value that cannot

subsequently be changed

Also called: Named constants

Symbolic constants

Use final reserved word

Page 21: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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

Page 22: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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 …

Page 23: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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

}

}

Page 24: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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

Page 25: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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

Page 26: Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach

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