Content Analysis

AnEsh
OODPWeek8.pptx

Object Oriented Design and Programming

Week 8

A B Emran Salahuddin, Anchal Shrestha , Chaitalia Samani (Sydney)

Hanspreet Kaur, Allan NG (Melbourne)

Kent Institute Australia Pty. Ltd.

ABN 49 003 577 302 CRICOS Code: 00161E RTO Code: 90458 TEQSA Provider Number: PRV12051

Version 2 – 18th December 2015

1

SLIDE TITLE

Farrell, J. (2017) Programming Logic and Design, Comprehensive (9th ed.) Cengage Learning

2

2

Object Oriented Design and Programming

Code Modularization

3

Code Modularization

Programmers seldom write programs as one long series of steps.

Instead, they break down the programming problem into reasonable units, and tackle one small task at a time.

These reasonable units are called modules.

Programmers also refer to them as sub-routines, procedures, functions, or methods.

4

Programming Logic and Design, Ninth Edition

Code Modularization

The process of breaking down a large program into modules is called modularization.

Modularization provides abstraction.

Modularization allows multiple programmers to work on a problem.

Modularization allows you to reuse your work.

Modularization makes it easier to identify structures.

5

Programming Logic and Design, Ninth Edition

Abstraction

Abstraction is the process of paying attention to important properties while ignoring nonessential details.

Abstraction is selective ignorance.

Life would be tedious without abstraction

6

Programming Logic and Design, Ninth Edition

Abstraction

For example , you can create a list of things to accomplish today:

7

Programming Logic and Design, Ninth Edition

Modularization provides Abstraction

Abstraction makes complex tasks look simple.

some level of abstraction occurs in every computer program.

Fifty years ago, a programmer had to understand the low-level circuitry instructions the computer used.

But now, newer high-level programming languages allow you to use English-like vocabulary in which one broad statement corresponds to dozens of machine instructions

8

Programming Logic and Design, Ninth Edition

Abstraction

No matter which high-level programming language you use, if you display a message on the monitor, you are never required to understand how a monitor works to create each pixel on the screen.

You write an instruction like print message and the details of the hardware operations are handled for you.

9

Programming Logic and Design, Ninth Edition

Multiple Programmers

When you dissect any large task into modules, you gain the ability to divide the task among various people.

Professional software developers can write new programs in weeks or months, instead of years, by dividing large programs into modules and assigning each module to an individual programmer or programming team.

10

Programming Logic and Design, Ninth Edition

Reusability

If a subroutine or function is useful and well-written, you may want to use it more than once within a program or in other programs.

For example, a routine that checks the current date to make sure it is valid (the month is not lower than 1 or higher than 12,the day is not lower than 1 or higher than 31 if the month is 1,and so on) is useful in many programs written for a business.

11

Programming Logic and Design, Ninth Edition

Easier to identify Structures

12

Programming Logic and Design, Ninth Edition

Easier to identify Structures

The single program segment shown in Figure 3-2 accomplishes the same steps as the two program segments shown together in Figure 3-3.

Both program segments are structured.

The structure may be more obvious in the program segments in Figure 3-3 because you can see two distinct parts—a decision structure calls a subroutine named overtimeModule(),and that module contains another decision structure, which is followed by a sequence.

Neither of the program segments shown in Figures 3-2 and 3-3 is superior to the other in terms of functionality, but you may prefer to modularize to help you identify structures.

13

Programming Logic and Design, Ninth Edition

MODULARIZING A PROGRAM

Most programs contain a main module which contains the mainline logic.

This module then accesses other modules or subroutines.

When you create a module or subroutine, you give it a name

Module names must be one word.

Module names should have some meaning.

End with a pair of parenthesis().

After writing instructions, you use word return to go to calling module

14

Programming Logic and Design, Ninth Edition

MODULARIZING A PROGRAM

15

Programming Logic and Design, Ninth Edition

MODULARIZING A PROGRAM

