Download presentation
Presentation is loading. Please wait.
Published byLinda Garrett Modified over 9 years ago
1
1 Today’s Objectives Announcements Quiz #1 on 8-Jun (Wednesday) – over C++ material in Ch. 1, 2, 3 Be sure to study math operators, assignment operators, relational operators, logic operators, if statements, for loops, while loops, incrementing, and common errors Quick review of some basic C++ (Ch. 1 and Ch. 2) C++ style output and input Escape sequences Variables Arithmetic operators, equality operators, and relational operators Control structures (Ch. 4 and Ch. 5) Control structures – if and else, while, for, switch Stream manipulators Assignment operators and logical operators Increment and decrement operators Intro to UML – Unified Modeling Language Algorithms and pseudocode UML diagrams The Software Life Cycle Bonus Lab 5-Jun-2006
2
2 Quick Review of Some Basic C++ Some C++ Features in Chapters 1 and 2
3
3 Simple Example from Last Week Page 42, Fig. 2.4 Comments // Everything after it on the same line is a comment /* Everything in between is a comment */ int main(){} //All execution starts here C++ statements end with semicolons ; Preprocessor directive to include a C++ standard library #include All C++ library elements are in the “std” namespace using namespace std; using std::cout; Output cout represents the standard output, usually the screen << stream insertion operator, can be concatenated cerr represents the standard error output, also usually the screen Quick Review (Deitel)
4
4 Escape Sequences Page 40, Fig. 2.2 Special characters used in printing \n Newline \t Tab \\ Backslash \" Double quote Example: cout << "\nEscape sequences begin with a \"\\\"\n"; Quick Review (Deitel)
5
5 Variables Declaring variables, page 1232, Append. C //Datatype followed by a variable name //Can also be initialized when declared int n, m = 0; //Initializes m but not n double x = 0.0; bool done = false; char userSelection = ' X ' ; int n(0); //Functional style of initialization What will print? Do variables have to be initialized? #include using namespace std; int main(){ int n; cout << " n = " << n << " \n " ; } Quick Review (Deitel)
6
6 Second Example from Last Week Page 43, Fig. 2.5 Input cin represents the standard input, usually the keyboard >> stream extraction operator cin can be used to capture input from the keyboard up to the first whitespace (e.g., space or return) double quiz1; cout << "Enter your grade for Quiz 1: "; cin >> quiz1; cout << "You typed " << quiz1 << endl; endl to output a newline and flush output buffer Quick Review (Deitel)
7
7 Arithmetic Operators Arithmetic operators, page 48, fig. 2.9 + Addition - Subtraction * Multiplication / Division % Modulus, result is the remainder Integer division int a = 28, b = 5; cout << "a / b = " << (a / b) << endl; //What prints? cout << "a % b = " << (a % b) << endl; //What prints? Precedence, page 49, fig. 2.10 and page 1228, Append. A Parentheses, innermost first, then left to right *, /, %, left to right +, -, left to right Example: cout << (2 * (5 * 5) + 3 * 5) + 7 << endl; Quick Review (Deitel)
8
8 Equality Operators and Relational Operators Equality operators equality operator == (don’t confuse with = ) x == y; //x is equal to y operator != x != y; //x is not equal to y Relational operators operator < x < y; //x is less than y operator > x > y; //x is greater than y operator <= x <= y; //x is less than or equal to y operator >= x >= y; //x is greater than or equal to y Quick Review (Deitel, 51–54, Fig. 2.12)
9
9 Where’s the Error? #include using namespace std; int main(){ bool done = false; while( !done ){ cout << "Continue?(Y/N): "; char input = 'Y'; cin >> input; if( input = 'N' ) done = true; } cout << "Bye!\n"; } Quick Review (Deitel)
10
10 Control Structures C++ Features in Chapters 4 and 5
11
11 C++ Control Structures Sequence – built-in Selection structures if if/else switch Repetition structures while do/while for Control Structures (Deitel, 127–131)
12
12 if/else Used to select from different actions, based on whether a condition is true if( courseScore >= 92.5 ) courseGrade = "A"; else if( courseScore >= 89.5 ) courseGrade = "A-"; else courseGrade = "B"; Control Structures (Deitel, 131–137)
13
13 Where’s the Error? if( courseScore >= 92.5 ); courseGrade = "A"; Control Structures (Deitel)
14
14 Counter-Controlled while Loop A while loop repeats an action as long as a condition is true In this example, a counter controls the number of iterations int grade, total=0, gradeCounter=1; while( gradeCounter <= 10 ) { cout << "Enter grade "; cin >> grade; total = total + grade; gradeCounter = gradeCounter + 1; } Control Structures (Deitel, 137–145, Fig. 4.9 on p. 142)
15
15 Where’s the Error? int grade, total=0, gradeCounter=1; while( gradeCounter <= 10 ); { cout << "Enter grade "; cin >> grade; total = total + grade; gradeCounter = gradeCounter + 1; } Control Structures (Deitel)
16
16 Sentinel-Controlled while Loop A sentinel is a special value used to signal that looping should end – useful when the number of iterations is not known in advance int main(){ char selection; string input; bool done = false; while( !done ){ printMenu(); cout << "Enter your selection (x to quit): "; cin >> input; selection = input[0]; if( selection == 'x' ) done = true; } Control Structures (Deitel, 145–153, Fig. 4.13 on p. 151)
17
17 Stream Manipulators Special elements that can be used to format output Can be used with the stream insertion operator << Include the following library: #include //Fig. 4.13, page 151, line 86 cout << "Average = " << setprecision(2) << fixed << average << endl; setprecision(2) – print two digits to the right of the decimal fixed – always use fixed point notation, not scientific notation, and always print any trailing zeroes setw(16) – set the width for each field to 16 spaces left – left-justify a field Control Structures (Deitel, 155)
18
18 Assignment Operators Operators for abbreviating assignment expressions Examples int a = 1, b = 1, c = 1; a += 2; //The same as: a = a + 2 a += b; //a = a + b b -= 3; //b = b – 3 c *= 4; //c = c * 4 c *= c; //What will be printed? cout << "c = " << c << endl; Control Structures (Deitel, 161, Fig. 4.19)
19
19 Increment and Decrement Operators Operators that add 1 or subtract 1 from a variable Post-increment and post-decrement int a = 1, b = 1; a++; // a = a + 1 b--; // b = b – 1 Pre-increment and pre-decrement ++a; // a = a + 1 --b; // b = b - 1 Control Structures (Deitel, 161–164, Fig. 4.20)
20
20 for Loop for( int i=0; i<10; ++i ) { cout << i << " "; } Initialization – assigns the starting value and is executed once. Condition – tested at the beginning of each loop, and the loop is executed only if it evaluates to true. Expression – evaluated at the end of each loop Block of statements executed in the loop Control Structures (Deitel, 188–197)
21
21 A block is needed only if multiple statements are executed in each loop. for( int i=0; i<10; ++i ) { cout << i; cout << " "; } for Loop If only one statement is executed in each loop, it’s not necessary to define a block with { and }. for( int i=0; i<10; ++i ) cout << i << " "; Control Structures (Deitel, 188–197)
22
22 Changes to the Value of i for( int i=0; i<5; ++i ) { cout << i << " "; } Iteration Value of i during the loop Expression at the end of the loop First loop0 (starting value)i = i + 1 Second loop1i = i + 1 Third loop2i = i + 1 Fourth loop3i = i + 1 Fifth loop4i = i + 1 Control Structures (Deitel, 188–197)
23
23 for Loop Guideline Use a “for” loop when you already know how many times to repeat your code for loops are usually used to step through a data structure with a known length, such as an array const int SIZE = 5; int myArray[SIZE] = {0,0,0,0,0}; for( int i=0; i<SIZE; ++i ) cout << myArray[i] << " "; Control Structures (Deitel, 188–197)
24
24 Nested for Loop int result = 0; for( int i=0; i<3; ++i ) for( int j=0; j<5; ++j ) result++; ijresult 001 012 023 034 045 106 117 128 139 1410 2011 2112 2213 2314 2415 Control Structures (Deitel, 188–197)
25
25 Nested Loop with Dependent Variable int result = 0; for( int i=0; i<3; ++i ) for( int j=i; j<5; ++j ) result++; ijresult 001 012 023 034 045 116 127 138 149 2210 2311 2412 Control Structures (Deitel, 188–197)
26
26 How many iterations? for( int i=0; i<10; ++i ) cout << i << " "; for( int i=10; i>0; --i ) cout << i << " "; int result = 0; for( int i=0; i<2; ++i ) for( int j=0; j<3; ++j ) result++; Control Structures (Deitel, 188–197)
27
27 switch Structure Can replace a complicated if/else structure char selection; cin >> selection; switch( selection ){ case '1': myStore.viewCustomerList(); break; case 'x': case 'X': done = true; break; default: cout << "Incorrect input" << endl; break; } Control Structures (Deitel, 199–205) This controlling expression is compared to each of the case labels Execution continues at the case label that matches The break causes execution to go to the next line following the switch
28
28 Logical Operators Logical AND – both conditions must be true && Logical OR – one or the other condition must be true || Examples: if( gender == 'F' && age > 64 ) seniorFemales++; if( average >= 90 || finalExam >= 90 ) grade = 'A'; Control Structures (Deitel, 211–215)
29
29 Intro to UML A tool for object-oriented analysis and design
30
30 Software Process Series of steps for software development Sometimes called a “method” No standard software process But it is important to use a process Important for non-trivial programs Advantages 1. Repeatable procedure 2. Remember all the things that need to be done 3. Measurable progress Intro to UML (Sommerville)
31
31 Steps in Software Development Also known as the “Software Life Cycle” Requirements gathering Analysis Program Design Coding Testing Operations Intro to UML (Royce)
32
32 Analysis and Design Analysis Determine what a program should do, but not how it should do it Build models, e.g. with UML or pseudocode algorithms Program Design Adjust the models to simplify implementation and improve execution Determine how the program should work When it’s done with object-oriented programs, it’s called object-oriented analysis and design, or OOAD Intro to UML (Royce)
33
33 Tools for Analysis and Design Pseudocode algorithms Algorithm = A step-by-step procedure for performing a task in a finite time Pseudocode = structured, English-like statements that describe the algorithm UML Unified Modeling Language Graphical language used to model object-oriented programs Use case diagrams, class diagrams, sequence diagrams, activity diagrams, etc. Intro to UML (Deitel)
34
34 Algorithm arrayMax(A,n): Input: An array A storing n >= 1 integers. Output: The maximum element in A. currentMax = A[0] for i = 1 to n – 1 do if currentMax < A[i] then currentMax = A[i] return currentMax Pseudocode example 1 (more code-like) Pseudocode Algorithm Intro to UML (Deitel, 125–127; Goodrich) set currentMax to the first element in the input array for each element in the array if currentMax < the new element then set currentMax to the new element return currentMax Pseudocode example 2 (more English-like)
35
35 UML Graphical language – symbols used to create diagrams that model object-oriented programs Intro to UML (Deitel) Use case diagrams Class diagrams Sequence diagrams
36
36 Phase 1: Requirements Gathering What are we making? Consult the stakeholders Read the assignment and ask the instructor Goal Problem statement List of requirements UML tool Use case diagrams Intro to UML (Eckel)
37
37 Phase 2: Analysis and Design How will we build it? Come up with a design describing the classes Goal Object model Functional model – describes the operations of the objects Tools UML – class diagrams Pseudocode for the functional model Intro to UML (Eckel; Blaha)
38
38 Phase 3: Coding and Testing Build the first version Start programming Goal Get something working Keep it simple – small, simple classes Testing Create tests early – even before coding Automate testing as much as possible so that tests can be run every time the program changes Intro to UML (Eckel)
39
39 Iterations Iteration = repetition You won’t get the whole project right the first time Start with the core functionality – get it working, then add more features with each iteration Intro to UML (Eckel)
40
40 Case Study: Video Rental System Phase 1 – What are we making? 1.The store will rent movies as either VHS tapes or DVDs. 2.The store will also rent games. 3.Multiple copies of all the most popular movies will be stocked. 4.The store clerk will need to be able to look up a movie by its title in order to determine whether it’s available in the store. 5.The store owner will need to print a list of movie and game titles that are in stock when doing the inventory. 6.The store needs to keep track of its customers, and only the registered customers can rent items. The clerk must be able to determine which customer has checked out an item, and also how many items they have checked out. 7.After renting a movie or game, the customer must return it after three days. Intro to UML
41
41 Phase 2 – How will we build it? Discover classes by finding the nouns 1.The store will rent movies as either VHS tapes or DVDs. 2.The store will also rent games. 3.Multiple copies of all the most popular movies will be stocked. 4.The store clerk will need to be able to look up a movie by its title in order to determine whether it’s available in the store. 5.The store owner will need to print a list of movie and game titles that are in stock when doing the inventory. 6.The store needs to keep track of its customers, and only the registered customers can rent items. The clerk must be able to determine which customer has checked out an item, and also how many items they have checked out. 7.After renting a movie or game, the customer must return it after three days. Intro to UML
42
42 Eliminate nouns that are: irrelevant, redundant, or used only as values 1.The store will rent movies as either VHS tapes or DVDs. 2.The store will also rent games. 3.Multiple copies of all the most popular movies will be stocked. 4.The store clerk will need to be able to look up a movie by its title in order to determine whether it’s available in the store. 5.The store owner will need to print a list of movie and game titles that are in stock when doing the inventory. 6.The store needs to keep track of its customers, and only the registered customers can rent items. The clerk must be able to determine which customer has checked out an item, and also how many items they have checked out. 7.After renting a movie or game, the customer must return it after three days. Intro to UML
43
43 Remaining nouns establish the basic structure of the object model store movie VHS tape DVD game list customer item Intro to UML In UML, Classes are modeled as rectangles, which can be divided into three sections. The class name is at the top. The data members are in the middle. Member functions are at the bottom. RentalItem – title : string – copiesInStock : int – copiesAvailable : int
44
44 Operations can be discovered by creating use cases 1.The store will rent movies as either VHS tapes or DVDs. 2.The store will also rent games. 3.Multiple copies of all the most popular movies will be stocked. 4.The store clerk will need to be able to look up a movie by its title in order to determine whether it’s available in the store. 5.The store owner will need to print a list of movie and game titles that are in stock when doing the inventory. 6.The store needs to keep track of its customers, and only the registered customers can rent items. The clerk must be able to determine which customer has checked out an item, and also how many items they have checked out. 7.After renting a movie or game, the customer must return it after three days. Intro to UML
45
45 Consider how a user of the system will interact with it 1.The store will rent movies as either VHS tapes or DVDs. 2.The store will also rent games. Intro to UML Store Employee Check out item to customer 3.Multiple copies of all the most popular movies will be stocked. Store Employee Add an item
46
46 Validate the use case diagram with the stakeholders Intro to UML Store Employee Check out item to customer Add an itemCheck in item from customer View items by title Search for a title Search for a customer Add a customer
47
47 Express the use cases as algorithms describing the operations Algorithm checkOutItem( title ) for each item in inventory if( item.title == title ) item.checkOut() Intro to UML Store Employee Check out item to customer RentalItem – title : string – copiesInStock : int – copiesAvailable : int + checkOut() Store + checkOutItem() Then add the operations to the appropriate class.
48
48 Bonus Lab #1 Optional Procedure First, a demo by the instructor Then you will complete the assignment on your own Print it and hand it in before the end of class Rules Do your own work, but you may help each other You may ask the instructor for help You may leave if you finish early or if you do not wish to do this assignment 1 bonus point added to a quiz grade for each correctly completed lab handed in before the end of class
49
49 References Blaha, M., Premerlani W., Object-Oriented Modeling and Design for Database Applications. Upper Saddle River, NJ: Prentice Hall, 1998. Deitel, H. M., and P. J. Deitel, C++ How to Program, Fourth Edition. Upper Saddle River, NJ: Prentice Hall, 2003. Eckel, B., Thinking in C++, Second Edition. Upper Saddle River, NJ: Prentice Hall, 2002. Goodrich, M. T., R. Tamassia, and D. Mount, Data Structures and Algorithms in C++. Hoboken, NJ: John Wiley & Sons, Inc., 2004. Josuttis, Nicolai M., The C++ Standard Library, A Tutorial and Reference. Boston: Addison-Wesley, 1999. Royce, W., Software Project Management. Boston: Addison-Wesley, 1998. Sommerville, I.,Software Engineering. Harlow, England: Addison- Wesley, 2001.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.