Programming and Problem Solving — Software Engineering
description
Transcript of Programming and Problem Solving — Software Engineering
1
Programming and Programming and Problem Solving —Problem Solving —
Software EngineeringSoftware Engineering(Read Chap. 2)
Problem SolvingProblem Solving
5 Phases of (Simplified) Software Life Cycle:
• Problem Analysis and Specification• Design• Implementation (Coding)• Testing, Execution and Debugging• Maintenance
OCD (Object-Centered Design)
A Temperature-conversion problemA Temperature-conversion problem::
Write a program that, given a temperature in Write a program that, given a temperature in Celsius, displays that temperature in Fahrenheit. Celsius, displays that temperature in Fahrenheit.
OCD (Object-Centered OCD (Object-Centered Design)Design)
1.1. Describe the _____________ of the program.Describe the _____________ of the program.
2.2. Identify the problem’s ____________Identify the problem’s ____________ and and make a table of information about them.make a table of information about them.
3.3. Identify the problem’s _________________ Identify the problem’s _________________ and make a table of information about and make a table of information about them.them.
4.4. Organize the objects and operations into a Organize the objects and operations into a sequence of steps, called a sequence of steps, called a _________________, _________________, to solve the problem.to solve the problem.
3
BehaviorBehaviorUsingOCD
A. Describe the desired behavior of the A. Describe the desired behavior of the program:program:
Our program should display program information Our program should display program information to the user and then a prompt for the to the user and then a prompt for the Celsius temperature on the screen, read thatCelsius temperature on the screen, read thattemperature from the keyboard, compute thetemperature from the keyboard, compute thecorresponding Fahrenheit temperature, and corresponding Fahrenheit temperature, and display the result, along with a descriptive display the result, along with a descriptive label on the screen.label on the screen.
These make up the ____________ in our These make up the ____________ in our problem.problem.
5
Problem ObjectsProblem ObjectsB. Identify the B. Identify the nounsnouns in the behavioral description (other than in the behavioral description (other than
"non-behavioral" ones like program and user):"non-behavioral" ones like program and user):
Our program should display program informationOur program should display program information
to the user and then a prompt for the to the user and then a prompt for the
Celsius temperature on the screen, read thatCelsius temperature on the screen, read that
temperature from the keyboard, compute thetemperature from the keyboard, compute the
corresponding Fahrenheit temperature, and corresponding Fahrenheit temperature, and
display that temperature, along with a descriptive display that temperature, along with a descriptive
label on the screen.label on the screen.
UsingOCD
Information about ObjectsInformation about ObjectsDetermine a _______and a ________ (if necessary) for each object Determine a _______and a ________ (if necessary) for each object
and whether it is a ____________ or a ______________.and whether it is a ____________ or a ______________.
6
UsingOCD
cout
cin
Problem Object Type Kind Name
program information text
constant
none
prompt text
constant
none
Celsius temperature real number
variable
celsius
screen output
variable
keyboard input
variable
Fahrenheit temperature
real number
variable
fahrenheit
label text
constant
none
C++ Type
?
?
double
?
?
double
?
OperationsOperations
These make up the ______________ in our These make up the ______________ in our problem.problem. 7
C. Identify the C. Identify the verbsverbs in the behavioral description: in the behavioral description:
Our program should display program informationOur program should display program information
to the user and then a prompt for the to the user and then a prompt for the
Celsius temperature on the screen, read thatCelsius temperature on the screen, read that
temperature from the keyboard, compute thetemperature from the keyboard, compute the
corresponding Fahrenheit temperature, and corresponding Fahrenheit temperature, and
display that temperature, along with a descriptive display that temperature, along with a descriptive
label on the screen.label on the screen.
UsingOCD
Information about Information about OperationsOperations
Identify the C++ ____________ to perform Identify the C++ ____________ to perform a given operation, if there is one.a given operation, if there is one.
8
To compute the Fahrenheit temperature, we need to
know/find the Celsius to Fahrenheit conversion formula.
UsingOCD
OperationOperationPre-Pre-defineddefined??
C++ C++ OperatoOperato
rrLibraryLibrary
DisplayDisplay yes yes << iostream
ReadRead yes yes >> iostream
Compute the Fahrenheit Compute the Fahrenheit temperaturetemperature
?? ?? ???? ????
Celsius to FahrenheitCelsius to Fahrenheit
Formula for converting Celsius to Formula for converting Celsius to Fahrenheit:Fahrenheit:
fahrenheit = 1.8 celsius + 32
Converting the temperature thus adds Converting the temperature thus adds new objects and operations to our new objects and operations to our problem.problem.
9
UsingOCD
10
Information about Objects Information about Objects (Revised)(Revised)
UsingOCD
Problem Object Type Kind C++ Type Name
program information text
constant
? none
prompt text
constant
? none
Celsius temperature realnumber
variable
double celsius
screen output
variable
? cout
keyboard input
variable
? cin
Fahrenheit temperature
realnumber
variable
double fahrenheit
label text
constant
? none
conversionfactor 1.8
real number
constant
double none
conversion factor 32
integer constant
int none
11
UsingOCD Information about Information about
Operations (Revised)Operations (Revised)
OperationPre-defined?
C++ Operato
rLibrary
Display yes << iostreamRead yes >> iostreamCompute the Fahrenheit temperature:Multiply two real values (1.8 and celsius)
yes * built-in
Add a real value (the result above) and an integer (32)
yes + built-in
Store a real value (the result above) in a variable
yes = built-in
AlgorithmAlgorithm
12
UsingOCD
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 __________________.
1. Display via cout information about the program to the user.
2. Display via cout a prompt for the Celsius temperature.
3. Read the temperature from cin.
4. Compute the Fahrenheit temperature from the Celsius temperature.
5. Display via cout the Fahrenheit temperature and an informative labell..
CodingCoding
Once we have designed an algorithm, the next step is to translate that algorithm into a high level language like C++.
This involves writing instructions to
— represent the objects, and
— perform the operations
in C++.
13
14
/* temperature.cpp converts a Celsius temperature to Fahrenheit. John Doe Lab 1 Jan. 5, 2012 CS 104X Input: A Celsius temperature Output: Corresponding Fahrenheit temperature-----------------------------------------------*/
#include <iostream> // cin, cout, <<, >>using namespace std;
int main(){
// 1. Display via cout information about the // program to the user.
// 2. Display via cout a prompt for the Celsius // temperature.
// 3. Read the temperature from cin.
// 4. Compute the Fahrenheit temperature from the // Celsius temperature.
// 5. Display via cout the Fahrenheit temperature // and an informative label. }
When learning to program, it is helpful to just start with the algorithm as comments in
main()
The Code
cout << "John Doe CS 104X -- Lab 1\n\n"; cout << "** Convert Celsius temps to Fahrenheit **\n";
cout << "Please enter a temperature in Celsius: ";15
/* temperature.cpp converts a Celsius temperature to Fahrenheit. John Doe Lab 1 Jan. 5, 2012 CS 104X Input: A Celsius temperature Output: Corresponding Fahrenheit temperature-----------------------------------------------*/
#include <iostream> // cin, cout, <<, >>using namespace std;
int main(){ // 1. Display via cout information about the // program to the user.
// 2. Display via cout a prompt for the Celsius // temperature.
The Code
double celsius; cin >> celsius;
double fahrenheit = 1.8 * celsius + 32;
cout << celsius << " degrees Celsius is " << fahrenheit << " degrees Fahrenheit.\n";
16
// 3. Read the temperature from cin.
// 4. Compute the Fahrenheit temperature from the // Celsius temperature.
// 5. Display via cout the Fahrenheit temperature // and an informative label.
}
The Code
It’s wise to echo input data to insure computer read what you intended.
17
Comments
/* temperature.cpp converts a Celsius temperature to Fahrenheit. John Doe Lab 1 Jan. 5, 2012 CS 104X Input: A Celsius temperature Output: Corresponding Fahrenheit temperature-----------------------------------------------*/
#include <iostream> // cin, cout, <<, >>using namespace std;
int main(){ // 1. Display via cout information about the // program to the user. cout << "John Doe CS 104X -- Lab 1\n\n"; cout << "** Convert Celsius temps to Fahrenheit **\n";
// 2. Display via cout a prompt for the Celsius // temperature. cout << "Please enter a temperature in Celsius: ";
Always begin a program with
opening documentation enclosed in /* and */.
/* temperature.cpp converts a Celsius temperature to Fahrenheit. John Doe Lab 1 Jan. 5, 2012 CS 104X Input: A Celsius temperature Output: Corresponding Fahrenheit temperature-----------------------------------------------*/
#include <iostream> // cin, cout, <<, >>using namespace std;
int main(){ // 1. Display via cout information to the user. cout << "John Doe CS 104X -- Lab 0\n\n"; cout << "** Convert Celsius temps to Fahrenheit **\n";
// 2. Display via cout a prompt for the Celsius // temperature. cout << "Please enter a temperature in Celsius: ";
18
LibrariesThis loads the C++ library that we need.
Lab & Proj. 1: #include <cmath>
19
/* temperature.cpp converts a Celsius temperature to Fahrenheit. John Doe Lab 1 Jan. 5, 2012 CS 104X Input: A Celsius temperature Output: Corresponding Fahrenheit temperature-----------------------------------------------*/
#include <iostream> // cin, cout, <<, >>using namespace std;
int main(){ // 1. Display via cout information about the // program to the user. cout << "John Doe CS 104X -- Lab 1\n\n"; cout << "** Convert Celsius temps to Fahrenheit **\n";
// 2. Display via cout a prompt for the Celsius // temperature. cout << "Please enter a temperature in Celsius: ";
Each step of the
algorithm is implemented
by one or more C++
program statementsinside main() function.
20
// 3. Read the temperature from cin. double celsius; cin >> celsius;
// 4. Compute the Fahrenheit temperature from the // Celsius temperature. double fahrenheit = 1.8 * celsius + 32;
// 5. Display via cout the Fahrenheit temperature // and an informative label. cout << celsius << " degrees Celsius is " << fahrenheit << " degrees Fahrenheit.\n";}
Always a goodidea to
echoinput data
Note spacing, indentation, & alignment to make program “look nice” and easier to read.
This will be one criterion used in grading.
Each step of the
algorithm is implemented
by one or more C++
program statementsinside main() function.
TestingTestingRun your program using sample data (whose correctness is easy to check):
John Doe CS 104X -- Lab 1
** Convert Celsius temps to Fahrenheit **Please enter the temperature in Celsius: 00 degrees Celsius is 32 degrees Fahrenheit.
21
John Doe CS 104X -- Lab 1
** Convert Celsius temps to Fahrenheit **Please enter the temperature in Celsius: 100100 degrees Celsius is 212 degrees Fahrenheit.
When you are convinced that the program is correct, run it with the required data values.
John Doe CS 104X -- Lab 1
** Convert Celsius temps to Fahrenheit **Please enter the temperature in Celsius: -17.78-17.78 degrees Celsius is -0.004 degrees Fahrenheit.
22
23
For a programming assignment: Lose a few points or may be lucky and the grader doesn’t catch it.
For a real-world problem: Much more may be at stake: money, jobs, and even lives.
Why Testing is Important:
• September,1999: Mars Climate Orbiter
• June, 1996: Ariane 5 rocket
• March,1991: DSC Communications
• February 25, 1991(Gulf War): Patriot missileTesting is never finished; it is only stopped.
Testing can only show the presence of errors, not their absence.
SeeOther Course Information
(CS 104 page):• Importance of
Program Testing — Horror Stories
MaintenanceMaintenance
24
• Large % of computer center budgets
• Large % of programmer's time
• Largest % of software development cost
Why?
Poor structure, poor documentation, poor style
less likely to catch bugs before release
fixing of bugs difficult and time-consuming
impede implementation of enhancements
SeeOther Course Information
(CS 104 page):• Time Spent on
Program Maintenance