View
214
Download
0
Embed Size (px)
Citation preview
1
CS 106, Winter 2009Class 6, Section 4
Slides by: Dr. Cynthia A. Brown, [email protected] section 4: Dr. Herbert G. Mayer, [email protected]
2
Numeric Data Types
• Double– Covers a huge range of values– Used for almost all computations– Can lose some accuracy in long computations (not
a concern for the programs we do in this class)• Integer– Used for counting– Always precise– Smaller range of values than Double
3
Types of Results
• Adding two integers gives an integer, adding two doubles gives a double
• Dividing two integers using / gives a double!• Which is why we have \ and mod: 14 \ 4 = 3, and 14 mod 4 = 2• The exponential function always gives a double
as a result• VB will change integers into doubles in mixed
computations, but not the reverse
4
String Data Type
• Besides numbers, strings are the other kind of data we will usually be working with
• Strings are strings of characters, so let’s look at characters first
5
Characters
• 256 characters used to be enough for upper and lower case letters, numbers, punctuation, and a few oddities
• The 8-bit byte was the standard character size, in the ASCII code
• Now we want Chinese, Arabic, etc so the new system has 16 bits, allowing for 216 = 65,536 characters. The representation system is called Unicode.
• It’s very important to have standard representations for characters!
6
Strings
• A string is a sequence of characters, typically stored in several words as a length and a list of characters (details vary)
• Strings are declared as followsDim professor as Stringprofessor = “Cindy Brown”
7
String Operations: Concatenation
DimaString, bString, cStringas StringaString = “Hello”bString = “Dolly”cString = aString&“ “&bString‘insert a space
The value of cString is now “Hello Dolly”• You can also use aString&= “ “ &bString
(this changes aString to “Hello Dolly”)
8
More String Operations
• There are plenty of useful string operations– str.Length‘length of the string– str.ToUpper‘convert to upper case– str.ToLower‘convert to lower case– str.Trim‘remove leading and trailing blanks– str.Substring(m,n) ‘substring length n starting at m– str.IndexOf(str2) ‘start of str2 in str
• Here str is the name of a String variable. See page 82 for examples
9
Examples
We’ll pause here and run a few examples
10
Other Data Types
• Boolean: named after logician George Boole• Can take only one of two values: True and
FalseDimvarNameas Boolean
• There are several other data types, which we will introduce if we need them
11
Strict and Explicit
• You should turn on the options for Strict and Explicit in the VB system (see book pg 78)
• This will make the system enforce declarations for variables, and will make you explicitly convert values to different types when needed
• This helps by catching errors and is a very good programming practice
12
Data Types
• We have learned about several data types• Double: for general computation• Integer: for counting• String: for working with text• Boolean: True and False
• Variables must be declared as one of these data types (or some other valid type), using a Dim statement
13
Casting
• Converting information from one data type to another is called casting. (CInt, CStr)
• Casting is most often needed when printing numeric values in text boxes or list boxes, or reading them from text boxes
DimnumVaras DoublenumVar = 124txtBox.Text = CStr(numVar + 2) ‘convert to string
14
Scope of Variables
• Variables can be declared within a sub-procedure or function, or at the level of the class
• A variable that is declared at the level of the class (also called a global variable) can be seen by all the sub-procedures and functions, and is in existence as long as the program is running
• A variable declared within a sub-procedure or function (also called a local variable) can only be seen there, and exists only as long as the sub-procedure or function is running
15
Global vs Local
Public Class formDemoDimvarAas Double‘global variable
Private Sub butRed_Click(…) HandlesbutRed.ClickDimvarB as String‘local variable<some code>End Sub
End Class
16
Tricky CasePublic Class formDemoDimvarAas Double = 1‘global variable
Private Sub butRed_Click(…) HandlesbutRed.ClickDimvarA as Double = 1‘local variablevarA += 1 ‘local varA now = 2, global varA still = 1End Sub
Private Sub butBlue_Click(…) HandlesbutBlue.ClickDimvarB as Double‘local variablevarB = varA‘local varB now = 1, using global varAEnd SubEnd Class
17
Types of Errors
• As you start to write code you will encounter errors
• This is a natural part of programming. Very few people can write a perfect program the first time
• There are three major types of errors– Syntax errors– Runtime errors– Logic errors
18
Syntax Errors
• A syntax error is a mistake in the form of the program: writing something that is not a legal VB statement
• The editor will catch syntax errors and try to figure out what the problem is
• You cannot run your program if it has any syntax errors in it
19
Runtime Errors
• With a runtime error the program will start to run, but then something happens that the system can’t handle, and you get a runtime error message
• Typical causes include dividing by zero (dividing by a variable whose value has become zero) or having a number become too large to fit in its data type
20
Logic Errors
• With a logic error, the program runs fine but produces a wrong answer (an answer you didn’t expect) or behaves in a way you didn’t want it to
• Reading over the code may show you the source of the error
• As with runtime errors, using the debugger is often the quickest way to identify where the error takes place
21
The Debugger
• Pages 617-618 have a quick overview of how to use the debugger
• Appendix D has a nice, thorough discussion with an example. I strongly suggest you work through that example and read the rest of the appendix
• This will save you a whole lot of time in the long run
22
Formatting Output
• Output can appear in a text box, list box, or file
• Formatting lets us present neatly arranged columns, control the number of decimal places shown, etc.
• Output is almost always a string. There are cases where people make files of other data types, but we will not encounter those
23
Formatting Sampler
• FormatNumber(123.628, 1): 123.6• FormatCurrency(123.628,2): $123.63• FormatCurrency(-1000): ($1000)• FormatPercent(0.185,2): 18.50%
• See the book for formatting with zones: how to make nice neat columns (pg 97)
24
Postponed Topic from Chapter 3
• Reading data from files and writing to files is very important
• It’s the only way to have a permanent record of what happened in your program
• We’ll skip it for now, come back if we have time
25
Break
10 min
26
New Assignment
• This will be the first programming assignment• It’s due ---• Let’s take a look….
27
Class example
Create an order process for an ice cream store. The user should be able to choose the number of scoops to buy. Each scoop costs $.75. After the user chooses the number of scoops, the program computes the cost, adding a 15% tip for the server.
28
Interface Objects
• Place to enter the number of scoops (txtScoops)
• Place to display the price (txtPrice)• A Buy button (butBuy) [I thought of this while
doing the use case…how will the program know the user is finished entering the number?]
• Information for the user (labels)
29
Use Case 1
• The user enters a number of scoops in the Scoops window
• The user pushes the Buy button• The program figures out how much to charge
by taking the number of scoops times .75 and adding the 15% charge
30
Use case 2
• The user has not entered anything in the Scoops window
• The user pushes the Buy buttonExperiment shows the program blows up with a runtime error. Once we have conditionals we can test for this (Chapter 4)Experiment shows a non-number causes a blowup too
31
Objects and Events
• Scoops text box– User enters a value
• Buy button– User pushes the button
• Price window– Program writes in the window
• The only really non-trivial one is pushing the Buy button
32
FlowchartPush Buy button
Input a number?yes no
Handle bad input
end
Compute costAdd tip
end
Print result
33
Variables and Constants
• What does the program need to remember?– How much a scoop costs– What percent the tip should be– How many scoops the user is buying
34
Let’s look at the code…