16

Programming Logic and Design, Ninth Edition

Once you have created a module you then need to be able to use it

When you want to use a module you need to call it.

The name of the module is used to call the module. It is referred to as “Called module”

In figure, getInput(), calculateAverage(), printResult() are methods and they have been called in main module (Calling module) using their names

Modules can also be called in other modules.

MODULARIZION IN JAVA

Modules can be named as methods in java.

We have only been using a single method within a single class.

Move towards multiple methods within a class.

Definition of method will be outside the main method but in the same class (in case of one class only).

17

Programming Logic and Design, Ninth Edition

MODULARIZION IN JAVA

Syntax

modifier returnValueType methodName(list of parameters) { //Method body; }

There is a method header containing:

The method name

A specification of input data (in a parameter list – see later)

Some magic code (Memorise it for now, will focus on it in OODP detailed concepts)

There is a method body enclosed in braces (curly brackets)

Containing Java statements to perform the method’s task

18

Programming Logic and Design, Ninth Edition

MODULARIZION IN JAVA

19

Programming Logic and Design, Ninth Edition

General syntax of method:

MODULARIZION IN JAVA

In previous figure:

Magic code- Just code it, detail discussion will be done in next week.

Return value type- What type of data method will return? For example- int, double, char, boolean etc.

Method name- Meaningful name of method

Parameter list- Input data for method to perform some tasks.

Method body- Executable statements

20

Programming Logic and Design, Ninth Edition

MODULARIZION IN JAVA

Depending upon the return type and parameters, there can be three types of methods:

Method with no return value and no parameters.

Method with return value and no parameters.

Method with return value and parameters.

21

Programming Logic and Design, Ninth Edition

Method with no return value and no parameters

22

Programming Logic and Design, Ninth Edition

This method will just print the statements.

No need of parameters and will not return anything.

Simply use name to call it, displayMenu()

Method with no return value and no parameters(continued)

23

Programming Logic and Design, Ninth Edition

Method with return value and no parameters

For the example used so far we have not returned any data and the return value type has been void.

Methods are often used to perform calculations (eg the average of an array of int values) and need to be able to return a value In order to return a value:

The method header needs to indicate the return value type (eg double )

A return statement needs to be included in the method body that is the same type as defined in the method header

24

Programming Logic and Design, Ninth Edition

Method with return value and no parameters(continued)

When calling (invoking) the method you need to:

Use the value in some way such as store it in a variable or use System.out.println to print the value directly

25

Programming Logic and Design, Ninth Edition

Method with return value and no parameters(continued)

26

Programming Logic and Design, Ninth Edition

Method with return value and no parameters(continued)

27

Programming Logic and Design, Ninth Edition

Local Variables

Variables declared in a Method are called local variables.

Their scope (code that can access them) is the method they are declared in.

This means their values can only be set or changed by the method’s code and not by any other method’s code.

28

Programming Logic and Design, Ninth Edition

Local Variables

This means programmers can create and maintain a method without fear of accidentally interacting with the data of other methods.

This dramatically increases the efficiency of programmers and lowers their error rates.

Any variables declared in main are local to main and declared in methods are local to methods.

29

Programming Logic and Design, Ninth Edition

Local Variables

Check the scope of variable sum

Programming Logic and Design, Ninth Edition

30

Method with return value and parameters

31

Programming Logic and Design, Ninth Edition

Data type of parameter of a method and the value passed while calling should be same.

Summary

Programming Logic and Design, Ninth Edition

32

The process of breaking down a large program into modules is called modularization.

Most programs contain a main module which contains the mainline logic.

This module then accesses other modules or subroutines.

Depending upon the return type and parameters, there can be three types of methods in Java.

kent.edu.au Kent Institute Australia Pty. Ltd. ABN 49 003 577 302 ● CRICOS Code: 00161E ● RTO Code: 90458 ● TEQSA Provider Number: PRV12051

33

33