13 Enhancing the Wage Calculator App

Slides:



Advertisements
Similar presentations
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
Advertisements

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Microsoft Visual Basic: Reloaded Chapter Eight Sub and Function Procedures.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Debugging Projects Using C++.NET Click with the mouse button to control the flow of the presentation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhancing the Wage Calculator Application Introducing Function Procedures and.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Wage Calculator Application.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Fund Raiser Application Introducing Scope, Pass-by-Reference and Option Strict.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 5 Completing the Inventory Application Introducing Programming.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Introduction to Visual Basic Programming. Introduction Simple Program: Printing a Line of Text Another Simple Program: Adding Integers Memory Concepts.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Enhancing the Wage Calculator Application.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Inventory Application.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
REEM ALMOTIRI Information Technology Department Majmaah University.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Dive Into® Visual Basic 2010 Express
14 Shipping Time App Using Dates and Timers
Chapter 6: User-Defined Functions I
Dr. Shady Yehia Elmashad
Introduction to Programming and Visual Basic
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 2: Introduction to Visual Basic Programming
JavaScript: Functions
JavaScript Functions.
JavaScript: Functions.
Dr. Shady Yehia Elmashad
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Chapter 5 - Functions Outline 5.1 Introduction
Dr. Shady Yehia Elmashad
Functions Declarations CSCI 230
Variables and Arithmetic Operations
Tutorial 12 – Security Panel Application Introducing the Select Case Multiple-Selection Statement Outline Test-Driving the Security Panel Application.
Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects Outline Test-Driving the Microwave Oven Application Designing.
Chapter 6 Methods: A Deeper Look
Chapter 3: Introduction to Problem Solving and Control Statements
Chapter 6 Control Statements: Part 2
Chapter 6: User-Defined Functions I
Debugging Visual Basic Programs
Methods.
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Presentation transcript:

13 Enhancing the Wage Calculator App Introducing Function Procedures and Sub Procedures ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Introduction The best way to develop and maintain a large app is to construct it from smaller, more manageable pieces. This technique is known as divide and conquer. Manageable pieces include program components—known as procedures. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Test-Driving the Enhanced Wage Calculator App Run the completed app (Fig. 13.1). Click the Calculate Button. The result is displayed in the Gross earnings: Label. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

13.2 Classes and Procedures The key to creating large apps is to break them into smaller pieces. In object-oriented programming, these pieces consist primarily of classes, which can be further broken down into methods. Programmers combine programmer-defined classes and methods with preexisting code in the .NET Framework Class Library. Using preexisting code saves time, effort and money. The concept of reusing code increases efficiency for app developers. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

13.2 Classes and Procedures (Cont.) Figure 13.2 lists several pre-existing Visual Basic methods. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating the Hypotenuse Calculator App Open HypotenuseCalculator.sln in the HypotenuseCalculator directory (Fig. 13.3). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating the Hypotenuse Calculator App (Cont.) Switch to Code view, and examine the code provided in the template, shown in Fig. 13.4. Initially, lines 8–11 are underlined by the IDE because they are not yet used. We’ve provided an incomplete event handler for the Calculate Hypotenuse Button. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating the Hypotenuse Calculator App (Cont.) Add lines 26–28 of Fig. 13.5 after event handler calculateButton_Click and before the End Class keywords, then press Enter. The keywords End Function are added by the IDE (line 30) when you press Enter. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating the Hypotenuse Calculator App (Cont.) The procedure name can be any valid identifier and is followed by a set of parentheses containing the parameter declarations, if any. The declaration in the parentheses is known as the parameter list, where variables (called parameters) are declared. Parameters enable a procedure to receive data that helps it perform its task. The parameter list can contain zero or more declarations separated by commas. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating the Hypotenuse Calculator App (Cont.) A Function procedure returns one value after it performs its task. To specify the return type, the parameter list is followed by the keyword As, which is in turn followed by a data type. The type that follows As, known as the return type, indicates the type of the result returned from the Function. The Function procedure ends with the keywords End Function. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating the Hypotenuse Calculator App (Cont.) The ^ operator (Fig. 13.6) is used to calculate the square of input. A Return statement is used to return this value. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating the Hypotenuse Calculator App (Cont.) These lines call Square by using the procedure name followed by a set of parentheses that contain the procedure’s argument (Fig. 13.7). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating the Hypotenuse Calculator App (Cont.) When you typed the opening parenthesis after the procedure name, you probably noticed that the Visual Basic IDE displays a window containing the procedure’s argument names and types (Fig. 13.8). This is the Parameter Info feature of the IDE. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating the Hypotenuse Calculator App (Cont.) Line 32 (Fig. 13.9) calls the .NET Framework Class Library method Sqrt of class Math (by using the dot operator). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating the Hypotenuse Calculator Application (Cont.) Run and test the app (Fig. 13.10). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating a Function Procedure That Returns the Largest of Three Numbers Open Maximum.sln in the Maximum directory (Fig. 13.11) and switch to Design view. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating a Function Procedure That Returns the Largest of Three Numbers (Cont.) Double click the Maximum Button to create an event handler. The IDE underlines Maximum in blue on your screen—indicating a compilation error—because Function procedure Maximum has not yet been defined (Fig. 13.12). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating a Function Procedure That Returns the Largest of Three Numbers (Cont.) Create the Function procedure Maximum (Fig. 13.13). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating a Function Procedure That Returns the Largest of Three Numbers (Cont.) The maximum is determined by using the Max method of .NET Framework Class Library class Math (Fig. 13.14). The Return statement terminates the procedure’s execution and returns the result (the value of finalMaximum) to the calling procedure. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating a Function Procedure That Returns the Largest of Three Numbers (Cont.) Run and test the app (Fig. 13.15). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating a Sub Procedure within the Wage Calculator App Open WageCalculator2.sln in the WageCalculator2 directory. Double click the Calculate Button to generate an event handler (Fig. 13.16). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating a Sub Procedure within the Wage Calculator App (Cont.) Add Sub procedure DisplayPay to your app (lines 18–39 of Fig. 13.17). There’s no return type, because Sub procedures do not return values. When control reaches the End Sub statement, control returns to the calling procedure. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating a Function Procedure within the Wage Calculator App Note that the return type of the procedure is Boolean (Fig. 13.18)—the value returned by the procedure must be a Boolean. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Creating a Function Procedure within the Wage Calculator Application (Cont.) In Sub procedure DisplayPay, replace the statement (line 26 of Fig. 13.17) If hours <= HOUR_LIMIT Then with line 26 of Fig. 13.19. . ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Figure 13.20 presents the source code of the app. Outline Figure 13.20 presents the source code of the app. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

