data structures: csci 362 – object design techniques data structures: csci 362 – object design...

28
Dr. Nazli Mollah Data Structures: CSCI 362 – Object Design Data Structures: CSCI 362 – Object Design Techniques Techniques lecture notes adapted from Data Structures with C++ using STL Overview of Lecture Overview of Lecture Review of last lecture Software Design Overview Software Design Phases Example: Calendar Class Example: Date Class Implementing the Calendar Class Lab Error Handling C++ Exceptions Object Composition TimeCard Class Operator Overloading Operating Function Time24 Class Member Function Overloading

Upload: lucinda-gardner

Post on 24-Dec-2015

236 views

Category:

Documents


0 download

TRANSCRIPT

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Overview of LectureOverview of Lecture

Review of last lecture

Software Design Overview Software Design Phases

Example: Calendar Class Example: Date Class

Implementing the Calendar Class Lab

Error Handling C++ Exceptions Object Composition TimeCard Class Operator Overloading Operating Function Time24 Class Member Function Overloading

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Review of Last LectureReview of Last Lecture

Information Hiding: Each program component hides as much info as possible from the component users In C++ this is done by placing components in the _____ part of the class

Abstract Data Types (ADT): a collection of values and operations that act on those values encapsulated

In encapsulation, the users: Do not need to know the hidden information Are not allowed to directly manipulate or use the hidden information

Terms in relation to classes and ADT Data members/ instance variables

Hold state of the object, each instance has its own copy Member functions/ methods

Actions of the objects Private sections

Accessible only by class Public sections

Accessible by users

Time24 Class example

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Introduction to Object Design TechniquesIntroduction to Object Design Techniques

There are a number of phases in in the software life cycle

It is important to design the public interface of a class and to develop algorithms for implementing class member functions carefully before beginning a class implementation

Testing and program maintenance is as important as creation of the original product because failure of running or updating the application results in the product becoming obsolete and a commercial failure

Error handling – the calling code should handle the error, not the member or free function that encounters the error

Exceptions are the better mechanism for handling errors

Object composition is a fundamental object design technique and it facilitates software reuse

Constructor initialization list can be used in providing initial values for objects included in by composition

Operator overloading is an object design technique that makes using a class easier and more flexible

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Software DesignSoftware Design

A computer program begins with a problem that the client wants solved

The process of building the program starts with an analysis of the problem It proceeds through a series of stages to produce a product that is reliable and

easy to maintain

Prior to the standardization of the software development life cycle: Programmers added code to software with little attention to its integration into the

system Over time systems deteriorated and became so difficult to update that

programmers had to develop new software to replace them

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Software Design TechniquesSoftware Design Techniques

The software life cycle consists of: Request phase Analysis phase Design phase Implementation phase Testing phase Maintenance phase

(RADIT M)

Each has an equal importance in the Software Development Lifecycle

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Software Design PhasesSoftware Design Phases

1. Request:

a. Client perceives a need for a software system to solve a problem or to facilitate more effective operations

b. Client meets with a computer consultant who undertakes a feasibility study for the project

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Software Design PhasesSoftware Design Phases

2. Analysis:

a. In this phase, the system requirements are determined

b. Systems analyst determines requirements and is the intermediary between the client and the software engineers

Result: a functional specification of the software system that includes a list of needs and special requirements.

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Software Design PhasesSoftware Design Phases

3. Design:

a. Translate the functional specification into an abstract model of the system.

b. Identify the components of the system and develop algorithms that will be used for implementation

c. Create the declaration of the classes including their attributes and public member functions

Describe how the classes are related to each other

Design the algorithms that allow the classes to effectively interact

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Software Design PhasesSoftware Design Phases

4. Implementation:

a. Use a programming language and the design specification to code the different components of the system

b. Convert the design into a program

c. Implement the classes independently and verify the action of their member functions

d. Implement program units that coordinate object interaction

e. Code the main program that manages the overall execution of the software system

Often, the implementation stage will discover errors or oversights in design

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Software Design PhasesSoftware Design Phases

5. Testing:

a. Check the program for logical and runtime errors and verify that the system meets the specifications of the client

b. Analysis, design, and implementation phases interact to bring about modifications and corrections to the specifications, and the code’s effective interaction among the phases depends on frequent and systematic testing

c. Test the classes and functions which are the individual units of the software system

d. Perform integrative testing to ensure that the units fit together to create a program that runs correctly and efficiently

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Software Design PhasesSoftware Design Phases

6. Maintenance:

