260 likes | 413 Views
CSS342: Introduction. Professor: Munehiro Fukuda. Introduction. Course Objectives. You will: Learn problem solving with object-oriented design Analyze algorithms in formal mathematical methods Exercise software engineering concepts You may say:
E N D
CSS342: Introduction Professor: Munehiro Fukuda CSS342: Introduction
Introduction Course Objectives • You will: • Learn problem solving with object-oriented design • Analyze algorithms in formal mathematical methods • Exercise software engineering concepts • You may say: • I know C++, templates, Big-O, sorting, recursion, lists, stacks, and queues. I read “for dummies” books. • However: • Can you swim, ski, and play tennis with only reading “swimming, skiing, and tennis for dummies”? • How: • Math/programming need exercises as sports do. • Solve many programming and mathematical problems. (7 assignments, 7 lab work, 4 math exercises, and 4 quizzes) CSS342: Introduction
Introduction Grading Read the class syllabus very carefully. CSS342: Introduction
Introduction Questions and Discussions • Professors’s email account: mfukuda@u.washington.edu • Mailing list: css342a_au13@u.washington.edu • Your email account: • Only your UW account can participate in the discussion group. • You can still email me from non-UW accounts for questions and appointments. • GoPost: • Visit the class syllabus under http://courses.washington.edu/css342/fukuda/ and click GoPost. CSS342: Introduction
Introduction Programming Environment • On-campus computing resources: • UW1-320: Linux Laboratory • UW1-310: Windows Laboratory • IDE: • You can use any IDE: Eclips, etc. • HOWEVER, make sure that your program runs on Linux. Don’t use Windows-specific system functions. • Visit for details: • http://courses.washington.edu/css342/fukuda/prog/assignment.html CSS342: Introduction
Introduction Today’s Topics • Software Engineering Principles • How to develop software • Namely, how to solve programming assignments in your case • Chapter 1: • Review arrays, pointers, and structures CSS342: Introduction
Software Engineering Life Cycle of Software Specification Maintenance Design Documentation Production Risk Analysis Refining Verification Testing Coding CSS342: Introduction
Software Engineering Specification • Clarify all aspects of the problem • What input data? Who will use it? What user interface? What level of error handling? Any special cases? What form of the output? • Example: • Given a list of names, write a program to search the list for a specific name. • Input data: a file containing a list of names and names to search for through keyboard input • Who: your instructor • User interface: ASCII-based • Error handling: an error message upon a wrong file name • Special cases: There may be two or more identical first names. • Output: if the name is found, print out its entire name, otherwise output “Not found”. CSS342: Introduction
Software Engineering Design • Modularity • Group cohesive parts into one well-defined module • Divide loosely-coupled parts into independent modules. • Design Specification of Each Module • Write its interface as a contract with other modules. • Do not describe the method/implementation of solution. • Include preconditions and postconditions. • Example: • Read a name list from a given input file. readFile( ) • Sort it. sort( ) • Repeat: while ( ) { • Input a new name. Cin >> ….; • Search for it. search( ) • Print out if it was found. Cout << ….; } CSS342: Introduction
Software Engineering Design Cont’d Example: Sort( anArray, num ) // Sorts an array into alphabetical order. // Precondition: anArray is an array of num strings // and 1 <= num <= MAX_ARRAY, where MAX_ARRAY // is a global constant that specifies the maximum size of // anArray. // Postcondition: anArray[0] <= anArray[1] <= … <= // anArray[num – 1], num is unchanged. CSS342: Introduction
Software Engineering Design Cont’d How about Classes? UML Class Diagrams UML Interaction Diagrams Bank :Customer bank:Bank -name:string=null -routingNum:int=-1 attribute authorize(name, identifier) +authorize( in name:string, in identifier:string ) -createAccount( ) OK operation 1 containment 0..* Account generalization (inheritance) Checking Savings CSS342: Introduction
Software Engineering Verification • Invariant: a condition that is always true at a particular point in an algorithm. • Using invariants, algorithm will contain fewer errors before you begin coding. • Example: // computes the sum of item[0], item[1], …, item[n-1] for any n >= 1 // Loop invariant: sum is the elements item[0] through item[j – 1]. int sum = 0; int j = 0; // 1: the invariant is true… sum = 0, item[0]..item[-1] while (j < n) { // 2: the invariant is true… sum is the same as case 1 or 3. sum += item[j]; ++j; // 3: the invariant is true… sum = item[0]+…+item[j-1] } // 4: the invariant is true and the loop always exits. CSS342: Introduction
Software Engineering Coding • Coding is a relatively minor phase in the software life cycle. • For any assignments, good coding style and appropriate comments reduce possible errors and makes modification easier. • Use descriptive variable names. • Use proper indentation in the loop and if-then interiors. • Insert blank lines between distinct functions, between code blocks, etc.. • Remove redundant code • Use functions for identifiable and recurring tasks. • Avoid tricky codes. • Adhere consistent coding style. (Otherwise, your assignment will be graded down.) CSS342: Introduction
Software Engineering Testing • Test individual modules first: • Methods, • Objects, • Functions, • Each portion of main, and finally • Entire runs of your program • If certain data must lie within a range, test with values at the endpoints of the range. • Example: • Test how your program behaves given a wrong file name • Test what output will be shown upon a wrong name • Test what if there are two or more identical last names • Test what if there are two ore more identical first/last names. CSS342: Introduction
Arrays, Pointers, and Structures Data Abstraction and Objects • Data Abstraction • A collection of data and a set of well-defined operations to the data can be reused without knowing the internal data representation and structure. • Objects • Instances of such abstracted data structure = the first-class objects • Then, how about the second-class objects? Information hiding Our main focus CSS342: Introduction
Arrays, Pointers, and Structures Discussion: Why are the ordinary arrays and C strings the second-class objects? • Arrays: • C strings: CSS342: Introduction
Arrays, Pointers, and Structures Using the C++ Standard vector For more info., visit http://www.cppreference.com/cppvector.html CSS342: Introduction
Arrays, Pointers, and Structures Using the C++ Standard string In C In C++ #include <string.h> #include <string> using namespace std; char *first = “Munehiro”; string first = “Munehiro”; char *last = “Fukuda”; string last = “Fukuda”; strcat( first, last ); first += last; Note: first.length( ) returns the string size. first.c_str( ) returns an ordinary C char string. For more info., visit http://www.cppreference.com/cppstring/ CSS342: Introduction
Arrays, Pointers, and Structures Call by Value, Reference, and Constant Reference Which of swap functions is appropriate? void swap(string a, string b) { string tmp = a; a = b; b = tmp; } void swap(string &a, string &b) { string tmp = a; a = b; b = tmp; } void swap(const string &a, const string &b) { string tmp = a; a = b; b = tmp; } Which of findMax functions is appropriate? int findMax(vector<int> a) { int max = a[0]; int i; for (i=1; i < a.size(); i++) if (a[i] > max) max = a[i]; return max; } int findMax(vector<int> &a) { int max = a[0]; int i; for (i=1; i < a.size(); i++) if (a[i] > max) max = a[i]; return max; } int findMax(const vector<int> &a) { int max = a[0]; int i; for (i=1; i < a.size(); i++) if (a[i] > max) max = a[i]; return max; } CSS342: Introduction
Arrays, Pointers, and Structures ? ? ? ? 6 ? 6 ? p p p p q q q q x x x x Pointers • Pointer variables int *p, *q; • Static allocation int x; • Address-of operator p = &x; • Memory cell to which P points *p = 6; • Pointer operations q = p; 4 3 1 5 CSS342: Introduction
Arrays, Pointers, and Structures ? p p p p ? q q q q Dynamic Allocation • Pointer declaration int *p, *q; • Dynamic allocation p = new int; q = new int; • Deallocation delete p; p = NULL; • Memory leak q = new int; 4 1 2 ? 3 NULL NULL Leak! ? ? ? ? CSS342: Introduction
Arrays, Pointers, and Structures Discussion: What is the difference between the following three programs? string *stupid( ) { string s = “stupid”; return &s; } int main( ) { cout << *stupid( ) << endl; return 0; } string stupid( ) { string s = “stupid”; return s; } int main( ) { cout << stupid( ) << endl; return 0; } string *stupid( ) { string *s = new string( “stupid” ); return s; } int main( ) { cout << *stupid( ) << endl; return 0; } CSS342: Introduction
Structure and Its Array Arrays, Pointers, and Structures Frank Cioch M 0202 Arnold Arnold Berger Berger M M 0201 0201 #include <vector> #include <string> #define MAXSIZE 10 struct Students { string firstName; string lastName; char sex; int id; } void swap( Students& s1, Students& s2 ); void main( ) { vector<Students> myClass(MAXSIZE); swap( myClass[0], myClass[1] ); } void sway( Students& s1, Students& s2 ) { Students temp = s1; s1 = s2; s2 = temp; } myClass[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Arnold Berger M 0201 Frank Cioch M 0202 Bill Erdly M 0203 Munehiro Fukuda M 0204 Chuck Jackels M 0205 Baili Liu F 0206 Janet McDaniel F 0207 Dina Meske F 0208 Clark Olson M 0209 Darian Smolar F 0210 temp CSS342: Introduction
Arrays, Pointers, and Structures Discussion: What If Students’ firstName and lastName are pointers to string struct Students { string firstName; string lastName; char sex; int id; } struct Students { string *firstName; string *lastName; char sex; int id; } void sway( Students& s1, Students& s2 ) { Students temp = s1; s1 = s2; s2 = temp; } CSS342: Introduction
Arrays, Pointers, and Structures Some C++ FundamentalsInput/Output In C:In C++:In Java: #include <stdio.h> #include <iostream> using namespace std; int i; int i; int i; scanf( “%d”, &i ); cin >> i; ObjectInputstream input = new ObjectInputStream( System.in ); try { i = input.readInt( ); } catch (..) { } i += 10; i += 10; i += 10; printf( “i + 10 = %d\n”, i ); cout << “i + 10 =“ << i << endl; System.out.println( ‘i + 10 = “ + i ); Note: cin: standard input (from keyboard) cout: standard output (to display) cerr: standard error (to display but distinguished from cout) CSS342: Introduction
Arrays, Pointers, and Structures C++ FundamentalsFiles In C: In C++: In Java: FILE *fp; ifstream inFile(“name.dat”); import java.io.*; or FileInputStream infile = istream inFile; new FileInputStream( “name.dat” ); if ((fp = fopen( “name.dat”, “r” )) inFile.open(“name.dat”); ObjectInputStream input = != NULL { if ( inFile ) { // true of false new ObjectInputStream( infile ); fscanf( fp, “%d”, &i ); inFile >> i; try { i = input.readInt( ); fclose(fp); inFile.close( ); } catch ( EOFException e ) { } } input.close( ); } Note: for output, use ofstream. CSS342: Introduction