1 Programming Style Files Lecture 1 Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by Reneta Barneva, SUNY-Fredonia.

Slides:



Advertisements
Similar presentations
C++ Plus Data Structures
Advertisements

1 Lecture 10 Chapter 7 Functions Dale/Weems/Headington.
Important Definitions Class: A structured data type that is used to represent an abstract data type (ADT) Class member: A components of a class. It can.
1 Lecture 19 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
1 Chapter 7 Functions Dale/Weems/Headington. 2 Functions l Control structures l every C++ program must have a function called main l program execution.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
Chapter 1 Software Engineering. Homework ► Read Section 2.2 (pages 79-98) ► Answer questions: ► 7, 8, 11, 12, & 13 on page 134. ► Answer on paper, hand.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
SW Engineering. 2 Goals Describe the general activities in the software life cycle Describe the goals for "quality" software Explain the following terms:
Program Input and the Software Design Process ROBERT REAVES.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate.
1 Chapter 7 Functions. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task l Using Function Arguments.
C++ Vs. Java Who will win?. Look Ma, no classes! C++ was conceived as an object-oriented extension to C C was (is) purely procedural language –Functions.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.
1 Programming in C++ Dale/Weems/Headington Chapter 7 Functions.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
1 Chapter 4 Program Input and the Software Design Process.
1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design.
1 Chapter 4 Program Input and the Software Design Process.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Chapter 4 Program Input and the Software Design Process CS185/09 - Introduction to Programming Caldwell College.
1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles Modified from the Slides made by Sylvia Sorkin, Community College of Baltimore.
Chapter 1 Software Engineering Principles. Lecture 2.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
1 Functions every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and.
Chapter 8 Functions. Chapter 8 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task l Using Function Arguments.
1 Functions. 2 Chapter 7 Topics  Writing a Program Using Functional Decomposition  Writing a Void Function for a Task  Using Function Arguments and.
Programming Life Cycle Problem analysisunderstand the problem Requirements definition specify what program will do High- and low-level designhow it meets.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task.
Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems.
No I/O is built into C++ l instead, a library provides input stream and output stream KeyboardScreen executing program istreamostream 1.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
1 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Program Input and the Software Design Process. 2 Chapter 4 Topics  Input Statements to Read Values for a Program using >>, and functions get, ignore,
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
CSI 1340 Introduction to Computer Science II Chapter 1 Software Engineering Principles.
Chapter 4 Program Input and the Software Design Process.
Chapter 1 Data Abstraction: The Walls CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Chapter 1 Software Engineering Principles. Problem analysis Requirements elicitation Software specification High- and low-level design Implementation.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
1 Chapter 4 Program Input and the Software Design Process.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Chapter 4 Program Input and the Software Design Process
Chapter 8 Functions.
C++ Plus Data Structures
Chapter 1 Data Abstraction: The Walls
Chapter 1 Software Engineering Principles
File I/O.
Chapter 1 Software Engineering.
Lecture 5A File processing Richard Gesick.
Introduction to C++ Programming
Programming Funamental slides
Programming Funamental slides
Chapter 3: Input/Output
No I/O is built into C++ instead, a library provides input stream and output stream Keyboard Screen executing program istream ostream 1.
Chapter 3 Input output.
Chapter 4 Program Input and the Software Design Process
Chapter 8 Functions.
Dale/Weems/Headington Program Input and the Software Design Process
Presentation transcript:

1 Programming Style Files Lecture 1 Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by Reneta Barneva, SUNY-Fredonia

2 Abstraction l A model of a complex system that includes only the details essential to the perspective of the viewer of the system.

3 Information Hiding l Hiding the details of a function or data structure with the goal of controlling access to the details of a module or structure. PURPOSE: To prevent high-level designs from depending on low-level design details that may be changed.

4 Two Approaches to Building Manageable Modules Divides the problem into more easily handled subtasks, until the functional modules (subproblems) can be coded. Identifies various objects composed of data and operations, that can be used together to solve the problem. FUNCTIONAL DECOMPOSITION OBJECT-ORIENTED DESIGN FOCUS ON: processes FOCUS ON: data objects

5 Find Weighted Average Print Weighted Average Functional Design Modules Main Print Data Print Heading Get Data Prepare File for Reading

6 Object-Oriented Design A technique for developing a program in which the solution is expressed in terms of objects -- self- contained entities composed of data and operations on that data. Private data << setf Private data >> get ignore cincout

7 More about OOD l Languages supporting OOD include: C++, Java, Smalltalk, Eiffel, and Object-Pascal. l A class is a programmer-defined data type and objects are variables of that type. l In C++, cin is an object of a data type (class) named istream, and cout is an object of a class ostream. Header files iostream.h and fstream.h contain definitions of stream classes.

8 Procedural vs. Object-Oriented Code “Read the specification of the software you want to build. Underline the verbs if you are after procedural code, the nouns if you aim for an object-oriented program.” Brady Gooch, “What is and Isn’t Object Oriented Design,” 1989.

9 l Program Verification is the process of determining the degree to which a software product fulfills its specifications. Program Verification PROGRAM SPECIFICATIONS Inputs Outputs Processing Requirements Assumptions

10 Verification vs. Validation Program verification asks, “Are we doing the job right?” Program validation asks, “Are we doing the right job?” B. W. Boehm, Software Engineering Economics, 1981.

11 Program Testing l Testing is the process of executing a program with various data sets designed to discover errors. DATA SET 1 DATA SET 2 DATA SET 3 DATA SET 4...

