1 cs 106, winter 2009 class 6, section 4 slides by: dr. cynthia a. brown,...

34
1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, [email protected] Instructor section 4: Dr. Herbert G. Mayer, [email protected]

Post on 19-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

1

CS 106, Winter 2009Class 6, Section 4

Slides by: Dr. Cynthia A. Brown, [email protected] section 4: Dr. Herbert G. Mayer, [email protected]

Page 2: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 3: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 4: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 5: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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!

Page 6: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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”

Page 7: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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”)

Page 8: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 9: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

9

Examples

We’ll pause here and run a few examples

Page 10: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 11: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 12: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 13: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 14: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 15: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 16: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 17: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 18: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 19: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 20: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 21: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 22: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 23: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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)

Page 24: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 25: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

25

Break

10 min

Page 26: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

26

New Assignment

• This will be the first programming assignment• It’s due ---• Let’s take a look….

Page 27: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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.

Page 28: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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)

Page 29: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 30: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 31: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 32: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

32

FlowchartPush Buy button

Input a number?yes no

Handle bad input

end

Compute costAdd tip

end

Print result

Page 33: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

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

Page 34: 1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer,

34

Let’s look at the code…