Docsity

Java Programming: Developing Class Methods and Algorithms, Slides of Applications of Computer Sciences

Partial preview of the text

Download Java Programming: Developing Class Methods and Algorithms and more Slides Applications of Computer Sciences in PDF only on Docsity!

Chapter 3:

Developing Class Methods

Object-Oriented Program

Development Using Java: A

Class-Centered Approach

Objectives

  • Method and Parameter Declarations
  • Returning a Single Value
  • Method Development: Algorithms
  • Application: Swapping Values
  • static and final Variables
  • Common Programming Errors

Writing the Method

(continued)

  • When a method is called, parameters passed

must agree in:

  • Number
  • Order
  • Data type

Reusing Method Names

(Overloading) (continued)

  • Parameter signature is part of the header

line containing:

  • Method name
  • Parameter list

Passing a Reference Value

  • A copy of the value in a variable is passed to the called method - Stored in one of the method’s formal parameters
  • A change to the parameter’s value has no effect on the argument’s value - Except in extremely limited cases

Returning a Single Value

(continued)

  • Example:
    • public double calculateArea()
  • Returning a value syntax:
    • return expression;
  • After value is returned, program control reverts to calling method

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

Method Development:

Algorithms (continued)

  • Pseudocode
    • English-like phrases used to describe algorithm
  • Formulas
    • Mathematical equations are used
  • Flowcharts
    • Diagrams that employ symbols are used

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

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