12 Various Types of Errors l Design errors occur when specifications are wrong l Compile errors occur when syntax is wrong l Run-time errors result from incorrect assumptions, incomplete understanding of the programming language, or unanticipated user errors.

13 Robustness l Robustness is the ability of a program to recover following an error; the ability of a program to continue to operate within its environment.

14 An Assertion l Is a logical proposition that is either true or false (not necessarily in C++ code). EXAMPLES studentCount is greater than 0 sum is assigned && count > 0 response has value ‘y’ or ‘n’ partNumber == 5467

15 Preconditions and Postconditions l The precondition is an assertion describing what a function requires to be true before beginning execution. l The postcondition describes what must be true at the moment the function finishes execution. l The caller is responsible for ensuring the precondition, and the function code must ensure the postcondition. FOR EXAMPLE...

16 void PrintList ( ofstream& dataFile, UnsortedType list) // Pre: list has been initialized. // dataFile is open for writing. // Post: Each component in list has been written to dataFile. // dataFile is still open. { int length; ItemType item; list.ResetList(); length = list.LengthIs(); for (int counter = 1; counter <= length; counter++) { list.GetNextItem(item); item.Print(dataFile); } 16 An Example

17 Another Example void GetRoots (float a, float b, float c, float& Root1, float& Root2 ) // Pre: a, b, and c are assigned. // a is non-zero, b*b - 4*a*c is non-zero. // Post: Root1 and Root2 are assigned // Root1 and Root2 are roots of quadratic with coefficients a, b, c { float temp; temp = b * b * a * c; Root1 = (-b + sqrt(temp) ) / ( 2.0 * a ); Root2 = (-b - sqrt(temp) ) / ( 2.0 * a ); return; }

18 A Walk-Through l Is a verification method using a team to perform a manual simulation of the program or design, using sample test inputs, and keeping track of the program’s data by hand. l Its purpose is to stimulate discussion about the programmer’s design or implementation.

19 Tasks within each test case: l determine inputs that demonstrate the goal. l determine the expected behavior for the input. l run the program and observe results. l compare expected behavior and actual behavior. If they differ, we begin debugging.

20 Integration Testing l Is performed to integrate program modules that have already been independently unit tested. Find Weighted Average Print Weighted Average Main Print Data Print Heading Get Data Prepare File for Reading

21 Integration Testing Approaches Ensures correct overall design logic. Ensures individual modules work together correctly, beginning with the lowest level. TOP-DOWN BOTTOM-UP USES: placeholder USES: a test driver to call module “stubs” to test the functions being tested. the order of calls.

22 Files

23 Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing program input data output data

24 is header file l for a library that defines 3 objects l an istream object named cin (keyboard) l an ostream object named cout (screen) l an ostream object named cerr (screen)

25 Insertion Operator ( << ) l The insertion operator << takes 2 operands. l The left operand is a stream expression, such as cout. l The right operand is an expression describing what to insert into the output stream. It may be of simple type, or a string, or a manipulator (like endl).

26 Extraction Operator ( >> ) l Variable cin is predefined to denote an input stream from the standard input device ( the keyboard ). l The extraction operator >> called “get from” takes 2 operands. The left operand is a stream expression, such as cin. The right operand is a variable of simple type. l Operator >> attempts to extract the next item from the input stream and store its value in the right operand variable.

27 Extraction Operator >> “skips” (reads but does not store anywhere) leading whitespace characters (blank, tab, line feed, form feed, carriage return) before extracting the input value from the stream (keyboard or file). To avoid skipping, use function get to read the next character in the input stream. cin.get(inputChar);

#include int main( ) { // USES KEYBOARD AND SCREEN I/O int partNumber; float unitPrice; cout << “Enter part number followed by return : “ << endl ; // prompt cin >> partNumber ; cout << “Enter unit price followed by return : “ << endl ; cin >> unitPrice ; cout << “Part # “ << partNumber // echo << “at Unit Cost: $ “ << unitPrice << endl ; return 0; } 28

29 Disk files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “A:\myInfile.dat” disk file “A:\myOut.dat” executing program input dataoutput data #include

30 For File I/O l use #include l choose valid variable identifiers for your files and declare them l open the files and associate them with disk names l use your variable identifiers with >> and << l close the files

31 Statements for using file I/O #include ifstream myInfile; // declarations ofstream myOutfile; myInfile.open(“A:\\myIn.dat”);// open files myOutfile.open(“A:\\myOut.dat”); myInfile.close( );// close files myOutfile.close( );

32 What does opening a file do? l associates the C++ identifier for your file with the physical (disk) name for the file l if the input file does not exist on disk, open is not successful l if the output file does not exist on disk, a new file with that name is created l if the output file already exists, it is erased l places a file reading marker at the very beginning of the file, pointing to the first character in it

#include int main( ) {// USES FILE I/O int partNumber; float unitPrice; ifstreaminFile;// declare file variables ofstreamoutFile; inFile.open(“input.dat”);//open files outFile.open(“output.dat”); inFile >> partNumber ; inFile >> unitPrice ; outFile << “Part # “ << partNumber // echo << “at Unit Cost: $ “ << unitPrice << endl ; return 0; } 33

34 Stream Failure l When a stream enters the fail state, further I/O operations using that stream are ignored. But the computer does not automatically halt the program or give any error message. l Possible reasons for entering fail state include: invalid input data (often the wrong type), opening an input file that doesn’t exist, opening an output file on a diskette that is already full or is write-protected.

#include int main( ) {// CHECKS FOR STREAM FAIL STATE ifstreaminFile; inFile.open(“input.dat”);// try to open file if ( !inFile ) { cout << “File input.dat could not be opened.”; return 1; }... return 0; } 35