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

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

Jan 06, 2018

Download

Documents

Britney Bennett

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
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming with Visual C++: Concepts and Projects Chapter 4B: Selection (Tutorial)

Programming with Visual C++: Concepts and Projects

Chapter 4B: Selection (Tutorial)

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

Tutorial: Vacation Planner

• Problem Analysis• Design– Interface sketch– Control and Data Tables– Algorithms

• Development– Create the interface– Code event handlers

• TestingProgramming with Visual C++: Concepts and Projects 2

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

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 Projects 3

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

Problem Analysis (continued)

Programming with Visual C++: Concepts and Projects 4

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

Problem Analysis (continued)

Programming with Visual C++: Concepts and Projects 5

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

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 Projects 6

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

Design (continued)

Programming with Visual C++: Concepts and Projects 7

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

Design (continued)

Programming with Visual C++: Concepts and Projects 8

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

Design (continued)

Programming with Visual C++: Concepts and Projects 9

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

Design (continued)

• Algorithm for Form1_Load() event

Programming with Visual C++: Concepts and Projects 10

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

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 Projects 11

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

Design (continued)

Programming with Visual C++: Concepts and Projects 12

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

Design (continued)

Programming with Visual C++: Concepts and Projects 13

• A look ahead at Visual C++ code (a multiple alternative if statement) to implement Step 2.4 (determining the cost of the room)

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

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 Projects 14

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

Design (continued)

Programming with Visual C++: Concepts and Projects 15

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

Design (continued)

Programming with Visual C++: Concepts and Projects 16

• A look ahead at Visual C++ code (a single alternative if statement) to implement Step 3.1

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

Design (continued)

Programming with Visual C++: Concepts and Projects 17

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

Design (continued)

Programming with Visual C++: Concepts and Projects 18

• A look ahead at Visual C++ code (a multiple alternative switch statement) to implement Step 3.4.1

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

Design (continued)

Programming with Visual C++: Concepts and Projects 19

• Final Algorithm for btnCalc_Click()

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

Design (continued)

Programming with Visual C++: Concepts and Projects 20

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

Design (continued)

• Test strategy– Identify valid data ranges– Identify invalid data ranges

Programming with Visual C++: Concepts and Projects 21

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

Design (continued)• Test strategy– Create test scenarios that involve valid and invalid

data

Programming with Visual C++: Concepts and Projects 22

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

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 Projects 23

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

Design (continued)

Programming with Visual C++: Concepts and Projects 24

• 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

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

Design (continued)

Programming with Visual C++: Concepts and Projects 25

• With the revision made, the algorithm now traces correctly to its finish

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

Design (continued)

Programming with Visual C++: Concepts and Projects 26

• Each test scenario should be verified this way

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

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 Projects 27

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

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 Projects 28

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

Development (continued)

Programming with Visual C++: Concepts and Projects 29

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

Development (continued)

Programming with Visual C++: Concepts and Projects30

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

Development (continued)

Programming with Visual C++: Concepts and Projects 31

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

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 Projects 32

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

Development (continued)

Programming with Visual C++: Concepts and Projects 33

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

Development (continued)

Programming with Visual C++: Concepts and Projects 34

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

Development (continued)

Programming with Visual C++: Concepts and Projects 35

• You can set a default selection for the ComboBox by assigning a SelectedIndex value

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

Development (continued)

• Code the event handlers– Form1_Load()– btnCalc_Click()

Programming with Visual C++: Concepts and Projects 36

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

Development (continued)

Programming with Visual C++: Concepts and Projects 37

• Form1_Load()– Assigns default ComboBox value– Initializes destination names on RadioButtons– Sets default destination RadioButton

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

Development (continued)

Programming with Visual C++: Concepts and Projects 38

• Variables depart and arrive are DateTime objects read from the monthlyCalendars

• Subtraction is defined for DateTime objects for– Months– Days– Years

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

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 Projects 39