Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSS342: Introduction1 Professor: Munehiro Fukuda.

Similar presentations


Presentation on theme: "CSS342: Introduction1 Professor: Munehiro Fukuda."— Presentation transcript:

1 CSS342: Introduction1 Professor: Munehiro Fukuda

2 CSS342: Introduction2 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) Introduction

3 CSS342: Introduction3 Grading Read the class syllabus very carefully. Introduction Course Work% Midterm exam25% Final exam25% Programming assignments #1 - #732.5% Quizzes #1 - #412% (3% each) Laboratory work #1 - #73.5% In-classroom exercises #1 - #42% Total100%

4 CSS342: Introduction4 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. http://courses.washington.edu/css342/fukuda/ Introduction

5 CSS342: Introduction5 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/assig nment.htmlhttp://courses.washington.edu/css342/fukuda/prog/assig nment.html

6 CSS342: Introduction6 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 Introduction

7 CSS342: Introduction7 Life Cycle of Software Specification Design Risk Analysis Verification Coding Testing Refining Production Maintenance Software Engineering

8 CSS342: Introduction8 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”. Software Engineering

9 CSS342: Introduction9 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 << ….; } Software Engineering

10 CSS342: Introduction10 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. Software Engineering

11 CSS342: Introduction11 Design Cont’d How about Classes? UML Class Diagrams Software Engineering UML Interaction Diagrams Bank -name:string=null -routingNum:int=-1 +authorize( in name:string, in identifier:string ) -createAccount( ) attribute operation Account 1 0..* containment CheckingSavings generalization (inheritance) :Customerbank:Bank authorize(name, identifier) OK

12 CSS342: Introduction12 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. Software Engineering

13 CSS342: Introduction13 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.) Software Engineering

14 CSS342: Introduction14 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. Software Engineering

15 CSS342: Introduction15 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? Our main focus Information hiding Arrays, Pointers, and Structures

16 CSS342: Introduction16 Discussion: Why are the ordinary arrays and C strings the second-class objects? Arrays: C strings: Arrays, Pointers, and Structures

17 CSS342: Introduction17 Using the C++ Standard vector Arrays, Pointers, and Structures #include vector array; vector array( size_type num, const type &val ); vector array( const vector &from ); Creates a vector Example: vector a; vector a(3); vector a(3, 0); vector b( a ); ==, !=, =,, =, [] Operators, assignment, and indexing Example: a == b; (true if a and b has the same size, and the i-th member of a and that of b are identical void push_back( const type &val) Appends val to the end of the current vector Example: a.push_back(5); void resize( size_type size, type val ) Changes the size of the current vector to size, setting any newly-created elements to val. Example: a.resize(5, 10); size_type size( ) Returns the number of elements in the current vector. Example: a.size( ); For more info., visit http://www.cppreference.com/cppvector.htmlhttp://www.cppreference.com/cppvector.html

18 CSS342: Introduction18 Using the C++ Standard string Arrays, Pointers, and Structures In CIn C++ #include #include 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/http://www.cppreference.com/cppstring/

19 CSS342: Introduction19 Call by Value, Reference, and Constant Reference Which of swap functions is appropriate? Arrays, Pointers, and Structures 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; } void swap(string &a, string &b) { string tmp = a; a = b; b = tmp; } int findMax(vector 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 &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 &a) { int max = a[0]; int i; for (i=1; i < a.size(); i++) if (a[i] > max) max = a[i]; return max; } Which of findMax functions is appropriate?

20 CSS342: Introduction20 Pointers 1.Pointer variablesint *p, *q; 2.Static allocationint x; 3.Address-of operatorp = &x; 4.Memory cell to which P points*p = 6; 5.Pointer operationsq = p; ??? pqx ?? pqx ?6 pqx 6 pqx 1 3 4 5 Arrays, Pointers, and Structures

21 CSS342: Introduction21 Dynamic Allocation 1. Pointer declarationint *p, *q; 2. Dynamic allocationp = new int; q = new int; 3. Deallocationdelete p; p = NULL; 4. Memory leakq = new int; ? ? ? p q p q p q p q ??? ? 123 4 NULL Leak! Arrays, Pointers, and Structures

22 CSS342: Introduction22 Discussion: What is the difference between the following three programs? Arrays, Pointers, and Structures 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; }

23 CSS342: Introduction23 #include #define MAXSIZE 10 struct Students { string firstName; string lastName; char sex; int id; } void swap( Students& s1, Students& s2 ); void main( ) { vector myClass(MAXSIZE); swap( myClass[0], myClass[1] ); } void sway( Students& s1, Students& s2 ) { Students temp = s1; s1 = s2; s2 = temp; } Berger Cioch Olson Erdly Meske Liu McDaniel Fukuda Jackels Smolar Arnold Frank Clark Bill Dina Baili Janet Munehiro Chuck Darian M M M M F F F M M F 0201 0202 0209 0203 0208 0206 0207 0204 0205 0210 myClass[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] temp BergerArnoldM0201 CiochFrankM0202BergerArnoldM0201 Structure and Its Array Arrays, Pointers, and Structures

24 CSS342: Introduction24 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; } Arrays, Pointers, and Structures

25 CSS342: Introduction25 Some C++ Fundamentals Input/Output In C:In C++:In Java: #include #include 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) Arrays, Pointers, and Structures

26 CSS342: Introduction26 C++ Fundamentals Files In C:In C++:In Java: FILE *fp;ifstream inFile(“name.dat”);import java.io.*; orFileInputStream infile = istream inFile;new FileInputStream( “name.dat” ); if ((fp = fopen( “name.dat”, “r” ))inFile.open(“name.dat”);ObjectInputStream input = != NULL {if ( inFile ) { // true of falsenew 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. Arrays, Pointers, and Structures


Download ppt "CSS342: Introduction1 Professor: Munehiro Fukuda."

Similar presentations


Ads by Google