The Software Lifecycle. Example Problem: Update a Checkbook Write a program that allows the user to...

Preview:

Citation preview

The Software Lifecycle

Analysis------------Verify

Design------------Verify

Implementation-------------------

Test

Integration-------------Test

Maintenance

Example Problem: Update a Checkbook

Write a program that allows the user toenter a starting balance, a transactiontype, D or W, and a transaction amount.

The program should perform the given operation (deposit or withdraw) and display the results.

Sample User Interactionwith the Program

Enter the starting balance and press <Enter>: 235.16Enter the transaction type (D) deposit or (W) withdrawal and press <Enter>: DEnter the transaction amount and press <Enter>: 75.00

Starting Balance $235.16

Transaction $75.00 D

Ending Balance $310.16

Sample User Interactionwith the Program

Enter the starting balance and press <Enter>: 310.16Enter the transaction type (D) deposit or (W) withdrawal and press <Enter>: WEnter the transaction amount and press <Enter>: 65.75

Starting Balance $310.16

Transaction $65.75 W

Ending Balance $244.41

Analysis

• Analysis describes what needs to be done to solve a problem, not the details of how that is done

• We can use structure charts or module specifications

Structure Chart for Top-Down Analysis

Main Task

Subtask 1 Subtask 2 Subtask 3

Structure Chart for The Checkbook Problem

Update checkbook

GetInformation

PerformComputations

DisplayResults

Second-Level Refinement

Update checkbook

GetInformation

PerformComputations

DisplayResults

GetStartingBalance

GetTransaction

Type

GetTransaction

Amount

Analysis: Specify Module, Task,Inputs, and Outputs

Module: Get information

Task: Have the user enter information from the keyboard

Outputs: starting balance transaction type transaction amount

Analysis: Specify Module, Task,Inputs, and Outputs

Module: Perform computations

Task: If the transaction is a deposit add it to the balance else subtract it from the balance

Inputs: starting balance transaction type transaction amount

Outputs: ending balance

Analysis: Specify Module, Task,Inputs, and Outputs

Module: Display results

Task: Display the results in readable form

Inputs: starting balance transaction type transaction amount ending balance

Design

• Receives the results of analysis

• Describes how a module accomplishes its solution

• Concern is with logic

Pseudocode

• Pseudocode is a high-level, non-executable notation for describing designs

• Pseudocode language forms resemble those of most programming languages but are easier to read

• Concern is with logic, not syntax

Design: Pseudocode forthe Top-Level Task

1. Get information2. Perform computations3. Display results

Second-Level Refinement

1. Get information 1.1 get starting balance 1.2 get transaction type 1.3 get transaction amount2. Perform computations 2.1 if deposit then add amount to balance else subtract amount from balance3. Display results 3.1 display starting balance 3.2 display transaction type 3.3 display ending balance

Implementation (Coding)

• Receives the results of design

• One design can be coded in many different programming languages

• Concern is with syntax or correct form of code

Steps in Coding

Edit theprogram

Compile theprogram

Run theprogram

Syntax errors

Run-time andlogic errors

The Parts of a C++ Program

• Preprocessor directives (to include libraries)

• Main function– data declarations– program statements

Program Comments

// Program file: chbook.cpp

// This program updates a checkbook.

Not executable, but describes the task of the program for the reader.

Preprocessor Directives

// Program file: chbook.cpp

// This program updates a checkbook.

#include <iostream.h>#include <iomanip.h>

Ask the compiler to include library code for the program.

These libraries contain code for input and outputoperations.

The main Function

// Program file: chbook.cpp

// This program updates a checkbook.

#include <iostream.h>#include <iomanip.h>

int main(){ return 0;}

This program actuallydoes nothing, but we’llinclude the code forthe checkbook program shortly.

Declare Data Variables

int main(){ double startingBalance, endingBalance, transAmount; char transType;

Each data variable has a name and a type.

Type double is for numbers with a decimal point.

Type char is for letters and other keyboard characters.

Declare Data Variables

int main(){ double startingBalance, endingBalance, transAmount; char transType;

C++ is case sensitive. TransType is a different name thantransType.

Use names that describe roles of data in the program.

Code the Module for Getting Data

int main(){ double startingBalance, endingBalance, transAmount; char transType; // Module for getting the data.

cout << "Enter the starting balance and press <Enter>: "; cin >> startingBalance; cout << "Enter the transaction type (D) deposit or (W) withdrawal "; cout << endl << "and press <Enter>: "; cin >> transType; cout << "Enter the transaction amount and press <Enter>: "; cin >> transAmount;

Don’t worry about understanding all these statements;we will cover them in detail shortly.

Code the Module for Performing Computations

int main(){ double startingBalance, endingBalance, transAmount; char transType; // Module for getting the data (now hidden)

// Module for performing computations.

if (transType == 'D') endingBalance = startingBalance + transAmount; else endingBalance = startingBalance - transAmount;

Code the Module for Displaying Results

int main(){ double startingBalance, endingBalance, transAmount; char transType; // Module for getting the data (now hidden)

// Module for performing computations (now hidden)

// Module for displaying results.

cout << setiosflags(ios::fixed | ios::showpoint | ios::right) << setprecision(2);

cout << endl; cout << "Starting balance $" << startingBalance << endl; cout << "Transaction $" << transAmount << " " << transType << endl; cout << "Ending balance $" << endingBalance << endl; return 0;

}

Testing

• Receives the results of coding

• Must check to see that the program produces the expected output for any given input

• Cannot be exhaustive; must use typical inputs and limiting cases (such as 0)

Recommended