13.5 Using the Debugger: Debugging Controls These ToolStripButtons (Fig. 13.21) provide convenient access to commands in the Debug menu. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Using the Debugger: Debugging Controls In the Wage Calculator app, set a breakpoint in line 15 (Fig. 13.22). Select Debug > Start Debugging. Enter the value 7.50 in the Hourly wage: TextBox, and enter 35 in the Weekly hours: TextBox. Click the Calculate Button ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Using the Debugger: Debugging Controls (Cont.) The Step Into ToolStripButton ( ) executes the app’s next statement (Fig. 13.23). If the next statement to execute is a procedure call and you click the Step Into ToolStripButton, control transfers to the called procedure so you can execute the procedure’s statements and confirm that they execute properly. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Using the Debugger: Debugging Controls (Cont.) Click the Step Into ToolStripButton to enter procedure DisplayPay (Fig. 13.24). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Using the Debugger: Debugging Controls (Cont.) Click the Step Over ToolStripButton ( ) to execute the current statement without stepping into it (Fig. 13.25). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Using the Debugger: Debugging Controls (Cont.) Click the Step Over ToolStripButton again. Step Over behaves like the Step Into when the next statement to execute does not contain a procedure call. If the next statement to execute contains a procedure call, the called procedure executes in its entirety, and the yellow arrow advances to the next executable line (Fig. 13.26). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Using the Debugger: Debugging Controls (Cont.) Set a breakpoint at the end of procedure DisplayPay in line 39 (Fig. 13.27). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Using the Debugger: Debugging Controls (Cont.) Clicking the Continue ToolStripButton ( ) executes any statements between the next executable statement and the next breakpoint or simply continues program execution if there are no more breakpoints. Click the Stop Debugging ToolStripButton ( ) to end the debugging session and return the IDE to design mode. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

13.6 Optional Parameters When a procedure is invoked repeatedly with the same argument value, you can specify that such a parameter is an Optional parameter. When the argument for an Optional parameter is omitted, the compiler rewrites the procedure call, inserting the default value. There are three rules for using Optional parameters: Each Optional parameter must have a default value. The default value must be a constant expression. All parameters after an Optional parameter must also be Optional parameters. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

13.6 Optional Parameters (Cont.) Consider the Function BoxVolume: Function BoxVolume(Optional length As Integer = 1, Optional width As Integer = 1, Optional height As Integer = 1 ) As Integer Return length * width * height End Function ' BoxVolume Each parameter has a default value specified with an = and a literal value (1). ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

13.6 Optional Parameters (Cont.) You can now invoke Function BoxVolume several different ways: BoxVolume() ' returns 1; default values used for length, width, height BoxVolume(10) ' returns 10; default values used for width, height BoxVolume(10, 20) ' returns 200; default value used for height BoxVolume(10, 20, 30) ' returns 6000; no default values used BoxVolume(, 20, 30) ' returns 600; default value used for length BoxVolume(10, , 30) ' returns 300; default value used for width Comma placeholders are used when an omitted argument is not the last argument in the call. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.