21
Software Engineering Software Engineering Object-Centered Design

Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

Embed Size (px)

Citation preview

Page 1: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

Software EngineeringSoftware Engineering

Object-Centered Design

Page 2: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

Problem SolvingProblem Solving

Does anyone scuba dive?Does anyone scuba dive?

Let’s solve this Let’s solve this scuba-diving problemscuba-diving problem::

Write a program that, given a depth under water, Write a program that, given a depth under water, computes the pressure at that depth.computes the pressure at that depth.

Page 3: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

BehaviorBehavior

A. Describe the desired behavior of the A. Describe the desired behavior of the program:program:

Our program should display a prompt for the Our program should display a prompt for the depth on the screen, read the depth from the depth on the screen, read the depth from the keyboard, compute the corresponding keyboard, compute the corresponding pressure, and display the pressure, along with pressure, and display the pressure, along with a descriptive label on the screen.a descriptive label on the screen.

Page 4: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

ObjectsObjects

B. Identify the B. Identify the nounsnouns in the behavioral description in the behavioral description (other than program and user):(other than program and user):

Our program should display a prompt for the depth Our program should display a prompt for the depth on the screen, read the depth from the keyboard, on the screen, read the depth from the keyboard, compute the corresponding pressure, and display compute the corresponding pressure, and display the pressure, along with a descriptive label on the the pressure, along with a descriptive label on the screen.screen.

These make up the These make up the objectsobjects in our program. in our program.

Page 5: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

OperationsOperations

C. Identify the C. Identify the verbsverbs in the behavioral description: in the behavioral description:

Our program should display a prompt for the depth Our program should display a prompt for the depth on the screen, read the depth from the keyboard, on the screen, read the depth from the keyboard, compute the corresponding pressure, and display compute the corresponding pressure, and display the pressure, along with a descriptive label on the pressure, along with a descriptive label on the screen.the screen.

These make up the These make up the operationsoperations in our program. in our program.

Page 6: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

AlgorithmAlgorithm

D. Organize the objects and operations into a D. Organize the objects and operations into a sequence of steps that solves the problem, sequence of steps that solves the problem, called an called an algorithmalgorithm..

0. Display a prompt for 0. Display a prompt for depthdepth on the on the screenscreen..

1. Read 1. Read depthdepth from the from the keyboardkeyboard..

2. Compute 2. Compute pressurepressure from from depthdepth..

3. Display 3. Display pressurepressure, plus an informative , plus an informative labellabel on the on the screenscreen..

Page 7: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

CodingCoding

Once we have designed an algorithm, the Once we have designed an algorithm, the next step is to translate that algorithm next step is to translate that algorithm into a high level language like C++.into a high level language like C++.

This involves figuring out how to This involves figuring out how to – represent our objects, andrepresent our objects, and

– perform our operations,perform our operations,

in C++ (with the aid of a book, if in C++ (with the aid of a book, if necessary...)necessary...)

Page 8: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

Representing ObjectsRepresenting Objects

A. Determine a type and name for each object:A. Determine a type and name for each object:

Object C++ Type Name

a promptthe depththe screenthe keyboardthe pressurea label

stringdoubleostreamistreamdoublestring

--depthcoutcinpressure--

Page 9: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

Performing OperationsPerforming Operations

B. Identify the C++ operator to perform B. Identify the C++ operator to perform a given operation, if there is one...a given operation, if there is one...

Operation Library? Name

Display a stringRead a doubleCompute pressureDisplay a double

iostreamiostream--iostream

<<>>--<<

To compute pressure, we need to find the To compute pressure, we need to find the pressure-depth formula in a reference book...pressure-depth formula in a reference book...

Page 10: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

Depth-to-PressureDepth-to-Pressure

In a reference book, we find thatIn a reference book, we find that– pressure is 0 atmospheres at depth 0pressure is 0 atmospheres at depth 0

– pressure increases 1 atmosphere every 33 pressure increases 1 atmosphere every 33 feet.feet.

– 1 atmosphere of pressure = 14.7 1 atmosphere of pressure = 14.7 lbs./sq.in.lbs./sq.in.

Computing the pressure thus adds new Computing the pressure thus adds new objects and operations to our objects and operations to our problem...problem...

Page 11: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

Objects (Revised)Objects (Revised)Object C++ Type Name

a promptthe depththe screenthe keyboardthe pressurea labelfeet/atmospherelbs/in2/atmosphere

stringdoubleostreamistreamdoublestringdoubledouble

--depthcoutcinpressure--FEET_PER_ATMLBS_PER_SQ_IN_PER_ATM

Page 12: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

Operations (Revised)Operations (Revised)Operation Library? Name

Display a stringRead a doubleCompute pressure

Divide two doublesAdd two doublesMultiply two doubles

Display a double

iostreamiostream--

built-inbuilt-inbuilt-in

iostream

<<>>--

/+*

<<

Page 13: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

DocumentationDocumentation

Always begin a file with an Always begin a file with an openingopening commentcomment::

/* pressure.cpp is a scuba-diving utility.

* Author: J. Adams

* Date: 1/1/98

* Purpose: Find pressure at a given depth.

*/

Page 14: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

Coding: Program StubCoding: Program Stub

/* pressure.cpp is a scuba-diving utility./* pressure.cpp is a scuba-diving utility. * ...* ... */*/

#include <iostream> // cin, cout, <<, >>using namespace std;

int main(){}

Page 15: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

Coding: Declaring Coding: Declaring ConstantsConstants

// ...int main(){ const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM = 14.7;}

Page 16: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

Coding: Algorithm Steps Coding: Algorithm Steps 0, 10, 1

// ...int main(){ const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM = 14.7; cout << “\nScuba Pressure Calculator!!” << “\n Enter the depth (feet): “;double depth;cin >> depth;

}

Page 17: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

Coding: Algorithm Step 2Coding: Algorithm Step 2

// ... cout << “\nScuba pressure calculator!!” << “\n Enter the depth (feet): “;double depth;cin >> depth;

double pressure = ((depth / FEET_PER_ATM) + 1) * LBS_PER_SQ_IN_PER_ATM;}

Page 18: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

Coding: Algorithm Step 3Coding: Algorithm Step 3

// ... double pressure = ((depth / FEET_PER_ATM) + 1) * LBS_PER_SQ_IN_PER_ATM; cout << “\nThe pressure at “ << depth << “ feet is “ << pressure << “lbs/sq.in.” << endl;}

Page 19: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

TestingTestingRun your program using sample data (whose correctness is easy to Run your program using sample data (whose correctness is easy to

check):check):

Scuba Pressure Calculator!! Enter the depth (feet): 33

The pressure at 33 feet is 29.4 lbs/sq.in.

Page 20: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

SummarySummary

Writing a program consists of these Writing a program consists of these steps:steps:

1. Design an algorithm for your program.1. Design an algorithm for your program.

2. Code your design.2. Code your design.

3. Test your program.3. Test your program.

4. Maintain/upgrade your program as 4. Maintain/upgrade your program as necessary.necessary.

Page 21: Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given

Summary (ii)Summary (ii)

OCD is a methodology for designing OCD is a methodology for designing programs:programs:

1. Describe the desired 1. Describe the desired behaviorbehavior of the of the program.program.

2. Identify the 2. Identify the objectsobjects required. required.

3. Identify the 3. Identify the operationsoperations required. required.

4. Organize objects and operations into an 4. Organize objects and operations into an algorithmalgorithm, refining object and operation lists , refining object and operation lists as necessary.as necessary.