Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Visual C++: Concepts and Projects Chapter 4B: Selection (Tutorial)

Similar presentations


Presentation on theme: "Programming with Visual C++: Concepts and Projects Chapter 4B: Selection (Tutorial)"— Presentation transcript:

1 Programming with Visual C++: Concepts and Projects Chapter 4B: Selection (Tutorial)

2 Tutorial: Vacation Planner Problem Analysis Design – Interface sketch – Control and Data Tables – Algorithms Development – Create the interface – Code event handlers Testing Programming with Visual C++: Concepts and Projects2

3 Problem Analysis Construct a program that allows the user to select a destination and indicate the number of nights and the number of rooms required The program will calculate cost and apply multiple room discounts, if appropriate Programming with Visual C++: Concepts and Projects3

4 Problem Analysis (continued) Programming with Visual C++: Concepts and Projects4

5 Problem Analysis (continued) Programming with Visual C++: Concepts and Projects5

6 Design Interface Sketch – MonthCalendar controls for date of arrival and departure – GroupBox control used to contain RadioButtons – RadioButtons indicate choice of destination When one is selected the others in the group will automatically become deselected – Some control settings will be initialized upon loading the form in memory Programming with Visual C++: Concepts and Projects6

7 Design (continued) Programming with Visual C++: Concepts and Projects7

8 Design (continued) Programming with Visual C++: Concepts and Projects8

9 Design (continued) Programming with Visual C++: Concepts and Projects9

10 Design (continued) Algorithm for Form1_Load() event Programming with Visual C++: Concepts and Projects10

11 Design (continued) Algorithm for btnCalc_Click() event – Step 1: Declare variables – Step 2: Read the input data Read rooms Read arrival date Read departure date Assign room cost based on destination (radio buttons) – Step 3: Process data Programming with Visual C++: Concepts and Projects11

12 Design (continued) Programming with Visual C++: Concepts and Projects12

13 Design (continued) Programming with Visual C++: Concepts and Projects13 A look ahead at Visual C++ code (a multiple alternative if statement) to implement Step 2.4 (determining the cost of the room)

14 Design (continued) Algorithm for btnCalc_Click() event – Step 3 – If rooms < 1 then error message – Calculate nights – If nights < 1 then error message – If no errors Select room discount Calculate total cost Display total cost Programming with Visual C++: Concepts and Projects14

15 Design (continued) Programming with Visual C++: Concepts and Projects15

16 Design (continued) Programming with Visual C++: Concepts and Projects16 A look ahead at Visual C++ code (a single alternative if statement) to implement Step 3.1

17 Design (continued) Programming with Visual C++: Concepts and Projects17

18 Design (continued) Programming with Visual C++: Concepts and Projects18 A look ahead at Visual C++ code (a multiple alternative switch statement) to implement Step 3.4.1

19 Design (continued) Programming with Visual C++: Concepts and Projects19 Final Algorithm for btnCalc_Click()

20 Design (continued) Programming with Visual C++: Concepts and Projects20

21 Design (continued) Test strategy – Identify valid data ranges – Identify invalid data ranges Programming with Visual C++: Concepts and Projects21

22 Design (continued) Test strategy – Create test scenarios that involve valid and invalid data Programming with Visual C++: Concepts and Projects22

23 Design (continued) Use a structured walkthrough (trace table) to verify that the algorithm is correct under each scenario List algorithm steps down the left side and variables across the top Programming with Visual C++: Concepts and Projects23

24 Design (continued) Programming with Visual C++: Concepts and Projects24 The first scenario fails because in Step 3.4 because ok does not contain a value. It was never assigned in Step 1 (we should make a note of this and revise the algorithm accordingly

25 Design (continued) Programming with Visual C++: Concepts and Projects25 With the revision made, the algorithm now traces correctly to its finish

26 Design (continued) Programming with Visual C++: Concepts and Projects26 Each test scenario should be verified this way

27 Design (continued) If the algorithm passes all tests then you are ready to move on to the Development stage If the algorithm fails any test then it must be revised until it passes Programming with Visual C++: Concepts and Projects27

28 Development Create the interface – Use a GroupBox control to contain the RadioButtons – GroupBoxes must be laid down before RadioButtons are placed in them Code the event handlers – Form1_Load() – btnCalc_Click() Programming with Visual C++: Concepts and Projects28

29 Development (continued) Programming with Visual C++: Concepts and Projects29

30 Development (continued) Programming with Visual C++: Concepts and Projects 30

31 Development (continued) Programming with Visual C++: Concepts and Projects31

32 Development (continued) The ComboBox control – Use the Items property to enter the collection of items you wish to appear in the ComboBox list – Each item has its own index value – The first (empty) item has a value of -1 Programming with Visual C++: Concepts and Projects32

33 Development (continued) Programming with Visual C++: Concepts and Projects33

34 Development (continued) Programming with Visual C++: Concepts and Projects34

35 Development (continued) Programming with Visual C++: Concepts and Projects35 You can set a default selection for the ComboBox by assigning a SelectedIndex value

36 Development (continued) Code the event handlers – Form1_Load() – btnCalc_Click() Programming with Visual C++: Concepts and Projects36

37 Development (continued) Programming with Visual C++: Concepts and Projects37 Form1_Load() – Assigns default ComboBox value – Initializes destination names on RadioButtons – Sets default destination RadioButton

38 Development (continued) Programming with Visual C++: Concepts and Projects38 Variables depart and arrive are DateTime objects read from the monthlyCalendars Subtraction is defined for DateTime objects for – Months – Days – Years

39 On Your Own Test scenarios – Verify that your program works as expected for each of the test scenarios Rewrite the switch statement – Use a multiple alternative if statement instead Extreme nesting – Remove the ok variable from your program – Use nested if…else statements to filter out error conditions Programming with Visual C++: Concepts and Projects39


Download ppt "Programming with Visual C++: Concepts and Projects Chapter 4B: Selection (Tutorial)"

Similar presentations


Ads by Google