Programming and Problem Solving — Software Engineering (Read Chap. 2)

advertisement
Programming and
Problem Solving —
Software Engineering
(Read Chap. 2)
1
Problem Solving
A Temperature-conversion problem:
Write a program that, given a temperature in Celsius,
displays that temperature in Fahrenheit.
5 Phases of (Simplified) Software Life Cycle:
• Problem Analysis and Specification
• Design
OCD (Object-Centered Design)
• Implementation (Coding)
• Testing, Execution and Debugging
• Maintenance
2
OCD (Object-Centered Design)
1. Describe the behavior of the program.
2. Identify the problem’s objects and make
a table of information about them.
3. Identify the problem’s operations and
make a table of information about them.
4. Organize the objects and operations into a
sequence of steps, called an algorithm,
to solve the problem.
3
Using
OCD
Behavior
A. Describe the desired behavior of the program:
Our program should display program information
to the user and then a prompt for the
Celsius temperature on the screen, read that
temperature from the keyboard, compute the
corresponding Fahrenheit temperature, and
display the result, along with a descriptive
label on the screen.
4
Using
OCD
Problem Objects
B. Identify the nouns in the behavioral description (other than
"non-behavioral" ones like "program" and "user"):
Our program should display program information
to the user and then a prompt for the
Celsius temperature on the screen, read that
temperature from the keyboard, compute the
corresponding Fahrenheit temperature, and
display that temperature, along with a descriptive
label on the screen.
These make up the objects in our problem.
5
Using
OCD
Information about Objects
Determine a type and a name (if necessary) for each
object and whether it is a constant or a variable.
Problem Object
Type
Kind
C++ Type
Name
program information
text
constant
?
none
prompt
text
constant
?
none
Celsius temperature
real
number
variable
double
screen
output
variable
?
cout
keyboard
input
variable
?
cin
Fahrenheit temperature real
number
label
text
variable
constant
double
?
celsius
fahrenheit
none
6
Using
OCD
Operations
C. Identify the verbs in the behavioral description:
Our program should display program information
to the user and then a prompt for the
Celsius temperature on the screen, read that
temperature from the keyboard, compute the
corresponding Fahrenheit temperature, and
display that temperature, along with a descriptive
label on the screen.
These make up the operations in our problem.
7
Using
OCD
Information about Operations
Identify the C++ operator to perform
a given operation, if there is one.
Operation
Predefined?
C++
Operator
Display
yes
<<
iostream
Read
yes
>>
iostream
Compute the Fahrenheit
temperature
??
??
??
Library
To compute the Fahrenheit temperature, we need to
know/find the Celsius to Fahrenheit conversion formula.
8
Using
OCD
Celsius-to-Fahrenheit
Formula for converting Celsius to Fahrenheit:
fahrenheit = 1.8 × celsius + 32
Converting the temperature thus adds new
objects and operations to our problem.
9
Using
OCD
Information about Objects
(Revised)
Problem Object
Type
Kind
C++ Type
Name
program information
text
constant
?
none
prompt
text
constant
?
none
Celsius temperature
real
number
variable
screen
output
variable
?
cout
keyboard
input
variable
?
cin
Fahrenheit temperature real
number
variable
double
double
celsius
fahrenheit
label
text
constant
?
none
conversion
factor 1.8
conversion
factor 32
real
number
constant
double
none
integer
constant
int
none
10
Using
OCD
Information about Operations
(Revised)
Operation
Predefined?
Display
yes
<<
iostream
Read
yes
>>
iostream
yes
*
built-in
yes
+
built-in
yes
=
built-in
Compute the Fahrenheit
temperature:
Multiply two real values
(1.8 and celsius)
Add a real value (the result
above) and an integer (32)
Store a real value (the
result above) in a variable
C++
Operator
Library
11
Using
OCD
Algorithm
D. Organize the objects and operations into a
sequence of steps that solves the problem,
called an algorithm.
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.
12
Coding
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
/* temperature.cpp converts a Celsius
temperature to Fahrenheit.
John Doe
CS 104X
Lab 1
The Code
Jan. 5, 2012
Input: A Celsius temperature
Output: Corresponding Fahrenheit temperature
-----------------------------------------------*/
#include <iostream>
using namespace std;
// cin, cout, <<, >>
int main()
{
} // 1. Display via cout information about the
//
program to the user.
When learning
to program, it
is helpful to
just start with
the algorithm
as comments
in main()
// 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.
14
/* temperature.cpp converts a Celsius
temperature to Fahrenheit.
John Doe
CS 104X
Lab 1
The Code
Jan. 5, 2012
Input: A Celsius temperature
Output: Corresponding Fahrenheit temperature
-----------------------------------------------*/
#include <iostream>
using namespace std;
// cin, cout, <<, >>
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: ";
15
The Code
// 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";
It’s wise to echo input
data to insure computer
read what you intended.
16
/* temperature.cpp converts a Celsius
temperature to Fahrenheit.
John Doe
CS 104X
Lab 1
Comments
Jan. 5, 2012
Input: A Celsius temperature
Output: Corresponding Fahrenheit temperature
-----------------------------------------------*/
#include <iostream>
using namespace std;
// cin, cout, <<, >>
Always begin
a program with
int main()
opening
{
documentation
// 1. Display via cout information about the enclosed in
//
program to the user.
/* and */.
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: ";
17
/* temperature.cpp converts a Celsius
temperature to Fahrenheit.
John Doe
CS 104X
Lab 1
Jan. 5, 2012
Input: A Celsius temperature
Output: Corresponding Fahrenheit temperature
-----------------------------------------------*/
#include <iostream>
using namespace std;
// cin, cout, <<, >>
int main()
{
// 1. Display via cout information about the
Libraries
//
program to the user.
loads the C++
cout << "John Doe CS 104X -- Lab This
1\n\n";
that we need.
cout << "** Convert Celsius temps library
to Fahrenheit
**\n";
& Proj.
#include <cmath>
// 2. Display via cout a promptLab
for
the 1:
Celsius
//
temperature.
cout << "Please enter a temperature in Celsius: ";
18
/* temperature.cpp converts a Celsius
temperature to Fahrenheit.
John Doe
CS 104X
Lab 1
Jan. 5, 2012
Each step of the
Input: A Celsius temperature algorithm is implemented
Output: Corresponding Fahrenheit temperature
by one or more C++
-----------------------------------------------*/
program statements
#include <iostream>
// cin, cout, <<, >>
inside main() function.
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
Why no \n ?
//
temperature.
cout << "Please enter a temperature in Celsius: ";
19
Each step of the
algorithm is implemented
by one or more C++
program statements
// 3. Read the temperature from cin.
inside main() function.
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.
Always
a good
cout << celsius << " degrees Celsius is "
idea to
echo
<< fahrenheit << " degrees Fahrenheit.\n";
input data
}
Note spacing, indentation, & alignment to make
program “look nice” and easier to read.
This will be one criterion used in grading.
20
Testing
Run 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: 0
0 degrees Celsius is 32 degrees Fahrenheit.
John Doe
CS 104X -- Lab 1
** Convert Celsius temps to Fahrenheit **
Please enter the temperature in Celsius: 100
100 degrees Celsius is 212 degrees Fahrenheit.
21
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
Why Testing is Important:
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.
•
•
•
•
September,1999: Mars Climate Orbiter
June, 1996: Ariane 5 rocket
March,1991: DSC Communications
February 25, 1991(Gulf War): Patriot missile
See
Other Course
Information
(CS 104 page):
• Importance of
Program Testing
— Horror Stories
Testing is never finished; it is only stopped.
Testing can only show the presence of errors,
not their absence.
23
Maintenance
• Large % of computer center budgets
• Large % of programmer's time
• Largest % of software development cost
Why?
See
Other Course
Information
(CS 104 page):
• Time Spent on
Program
Maintenance
Poor structure, poor documentation, poor style
⇒ less likely to catch bugs before release
⇒ fixing of bugs difficult and time-consuming
⇒ impede implementation of enhancements
24
Download