a. Periodically update of the software to stay current and to respond to changing needs of the client

b. Begins as soon as the client installs the system

c. The client will identify bugs or features that must be changed or added

d. A client may add new hardware and/or find new applications for the software

e. Modern software systems must be regularly upgraded or they will eventually become obsolete and unusable.

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Example: CLASS CalendarExample: CLASS Calendar

1. Request:

Computer professionals have a preliminary session with the client who has requested this meeting

The client asks for a simple software which will facilitate that upon entering month and year, the program will display a calendar e.g. entering 8 and 1981 will result in a calendar with the month August, 1981 being displayed

The client is of the opinion that for their particular chemical manufacturing plant, a system that can easily call up a particular month of a particular year will help in the effectiveness of running their business

Sun Mon Tues Wed Thur Fri Sat

1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30 31

August 1981

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

CLASS CalendarCLASS Calendar

2. Analysis Involves understanding what the client’s business involves and how this program will be

beneficial for them so that it can be designed and built for that purpose – the objective has to be clear and the client and programmer have to be on the same page

In this case, the client has a chemical manufacturing plant which needs to keep track of certain chemical compounds. Each chemical pack has a tracking number which includes sell date by month number and the year.

A feasibility study has to be carried out to see whether building the software will be cost and time effective for the client (this is paramount in the case of more complicated software design and global companies)

The system requirements should also be assessed This is carried out by a system analyst who acts as a intermediary between the client and the software

engineer

Results of the Analysis Phase:Functional Specification of the software system that includes:

1. input: the program prompts the user to enter a month (1 to 12) and a year (1900 or later)

2. output: displays a calendar with the name of the month, and the year in a label, dates listed as Sun, Mon, Tue,…, Sat, and the dates listed in a table below

3. details of assumptions: use as base reference that Jan 1, 1900 is Mon4. processing requirements: client wants flexibility to take program and produce a

yearlong calendar by calling the monthly calendar display for each calendar

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

CLASS CalendarCLASS Calendar

3. Design of Program

The problem is viewed as a set of objects that interact to carry out tasks

The software system represents each object by a class consisting of data and of operations that manage that data

Object-oriented programming employs a bottom-up approach that first identifies the objects and their operations and then builds scenarios that describe how the objects should interact

In this phase the structural blueprint are designed for programmers to use to implement the individual components and relate in the overall system

Object 1ClassData

Operations

Overall System

Object 4ClassData

Operations

Object 5ClassData

Operations

Object nClassData

Operations

Object 3ClassData

Operations

Object 2ClassData

Operations

Main Program

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Designing the Calendar ClassDesigning the Calendar Class

