1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.

Slides:



Advertisements
Similar presentations
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C Programming
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Data types and variables
Introduction to C Programming
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Variables & Math Operators CE 311 K - Introduction to Computer Methods Daene C. McKinney.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 8: String Manipulation
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Expressions, Data Conversion, and Input
Objectives You should be able to describe: Data Types
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.
Dani Vainstein1 VBScript Session 9. Dani Vainstein2 What we learn last session? VBScript coding conventions. Code convention usage for constants, variables,
Variables and Constants
CS0004: Introduction to Programming Input and Output.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
CS0004: Introduction to Programming Variables – Strings.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Visual Basic.NET BASICS Lesson 4 Mathematical Operators.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2 Variables.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
A variable is a name for a value stored in memory.
Topics Designing a Program Input, Processing, and Output
Expressions.
An Application Uses Variables to Hold Information So It May Be Manipulated, Used to Manipulate Other Information, or Remembered for Later Use.
2.5 Another Java Application: Adding Integers
Variables and Arithmetic Operations
Variables, Expressions, and IO
Introduction to Scripting
Chapter 3 Data Types, Variables, and Expressions
Variables and Arithmetic Operations
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Core Objects, Variables, Input, and Output
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Presentation transcript:

1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls (list boxes, text boxes) These examples illustrate the concepts discussed earlier

Example

3 This is a ListBox called lstResults

Example Variable declarations Variable a is uninitialized so starts with a value of 0. Variable b is initialized to 3.

Example Calling methods of the ListBox object’s Items property The Clear method empties the Listbox. The Add method adds a row to the ListBox.

Anatomy of a Method Call 6 lstResults.Items.Add(a) The ListBox The Items property contains the collection of data that are displayed in the ListBox The Items property’s Add method is a subroutine that places an item into the collection When calling the Add method, you pass it the data as an argument.

Example Assigning a value into the a variable

Example Adding a third item to the list

Method Call passing a complex expression as an argument 9 lstResults.Items.Add(a * (2 + b)) An argument can be a complex expression. The expression will be fully evaluated before the resulting data is sent. In this case, the following steps take place in this order: 1)2 + binnermost parentheses  5 2)Multiply a times the result if (1)  25 3)Pass the result of (2) to the Add method  Order of operations from innermost to outermost based on parentheses

Example

Example This is a ListBox called lstResults

Example Variable declarations

Example Variable assignments

Example Empty the list box items

Example Add three values to the list box

Order of operations 16 lstResults.Items.Add(Math.Sqrt(5 * b + 1)) Add to list

Order of operations Add to list lstResults.Items.Add(Int(a ^ b + 0.8)) The Int function truncates the number…it will not round up, but rather just chops off the fractional part.

Order of operations … Add to list lstResults.Items.Add(Math.Round(a / b, 3)) The Math.Round method rounds the number either up or down, depending on which rounded value is nearer. It can take two arguments: 1)The number to be rounded 2)The total number of decimal places for the rounded number If the second argument is not provided, 0 is assumed. There will be no decimal places, so the result will be a whole number. Multiple arguments to methods are separated by commas.

Example

Example This example converts 41 inches into 3 feet, 5 inches

Example Variable declarations…three in one statement

Example Backslash is for integer division. Truncates the fractional part. So, feet = 3

Example Mod gives the remainder of an integer division. So, inches = divided by 12 is 3, with a remainder of 5

Example This is a string literal, which will be displayed literally. This is a string variable, so its contents will be displayed

Example

26 Example This example adds the values in two textboxes and places them in a third

Example The CDbl function takes a string value and attempts to convert it into a Double value. Note: the string must consist of digits (and perhaps one dot for a decimal point). Otherwise an exception (run-time error) would occur.

Example The CStr function takes a numeric value and converts it to a string. This is necessary because a TextBox’s Text property requires a string value.

3 Program Modes Design mode Run mode Debug or Break mode 29

