370 likes | 450 Views
Problem Solving and Software Engineering. Chapter 1. Objectives. Indicate uses of computers First look at a C++ program Basic phases of software life cycle Object-centered design Issues, ethical principles First look: classes and object-oriented design. Uses of Computers. Industry
E N D
Problem Solving and Software Engineering Chapter 1
Objectives • Indicate uses of computers • First look at a C++ program • Basic phases of software life cycle • Object-centered design • Issues, ethical principles • First look: classes and object-oriented design C++, An Introduction to Computing, 3rd Ed.
Uses of Computers • Industry • Robots, CAD, project management and control • Government • Defense, space, compile data, weather • Medicine • Medical records, life support systems, CAT scan, MR scan • Entertainment • Animation, colorization, special effects • Science • Simulations, molecule analysis, food quality testing • Information Technology • Digital libraries, online art C++, An Introduction to Computing, 3rd Ed.
What is Programming? • Computer Program • A sequence of statements that instruct a computer in how to solve a problem. • Programming • The act of designing, writing and maintaining a program • Programmers • People who write programs C++, An Introduction to Computing, 3rd Ed.
Comment Compilerdirectives Specifies standard related names Main portion of program.Contains C++ statements. The World of C++ • Sample program /* greeting.cpp greets its user. * * Input: The name of the user * Output: A personalized greeting *********************************************************/ #include <iostream> // cin, cout, <<, >> #include <string> // string using namespace std; int main() { cout << "Please enter your first name: "; string firstName; cin >> firstName; cout << "\nWelcome to the world of C++, " << firstName << "!\n"; } C++, An Introduction to Computing, 3rd Ed.
Output statement to prompt user Variable declaration Input from keyboard stored in variable Output character string and value stored in variable The World of C++ • C++ statements int main() { cout << "Please enter your first name: "; string firstName; cin >> firstName; cout << "\nWelcome to the world of C++, " << firstName << "!\n"; } C++, An Introduction to Computing, 3rd Ed.
Problem Solving through Software Engineering Phases • Design • Analysis, specify algorithms to solve problem • Coding • Write solution in syntax of language • Testing, Execution, Debugging • Get rid of “bugs” • Maintenance • Update, modify to meet changing needs C++, An Introduction to Computing, 3rd Ed.
Problem • World’s largest ball of twine found in Cawker City, Ks. • How much does the ball weigh? • How many mileswould the twinereach ifunrolled? C++, An Introduction to Computing, 3rd Ed.
Object-Centered Design Steps • State how you want the program to behave • Identify real-world objects, categorize • Identify operations needed to solve the problem • Develop algorithm – arrange objects, operations in an order which solve the problem C++, An Introduction to Computing, 3rd Ed.
Behavior To find the weight of a ball of string:Enter radius of sphere : 9 Now computing . . . Weight of ball of string = 999.99 C++, An Introduction to Computing, 3rd Ed.
Objects C++, An Introduction to Computing, 3rd Ed.
Operations • Output prompt for radius of sphere to cout • Input real value from cin • Store it in radius • Compute weight • Output weight to cout C++, An Introduction to Computing, 3rd Ed.
More Objects • Computation of weight requires additional objects C++, An Introduction to Computing, 3rd Ed.
Algorithm • Initialize constant PI • Output prompt for radius to cout • Input real value from cin, store in radius • Output prompt for density to cout • Input real value from cin, store in density • Compute • Output weight to cout C++, An Introduction to Computing, 3rd Ed.
Coding • First, create a program stub that contains opening documentation • Compiler directives that add items in libraries needed for some of the • Objects and operations • An empty main function • Convert each step of the algorithm into code. • If it uses a software object that hasn’t already been declared, add a declaration statement that specifies the object’s type and name. C++, An Introduction to Computing, 3rd Ed.
Coding /* sphereWeight.cpp computes the weight of a sphere. * * Input: The radius (feet) and * the density (pounds/cubic foot) of a sphere * Output: The weight of the sphere (pounds) ************************************************/ #include <iostream> // cin, cout, <<, >> #include <cmath> // pow() using namespace std; int main() { } C++, An Introduction to Computing, 3rd Ed.
Coding int main() { const double PI = 3.14159; double radius; // double radius, double density; // density, double weight; // weight; // INPUT DATA cout << "Enter the sphere's radius (feet): "; cin >> radius; cout << "Enter its density (pounds/cubic feet): "; cin >> density; // CALCULATE WEIGHT weight = density * 4.0 * PI * pow(radius, 3) / 3.0; // OUPUT WEIGHT cout << "\nThe weight of the sphere is approximately " << weight << " pounds.\n"; } C++, An Introduction to Computing, 3rd Ed.
Testing Enter radius of sphere (feet) : 6.5 Enter its density (pounds/cubic feet) : 14.6 The weight of the sphere is approximately 16795 pounds C++, An Introduction to Computing, 3rd Ed.
Testing, Execution, Debugging Common error sources • Violations of grammar rules of the high level language • Errors that occur during execution • Errors in the design of the algorithm C++, An Introduction to Computing, 3rd Ed.
Syntax Errors • Example:double radius • Missing semi-colon • Compiler finds most of these kinds of errors • Different compilers give varying degrees of helpful diagnostics C++, An Introduction to Computing, 3rd Ed.
Run Time Errors • Not detected until program runs • Examples • Division by zero causes program to crash • Taking square root of negative causes program crash • Program must be modified to keep such events from happening C++, An Introduction to Computing, 3rd Ed.
Logic Errors • Program compiles, runs without crashing, but gives incorrect results • These are hardest errors to find • Find by using sample data and hand calculating the correct results, comparing • Note: Testing grows increasingly more difficult with larger programs • Some will run for years without logic error appearing C++, An Introduction to Computing, 3rd Ed.
Maintenance • Student programs run only a few times • Real-world programs used for many years • Due to significant investment of resources • New features may be required during life of program usage • Upgrading called “maintenance” C++, An Introduction to Computing, 3rd Ed.
Problem Session Working in groups of two, Solve the following problem...
Problem Sam Splicer installs coax cable for Metro Cable Co. • Basic service charge $25.00 • Additional $2.00 for each foot of cable • Company Pres. wants to compute revenue generated by Sam for given month • Example:263 yards of cable at 27 locations generates $2253.00 in revenue C++, An Introduction to Computing, 3rd Ed.
Describe Behavior of Program • Program should display prompt for number of installations performed and total number of yards of cable installed • User enters values from keyboard • Program computes, displays on screen total amount of revenue resulting from these installations C++, An Introduction to Computing, 3rd Ed.
Behavior Envisioned To determine revenue generated, Enter number of installations : 27 Enter number of yards of cable installed : 263 Total revenue generated is $2253.00 C++, An Introduction to Computing, 3rd Ed.
Objects • Use description to fill in chart for objects. C++, An Introduction to Computing, 3rd Ed.
Operations • Use description to fill in chart for operations. C++, An Introduction to Computing, 3rd Ed.
Algorithm and Coding • Work together to determine the steps necessary to have the objects manipulated by the operations • Write the source code • Compile and Link • Test the program C++, An Introduction to Computing, 3rd Ed.
OBJECTive ThinkingSpheres as Objects • Recall behavioral description concerning the sphere of string: • Then note the list of objects: • No mention made of sphere objects • Only used sphere attributes • We ignored the central noun … sphere Display prompt for radius. User enters value. Program computes weight, displays it on screen C++, An Introduction to Computing, 3rd Ed.
Creating a New Type : Sphere • When no type for an object exists, we create a type • Called a class • A class provides • Space for storing the attributes of an object • Operations for manipulation of the object C++, An Introduction to Computing, 3rd Ed.
Operations for Sphere Class • Initialize attributes to default values • Read in various values for attributes • Print various attributes • Retrieve various attribute values for use by other routines C++, An Introduction to Computing, 3rd Ed.
File with Sphere declaration Declaration of Sphere object Sphere object sends message to operation Code Example Using Sphere Object #include <iostream> // cin, cout, <<, >> #include "Sphere.h" // Sphere class using namespace std; int main() { cout << "Enter the radius (feet) " << " and density (lbs/sq-ft) of the sphere: "; Sphere aSphere; aSphere.readRadiusAndDensity(cin); cout << "\nThe sphere weighs " << aSphere.getWeight() << " pounds.\n"; } C++, An Introduction to Computing, 3rd Ed.
Calculating Sphere Density using Sphere Class #include <iostream> // cin, cout, <<, >> #include "Sphere.h" // Sphere class using namespace std; int main() { cout << "Enter the radius (feet) " << " and weight (lbs) of the sphere: "; Sphere aSphere; aSphere.readRadiusAndWeight(cin); cout << "\nThe sphere's density is " << aSphere.getDensity() << " lbs/sq-ft\n"; } C++, An Introduction to Computing, 3rd Ed.
Ethics and Issues “To be good is noble, but to show others how to be good is nobler, and no trouble.”Mark Twain • Ethics are becoming more of an issue in the area of computers. • Professional organizations are adopting, instituting codes of ethics • Colleges and universities have policies governing responsible uses of computers C++, An Introduction to Computing, 3rd Ed.
Ethics and Issues • Consider the essay by Professor Anne Marchant (see web site of text) • Ethics and Society • Computer Crime and Security • Health Concerns and the Environment • Information Ownership • “Netiquette” and Hoaxes • Internet Content and Free Speech • Privacy • Quality Control and Risk Reduction • The Future C++, An Introduction to Computing, 3rd Ed.