CLASS calendar Declaration “d_cal.h”class calendar{

public:calendar(int m = 1, int y = 1900);

// initialize the month and year for display.// precondition: month is in the range 1 to 12 and year// is 1900 or later

Design begins by viewing a calendar month as an object

Create a calendar class whose public-member functions meet the specifications given by the client

The class data will store the month and year

A user will input the month and year, so the constructor uses these values to initialize the data

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Designing the Calendar ClassDesigning the Calendar Class

CLASS calendar Declaration “d_cal.h”

void displayCalendar() const;

// display the calendar with a header and a table of dates

int getMonth() const;// return the current month for the calendar

int getYear() const;// return the current year for the calendar

The operation displayCalendar() outputs the calendar

The client wants the flexibility to output a yearly calendar by displaying the individual calendars for each month in the year – to provide this capability, we add the functions getMonth() and getYear() to access the data and the functions setMonth() and setYear() to update the data

The constructor and the set functions have preconditions that ensure the month and the year are within their correct ranges

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Designing the Calendar ClassDesigning the Calendar Class

CLASS calendar Declaration “d_cal.h”

void setMonth(int m);

// update the month for the calendar// precondition: m must be in the range 1 to 12

void setYear(int y);// update the year for the calendar// precondition: y must be >= 1900

private:. . .

;

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Designing the displayCalendar() AlgorithmDesigning the displayCalendar() Algorithm

The member function displayCalendar() outputs both the calendar label and the grid of dates and names for the days in the week – these are distinct tasks

To display the label , we need to convert the integer value for the month to a string that gives the name of the month

Displaying the dates under the correct names for the days in the week requires knowing the day corresponding to the first date of the month, the number of days in the month etc.

To isolate the tasks, our implementation design creates 2 private-member functions in a class implementation (see pg 62)

The key element in the design of the calendar problem is the algorithm for displayDates()

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

displayCalendar()displayCalendar()

Once displayCalendar() begins placing dates in the calendar grid, it must know when to stop

We must have access to the number of days in the month, including special attention to February in case of a leap year

Finding the Day for the First of the Month

Allocate values to days of the week (Sun is 0, Mon is 1…Sat is 6) Jan 1, 1900 is Mon (not a leap year) – what is Jan 1, 1901? (365 + 1)/7 = 52 r 2 Tues

Jan 1, 1999 is Friday – what is April 1, 1999 (not a leap year) Jan (31) +Feb (28) + Mar (31) = 90 days (90 + 5)/7 = 13 r 4 Thurs

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Class dateClass date

CLASS date Constructor “d_date.h”

date(int mm = 1, int dd = 1, int yy = 1900);

Initializes the date with default January 1, 1900

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Class dateClass date

CLASS date Operations “d_date.h”

int daysInMonth();Returns the number of days for the month. If the year is a leap year, month 2 (February)

returns 29

int getMonth();Returns the month for the current date

int getYear();Returns the year for the current date

bool isLeapYear();Return true if the year is a leap year, otherwise returns false

int numberOfDays();Return the number of days into the year

void setMonth(int mm);Update the month in the current date

void setYear(int yy);Update the year in the current date

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Program ImplementationProgram Implementation

4. Implementing the Program The implementation phase of the software development cycle uses the syntax

and features of a programming language like C++ to convert the design into a program

A design includes a series of classes and functions to direct the interaction among the objects:

Steps1. Implement the classes independently and verify the actions of their member functions

2. Implement program units that coordinate object interaction

3. Code the main program that manages the overall execution of the software system Theoretically, in the development cycle, implementation should ideally follow the

design In reality there is dynamic interaction between design and implementation since

many errors and deficiencies in design are understood only during the implementation phase of a program

To correct the errors and deficiencies, it is necessary to modify and update the initial design

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Testing and Debugging ProgramTesting and Debugging Program

5. Testing the Program Analysis, design, and implementation are interactive processes that bring about

modifications ad corrections to the specifications and the code

Effective interaction among the phases depends on frequent and systematic testing

It is necessary to test the classes and functions that are the individual units of the software system as well as to perform integrative testing to ensure that the units fit together to create a program that runs correctly and cohesively and efficiently and meets the specifications of the client

Program Validation Testing: involves the client and the software developers to ensure that the correct problems are being solved. This is carried out throughout the development cycle

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Correctness of a ProgramCorrectness of a Program

There a number of testing strategies that enable developers to verify the correctness of a program

These strategies fall into 2 categories:1. Blackbox tests: focus on the input and output behavior of the program without

concern for the internal structure of its classes and functions. A programmer performs blackbox testing by choosing input data for which the output is known

2. Whitebox tests: focus on the internal structure of the program. The test requires data that will exercise each line of code in each function. Special attention is paid to statements and loops within the code

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Maintaining the ProgramMaintaining the Program

6. Maintaining the Program Ideally, the analysis, design, and implementation phases of the software-

development cycle produces an error-free application that meets all of the client’s needs

But let’s keep it real! This rarely happens!

So in order to ensure the longevity and effectiveness of the software, the maintenance phase must begin as soon as the client installs the system

Under the scrutiny of usage of the client, bugs or features that need to be modified are identified

Over time, a client adds new hardware and finds new applications for the software

Software systems must be regularly upgraded

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

Handling Runtime ErrorsHandling Runtime Errors

Handling errors during function execution is an important aspect of program design

When implementing any function, a programmer must be aware that the code will execute in a runtime environment that includes not only the computer system but also a user.

There are three fundamental ways to handle errors: Output an error message and terminate the program

Generally avoided unless necessary

Return a Boolean flag from the function call indicating that an error occurred Grants the advantage of leaving the decision about how to handle the error

to the calling code block

use the C++ exception mechanism The best approach

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

C++ Exception MechanismC++ Exception Mechanism

C++ exceptions are handled by three keywords:

Try

Function calls and code that may generate an exception are placed in a try block

Catch

The exception handler outputs an error message and either takes corrective action or terminates the program

Throw

Bypasses the normal function return mechanism and searches chain of previous function calls until it locates a catch block.

Dr. Nazli Mollah

Data Structures: CSCI 362 – Object Design TechniquesData Structures: CSCI 362 – Object Design Techniques

lecture notes adapted fromData Structures with C++ using STL

C++ Exception MechanismC++ Exception Mechanism

An exception is an object that contains information that is transmitted without using the normal function return process