css342: introduction1 professor: munehiro fukuda

26
CSS342: Introduction 1 CSS342: Introduction Professor: Munehiro Fukuda

Upload: kristian-lambert

Post on 20-Jan-2018

230 views

Category:

Documents


0 download

DESCRIPTION

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%

TRANSCRIPT

Page 1: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 1

CSS342: Introduction

Professor: Munehiro Fukuda

Page 2: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 2

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

Page 3: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 3

GradingRead the class syllabus very carefully.

Introduction

Course Work %Midterm exam 25%Final exam 25%Programming assignments #1 - #7 32.5%Quizzes #1 - #4 12% (3% each)Laboratory work #1 - #7 3.5%In-classroom exercises #1 - #4 2%Total 100%

Page 4: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 4

Questions and Discussions• Professors’s email account:

[email protected]• Mailing list:

[email protected]• 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.

Introduction

Page 5: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 5

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/assignme

nt.html

Page 6: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 6

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

Page 7: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 7

Life Cycle of SoftwareSpecification

Design

Risk Analysis

Verification

CodingTesting

Refining

Production

Maintenance

Software Engineering

Page 8: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 8

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

Page 9: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 9

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

Page 10: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 10

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

Page 11: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 11

Design Cont’dHow about Classes?

UML Class Diagrams

Software Engineering

UML Interaction DiagramsBank

-name:string=null-routingNum:int=-1

+authorize( in name:string, in identifier:string )-createAccount( )

attribute

operation

Account

1

0..*containment

Checking Savings

generalization (inheritance)

:Customer bank:Bank

authorize(name, identifier)

OK

Page 12: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 12

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

Page 13: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 13

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

Page 14: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 14

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

Page 15: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 15

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

Page 16: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 16

Discussion: Why are the ordinary arrays and C strings the second-class objects?

• Arrays:

• C strings:

Arrays, Pointers, and Structures

Page 17: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 17

Using the C++ Standard vectorArrays, Pointers, and Structures

#include <vector>

vector<type> array; vector<type> array( size_type num, const type &val ); vector<type> array( const vector &from ); Creates a vectorExample: vector<int> a; vector<int> a(3); vector<int> a(3, 0); vector<int>b( a );

==, !=, <=, >=, <, >, =, []Operators, assignment, and indexingExample: 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 vectorExample: 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.html

Page 18: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 18

Using the C++ Standard stringArrays, Pointers, and Structures

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/

Page 19: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 19

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<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;}

Which of findMax functions is appropriate?

Page 20: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 20

Pointers1. Pointer variables int *p,

*q;2. Static allocation int x;3. Address-of operator p = &x;4. Memory cell to which P points *p = 6;5. Pointer operations q = p;

? ? ?

p q x

? ?

p q x

? 6

p q x

6

p q x

1 3 4

5

Arrays, Pointers, and Structures

Page 21: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 21

Dynamic Allocation1. Pointer declaration int *p, *q;2. Dynamic allocation p = new int; q = new int;3. Deallocation delete p;

p = NULL;4. Memory leak q = new int;

??

?

p

q

p

q

p

q

p

q

? ? ?

?

1 2 3 4NULL NULL

Leak!

Arrays, Pointers, and Structures

Page 22: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 22

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;}

Page 23: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 23

#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;}

BergerCioch

Olson

Erdly

Meske

LiuMcDaniel

FukudaJackels

Smolar

ArnoldFrank

Clark

Bill

Dina

BailiJanet

MunehiroChuck

Darian

MM

M

M

F

FF

MM

F

02010202

0209

0203

0208

02060207

02040205

0210

myClass[0][1][2][3][4][5][6][7][8][9]

temp BergerArnold M 0201

CiochFrank M 0202BergerArnold M 0201

Structure and Its ArrayArrays, Pointers, and Structures

Page 24: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 24

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

Page 25: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 25

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)

Arrays, Pointers, and Structures

Page 26: CSS342: Introduction1 Professor: Munehiro Fukuda

CSS342: Introduction 26

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.

Arrays, Pointers, and Structures