Stepping through program Execute one line of code at a time (Stepping into) Execute one procedure (Stepping over) Execute remaining lines of code (Stepping out) Execution will stop a pre-specified line of code (Break point) Hoover to see value of particular variable or object 30

Stepping through program 31 Each line is highlighted before execution Hoover to see values Num1 & both Textboxes num2 is 0 – Why?

Break points 32 Set a break point & run Program stops Hoover to see values

Example

Example Declaring three String variables

Example Assigning String literals into String variables.

Example Concatenating the contents of two String variables and assigning them into another String variable Note: remember that the right side of an assignment statement is an expression. In this example, the expressions are String expressions.

Example The final result placed in the Text property of txtOutput is the result of another concatenation

Example This example illustrates the use of some String methods and properties.

Example The Substring method takes two arguments: 1)The beginning position of the substring (first position of the string is 0) 2)The length of the substring (number of characters to return)

Example This IndexOf method returns the first position of a substring within a string.

Example This ToUpper method converts the characters of a string to upper case. There is also a ToLower method In this case, the string expression is a concatenation. Note that (str1 & str2) is a concatenation of two strings. Because this is in parentheses, the concatenation occurs BEFORE the conversion to upper case. What would happen if you did not have the parentheses around str1 & str2?

Example The Trim method removes beginning and end spaces from a string. Here, the trim takes place for str1, and the result is concatenated with str2

Example The Length property of a string gives the total number of characters in the string. As you can see, str2 contains “a wink”, which has a total of six characters In this case, the Substring method is only taking ONE argument (str2.Length – 4)  2. If a second argument is not provided Substring returns the remainder of the string, starting at the specified beginning position So, at the end of the assignment statement, str3 contains the string “wink”.

Example Note: These methods (SubString, IndexOf, Trim, ToUpper) return a string expression that can be used for display or assignment. But they do NOT change the original contents of the variables str1 and str2. Only an assignment changes them. So, at the end of this program, the contents of str1 and str2 are still “Quick as ” and “a wink”.

Example It is good programming practice to include comments in your code in order to explain what is being done.

Example In previous examples, the variables were declared INSIDE the procedure. Here, it is declared OUTSIDE any subroutines. Two things to note about class-level variables: 1)Their data remain in existence throughout the entire time that the Form is running 2)Their data is accessible by ALL subroutines and functions of the form

Example Compare previous to what would happen if numTimes were declared INSIDE the procedure. In this case, numTimes would only exist as long as the subroutine was running, and would disappear when it ended. So, EVERY TIME you run the routine, it will reinitialize to zero. Wouldn’t be able to keep a count of how many times the button was pushed!

Example Working with dates In this case, we have declared a variable as a Date.

Example Note that we have set a mask in the mtbDayOfBirth MaskedTextBox control. The MaskedTextBox’s Mask property helps guide the user and prevent invalid input. Here we guarantee that only numbers are entered separated by slashes

50 Masked Text Box Control Click on the Tasks button to reveal the Set Mask property. Click Set Mask to invoke the Input Mask dialog box.

51 Input Mask Dialog Box

Mask A Mask setting is a sequence of characters, with 0, L, and & having special meanings. 0 Placeholder for a digit. L Placeholder for a letter. & Placeholder for a character 52

Sample Masks State abbreviation: LL Phone number: Social Security Number: License plate: &&&&&& Date: 00/00/

Example The CDate function converts a String to a Date

Example The FormatDatetime takes a date and formats it four output. Options are LongDate, ShortDate, GeneralDate, Longtime, and ShortTime

Example The DateDiff function gives the time gap between two dates. You can specify which intervals you want: days, months, etc.

Example FormatNumber allows you to display a number in a wide variety of formats. In this case we are specifying to display only the whole number part of the difference. As always, use parentheses to determine the order of operations that take place…inner to outer.

Formatting Numbers Lots of approaches: FormatNumber is good for getting the correct number of places to the right of the decimal, and has other options for how to display negative numbers, etc. FormatCurrency is a great way to show dollars and cents. For example, try this: FormatCurrency(varName) Where varName is the name of a numeric variable 58