Working with Variables, Constants, Data Types, and Expressions - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Working with Variables, Constants, Data Types, and Expressions

Description:

... Loan Calculator. Develop a window application for Automobile Loan ... Click Create directory for Solution, type Automobile Loan Calculator in the Name box ... – PowerPoint PPT presentation

Number of Views:429
Avg rating:3.0/5.0
Slides: 43
Provided by: steve1703
Category:

less

Transcript and Presenter's Notes

Title: Working with Variables, Constants, Data Types, and Expressions


1
Chapter 4
  • Working with Variables, Constants, Data Types,
    and Expressions

2
Objectives
  • Learn to use the RadioButton and GroupBox
    controls, and Layout toolbar
  • Learn to use the Pmt function, and the Format
    function
  • Lock controls on a form
  • Visual Basic .NET data types
  • Declare and use variables and constants within
    code
  • Convert between data types
  • Use the Option Strict statement
  • Use arithmetic expressions
  • Describe the order of operator precedence in code

3
Problem Car Loan Calculator
  • Develop a window application for Automobile Loan
    Calculator. It will calculate a customers
    monthly automobile loan payment based on a loan
    amount, interest rate, and the term, or length,
    of the loan.
  • MonthlyPayment (InterestRate
    (1InterestRate) NumberofPayments) / ((1
    InterestRate) NumberofPayments-1) LoanAmount

4
Program Development
5
Problem Analysis
Question What are the inputs, outputs?
6
Interface Design
  • The increment of Loan amount is 1000. The
    maximum value allowed is 25000
  • The Current interest rate is incremented or
    decremented by 0.05 with minimum 5.0 and
    maximum 15.00.
  • The format of Monthly payment is currency

7
Program Design
  • Two tasks
  • Calculate monthly payment
  • Reset the input and output values

8
Validate Design
  • Validate the design by stepping through the
    requirements and make the design addresses each
    requirement
  • For example,
  • The formula in the Algorithms section of the
    requirements document is handled in the design by
    the Computer Payment Click event
  • The items in the first note in the Notes section
    are shown as requested on the storyboard

9
Starting a Project
  • Start VB .NET and click the New Project button on
    the Start Page
  • Click Create directory for Solution, type
    Automobile Loan Calculator in the Name box
  • Browse data disk for Chapter4, and then click
    Open
  • Click the OK button

10
Setting Form Properties
  • Set the properties of Form1 using the table as a
    guide

11
Adding Controls
  • Add three Label, two NumericUpDown, one TextBox,
    and two Button controls
  • Add a GroupBox control

12
Adding RadioButton Controlsto a Group Box
  • Drag the RadioButton button to GroupBox1 control.
    Be sure to release the mouse button while the
    mouse pointer is within the borders of the
    GroupBox1
  • Repeat this step for two more RadioButton controls

13
Change Properties of Controls
14
Change GroupBox Properties
  • The GroupBox is used to group functionally
    similar controls together

15
Change GroupBox Properties
Checked property value is true
16
Using Layout Toolbar
  • The layout toolbar contains tools that allow you
    to adjust the alignment, spacing, and size of
    controls
  • Select the Label2 control, the Current interest
    rate ()
  • Resize the control so that the text displays on a
    single line. Press and hold down the CTRL key
    and then click the other two Label controls on
    the form
  • Click the Align Rights button on the Layout
    toolbar and then click the Make Vertical Spacing
    Equal button on the Layout toolbar.
  • When use Layout toolbar to size and align
    multiple controls, the last control you select is
    used as the basis for aligning the controls

17
Sizing and Aligning Controls
  • Select the nudLoanAmount, nudRate, and
    txtMonthlyPayment controls. Click the Make Same
    Size button and then Align Lefts button on the
    toolbar.

18
Setting a Default Button
  • The default button is the Compute Payment button.
    Pressing the ENTER key is equivalent to clicking
    the button
  • Select the AcceptButton property in the
    properties window to btnComputePayment

19
Locking Controls Positions
  • Disallows the ability to move controls or modify
    control sizes on a form during design time
  • Click Format on the menu bar, and select Lock
    Controls

20
Constants and Variables
  • A value is a number or string that programmers
    use in the code
  • A variable represents a location in computer
    memory that can change values as the code
    executes
  • A constant represents a location in computer
    memory that cannot be changed during execution
  • By the name of a constant, we can easily tell the
    use or the purpose of the constant

