340 likes | 475 Views
CS 106, Winter 2009 Class 6, Section 4. Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer, herb@cs.pdx.edu. 1. Numeric Data Types. Double Covers a huge range of values Used for almost all computations
E N D
CS 106, Winter 2009Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer, herb@cs.pdx.edu 1
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
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
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
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!
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 follows Dim professor as String professor = “Cindy Brown”
String Operations: Concatenation DimaString, bString, cStringas String aString = “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”)
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
Examples We’ll pause here and run a few examples
Other Data Types • Boolean: named after logician George Boole • Can take only one of two values: True and False DimvarNameas Boolean • There are several other data types, which we will introduce if we need them
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
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
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 Double numVar = 124 txtBox.Text = CStr(numVar + 2) ‘convert to string
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
Global vs Local Public Class formDemo DimvarAas Double‘global variable Private Sub butRed_Click(…) HandlesbutRed.Click DimvarB as String‘local variable <some code> End Sub End Class
Tricky Case Public Class formDemo DimvarAas Double = 1‘global variable Private Sub butRed_Click(…) HandlesbutRed.Click DimvarA as Double = 1‘local variable varA += 1 ‘local varA now = 2, global varA still = 1 End Sub Private Sub butBlue_Click(…) HandlesbutBlue.Click DimvarB as Double‘local variable varB = varA‘local varB now = 1, using global varA End Sub End Class
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
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
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
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
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
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
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)
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
Break 10 min
New Assignment • This will be the first programming assignment • It’s due --- • Let’s take a look….
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.
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)
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
Use case 2 • The user has not entered anything in the Scoops window • The user pushes the Buy button Experiment 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
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
Flowchart Push Buy button yes no Input a number? Compute cost Add tip Handle bad input end Print result end
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