21
Data Types
  • Computer uses binary numbers to represent
    everything
  • 1310000011012 c 9910 011000112
  • The data type of a variable or a constant
    determines what kind of data the variable or
    constants can store
  • Try to use the data type that takes up the
    smallest amount of memory
  • Try to use integral data type if decimal number
    is not necessary

22
Data Types
23
Declaring Constants
24
Declaring Constants
  • Double-click the Form1 form in an area that does
    not contain a control.
  • When the code window displays, enter the seven
    lines of code below

25
Coding Form Load Event Procedure
  • The form Load Event Procedure executes when the
    form first displays on screen. Execution happens
    before any other events.
  • Click the Form1_Load event procedure and enter
    the two lines of code below.

26
Coding the btnReset_Click Event Procedure
  • Click the Form1.vbDesign tab and then
    double-click the btnReset control. Enter the 7
    lines of code below.

27
Option Strict Statement
  • To force you to ensure that all assignment
    statements use the same data type on both sides
    of the assignment
  • In the code window on line 1, type Option Strict
    On

28
Declaring Variables
29
Scope of a Variable
  • Scope refers to a variables accessibility. It is
    defined by the placement of the variable in the
    code.
  • MaximumLoanAmount declared in the general area of
    the form is a global constant
  • Variable declared inside a procedure is a local
    variable
  • Local variable is not accessible outside that
    procedure

30
Global Variable
  • Append a letter g in front of a variable name to
    indicate a global variable
  • Place global variable after the comment header

31
Event Procedures of RadioButton
  • The CheckChanged event procedure executes when
    user clicks a RadioButton
  • Double-click the radTwoYears control on the
    Form1.vbDesign, enter the code
  • Repeat for radFiveYears and radSixYears buttons

32
Declaring Local Variables
  • Click the Form1.vbDesign tab and then
    double-click the btnComputePayment control.
    Enter the code below in the code window.

33
Converting Data Types
  • To ensure that all data being used in the
    calculation has the same data type, we may use
    Convert.ToDouble() method.

34
Arithmetic Operators
  • Arithmetic operator is used to manipulate two or
    more numeric values
  • The Mod operator divides two numbers and return
    the remainder of division operation
  • The \ operator truncates any decimal numbers to
    integers, divides, and returns an integral
    quotient

34 mod 6 is 4 6.6 \ 2.5 is 3 6.6 / 2.5 is 2.64
35
Precedence of Operators
  • Precedence of operators determine the sequence an
    expression is evaluated by a computer
  • An expression is evaluated from left to right
  • (), , , /, \, Mod, , -

(2 3 4 / 5) 2 5 / (4 3 2 3) (2
3 4 / 5) 2 5 / (4 3 8) (2 2.4) 2
5 / 4 (-0.4) 2 5 / 4 0.16 5 / 4 0.16
1.25 1.41
36
In-Class Exercise
  • Evaluate each of the following expressions
  • 4 5 3 / 6 6 2 / 12
  • 7 5 / 2 9 Mod 3 3
  • If necessary, insert parentheses so that each
    numeric expression results in the value indicated
    on the right side of the arrow
  • 8 / 2 2 12 ? 14
  • 7 3 4 2 3 / 13 ? 22

37
Construction of Error-Free Numeric Expressions
  • Do not attempt to divide by 0
  • Do not attempt to take square root of a negative
    number
  • Do not attempt to compute a value larger than the
    maximum a data type may represent

38
Calculating a Monthly Interest Rate
  • Enter the lines of code below and do not press
    the ENTER key

39
Intrinsic Functions
  • Intrinsic functions are VB .NET built-in
    functions
  • The pmt() function is in the financial category
    of the run-time library functions Pmt( rate,
    nper, pv, fv, due)
  • The function Pmt() requires a negative value for
    the present value (pv) argument
  • The function returns a value of certain data type

40
The Format Function
  • The Format function is another intrinsic
    function
  • strPayment Format(dblPayment, Currency)
  • Currency is one of many predefined format name

41
Finish btnComputePayment_Click
42
Summary
  • Use the RadioButton and GroupBox controls, and
    Layout toolbar
  • Set a default button on a form, and lock controls
    on a form
  • Declare and use variables and constants
  • Describe data types and Convert between data
    types
  • Use arithmetic expressions
  • Code a form Load event procedure
  • Describe the order of operator precedence in code
  • Use the intrinsic function like Pmt, and Format
    functions
Write a Comment
User Comments (0)
About PowerShow.com