Logistics
- Roll
- Today: Unit I recap & Comp.Demo; Unit II Intro & Demonstration
Assignments
Hopefully, you created the project for the Unit I practice. I recently posted the Unit II assignment description. We're starting Unit II stuff. Remember to be thinking about what you would like/can do for a project. Feel free to talk with me about ideas you might have (I can suggest whether they are too small, too big, or just right).
We will start Unit II practice today and you should work on it outside of class, bring questions next time (we'll work on it some more) and try to be done with it before class time next Tuesday (or have questions on anything not yet done).
Questions over Unit I?
If you are having any difficulty or do not feel comfortable with what we have been doing, now is the time for questions. You will attempting a competency demo (last 30 minutes of class today) and will need to be able to:
- create a project with a specified name
- rename forms
- place specified elements/controls on the form in a reasonably spaced manner
- change various properties of the form, particularly the Name property using standard name prefixes
- create a second form in the same project
- Save All the project
- compress the project folder and change the extension to
.zipped
- e-mail the compressed folder to me
If you have questions, ask now. Questions? Wonderings?
CD I.a.1
(We didn't get as far as planned.)
Display competency demo I.a.1 on the projector. Students work on it for the rest of the period.
Next Time
- Introductory info on Unit II; Unit II practice
- Questions? Wonderings? Comments?
Day 6 — Unit II: IPO
Logistics
- Roll
- Today: Unit II Intro & Demonstration; work time
Do you have any questions or comments about the Unit I competency demos?
Unit II
Unit II introduces the common data and operation parts of programming. The data parts are literals, variables, and form element properties. They will mostly be numeric or text string (and some Boolean—True & False). The operations for numbers include addition, subtraction, multiplication, division, exponentiation, & modulus determination. The main operation for strings is concatenation (putting strings together to make another string).
Unit II also addresses the basic actions of programming—input, output, and process (typically using the assignment statement).
Once you become familiar with data, operations, and the three basic actions programming is just a manner of putting them together appropriately. It will be helpful to be thinking about storage and values. Storage (form element properties and variables) is where values can be placed or looked up. Values can be built or produced and then are often placed in storage. Values can also displayed to the user or put in a file (output). Finally, values can come from the user (input) and be placed in storage. Let's look at storage and values for each of the actions.
Assignment Statement
An assignment statement has the following form:
memoryLocation = expression
The assignment statement is basically two actions. First the computer has to determine the value for the expression which can be any "legal" combination of literals, variables, and operations on (pairs of) variables. Once the value is determined the second action can occur—it is copied into/stored in the memoryLocation for later use.
We typically do not read the assignment statement using the word "equals". Rather we use "becomes" or "takes the value of" or other similar word/phrase. An example shows why. We will often use code similar to count = count + 1
. Clearly count
cannot be "equal to" count + 1
. I may forget and use "equals" sometimes but it is really important that you know, understand, and think correctly about the action of the assignment statement. It is "the" most fundamental action in programming.
Variables will be defined by the programmer (typically) with a Dim
statement which will provide the name of variable and the kind of data that it can store. Properties belong to the form or form element and they are defined by the language. You can identify them by looking at the property list of a form or a control. A common property that can receive a value is the .Text
property of text boxes and other controls. Expressions are combinations of literals, variables, functions, control properties, and operators. The declaration and assignment statements below illustrate these ideas.
Remember, your job is to understand this stuff and to ask questions when you don't understand. Questions? Wonderings? Comments?
Dim intVal As Integer 'declare a variable to store whole numbers
Dim fracVal As Double 'declare a variable to store a number with a fractional part
Dim strVal As String 'declare a variable to store letters, digits, & special characters
'literals as the expression
intVal = 1
fracVal = 1.5
strVal = "1"
'literals and operators
intVal = 10 - 2 * 5 ' 0 is stored
intVal = 10 / 5 + 2 ' 4 is stored
intVal = 10 / (5 + 2) ' 1 is stored Do you know why?
intVal = 10 ^ (1/2) ' 3 is stored Do you know why?
intVal = 10 Mod 3 ' 1 is stored Do you know why?
strVal = "two" & "2" ' two2 is stored
The computer has rules for the order in which the operators are applied:
- parentheses first
- exponentiation next
- multiplication and division next
- modulus next
- addition and subtraction next
If you have a unary minus or plus in an expression (e.g., -5 + 7
) it will get applied before any of the other operations. You will need to be sure you understand how expressions get evaluated.
Variables could occur in any of the places we had literals in the examples above. A similar thing happens. The values stored in the variables are looked up and placed where the variable name occurs. Then the expression is evaluated as if the values were literals.
Questions? Wonderings? Comments?
Getting Data
There are essentially two ways to accept input (get data from the user). They can type data into a text box or they can enter data via an InputBox()
. Both are shown below.
- Text box data input
When a user types something in a text box, they change the value of the box's Text property. Your code will need to get a copy of that value. The following code stores/saves a "string" or text value from a text box named txtInput1
.
Dim strVal as String
strVal = txtInput1.Text
VB will automatically attempt to convert whatever is in the text box to a number when you assign the .Text
value to a numeric variable. I prefer (but sometimes forget) to use the Val()
function to explicitly make the change and remind me of what is happening. The following code stores/saves/copies a fractional numeric value typed into a text box. (I'm assuming the same text box.)
Dim fracVal as Double
fracVal = Val(txtInput1.Text)
The following code stores/saves/copies an integer numeric value typed into a text box. (I'm assuming the same text box.) Note: the value is rounded when it is converted.
Dim intVal as Integer
intVal = Val(txtInput1.Text)
- InputBox data input (Forgot this first time.)
You have probably seen input boxes before. The following code stores/saves a "string" or text value provided via a VB input box. The text in the quotes indicates the value being asked for.
Dim name as String
name = InputBox("Enter your name")
When the box shows up on the screen you type something and hit the button and the data gets stored in the variable.
Questions? Wonderings? Comments?
Functions
You are probably familiar with function from your use of calculators. For instance, most calculators have a square root function. Many have other functions. Calculator functions perform set of calculations to determine a value and then return that value. VB has a lot of functions. (You can look them up if you want.) I think the practice exercises (and your project) will use many of the following (but perhaps not many more).
Rnd()
Generates a "pseudo random" number in the range [0...1). The value generated will be >= 0 and < 1. Usually, you will have to perform some arithmetic manipulation of the value to get a useful number. What tasks might we do using the Rnd()
function? Questions? Wonderings? Comments?
Fix()
Returns the value with any fractional part removed. Fix(5.2)
produces 5 and Fix(-5.2)
produces -5. For what tasks might the Fix()
function be useful? Questions? Wonderings? Comments?
Ceiling()
Returns the next greater integer, e.g., Ceiling(7.1)
produces 8. What tasks might we do using the Ceiling()
function? Questions? Wonderings? Comments?
Floor()
Returns the next smaller integer, e.g., Floor(7.999)
produces 7. For what tasks might the Floor()
function be useful? Questions? Wonderings? Comments?
Math.PI
Returns the value used by the computer for the number pi. When might we use the PI
constant? Questions? Wonderings? Comments?
Length
(actually a property, not a function, but used similarly to string functions)
Provides the number of items in a thing. Strings have a length—the number of characters contained in the string. A key idea with length is that it tells the number of things but not the position (at least not directly). For examples, the string "Hello there!" contains 12 characters. The first character is "H" and it occurs in position 0 of the string. If were were to try to get the character in position 12 character of that string, we would have an error. Keep in mind that the 1st character is in position 0 and the last character is in position Length - 1
. What tasks might we do with the Length
property? (FYI, the is also a length function Len()
.) Questions? Wonderings? Comments?
Substring( , )
Produces/returns a copy of a portion of a string. For example, ("Hello World!").Substring(3, 6)
returns lo Wor
. The copying begins in location 3 (the first location is numbered 0) and includes 6 characters. For what tasks might the Substring()
function be useful? Questions? Wonderings? Comments?
IndexOf( )
Examines a string to see if it contains the indicated value. For example, ("Hello World!").IndexOf("W")
returns 6
, the starting location of the string "W". ("Hello World!").IndexOf("lo")
would return 3
, the "starting" location of the string "lo". ("Hello World!").IndexOf("H")
would return 0
. ("Hello World!").IndexOf("wo")
would return -1
. Upper/lower case matters and if the target string is not found, the function returns -1. For what tasks might the IndexOf()
function be useful? Questions? Wonderings? Comments?
Unit II Practice/Learning Activity
Unit II is about learning the basic actions of a program. Those actions are input, process, and output. In VB input typically occurs as people enter something in a text box, or click or some control. Processing will be taking the input (or finding out what was clicked on), doing something, and then producing output. Output in VB will often be putting a value into a text box or showing something in a message box. Let's look at the Unit II stuff to demonstrate some of this.
The Unit II practice activity is available on the course site via http://www.cs.uni.edu/~east/teaching/vb/current/unit-II_practice.html. We worked on a VB project that includes forms you can use to write your code. I compressed the project I made and it is available in the projects_etc folder in the notes directory. You'll need to download and unzip the file (VB-practice_II.zip), then find and start the project. Let's do that so you'll know how later.
Let's do that so you'll know how later.
- Download the file
- Find the file and move or copy it to the desktop (or wherever you wish to work on it.
- Right-click on the file name
- Select Extract here
- Open the folder that was created
- Find and click on the VB-practice.sln file
(Once everyone seems ready)
- explain the nature/structure of the activity
Your task is to write code that solves the indicated problem. Typically you will look at the next problem, decide how to approach it, identify the variables needed and how to get the data, figure out the needed manipulations (calculations or function operations), write the code, and test the code. If it works, great—comment out this code and go on to the next problem. If it does not work try to figure out what the problem is. After a reasonable and unsuccessful attempt to debug your code, send me a message with a copy of your code, comment out the code, and move on to the next problem.
- start working some of the exercises
- #1
We need to calculate gross pay. That is fairly simple, just multiply hours time pay rate. But we have to "get" the data before we can use it. What kind of data? Well, ... hours might be something line 30.5 and pay rate probably is not an even dollar amount, so we need Double variables for both values.
'# 1
Dim hours, payRate, grossPay As Double
hours = Val(txtInput1.Text)
payRate = Val(txtInput2.Text)
grossPay = hour * payRate
Now we need to report the results. Either of the program statements below will accomplish this. Note that I have identified what the value being reported stands for. Text boxes could use labels for that but we didn't so I am including it in the value reported.
Dim result As String = "Gross pay is: " & grossPay.ToString
txtResult.Text = result
MessageBox.Show(result)
Questions? Wonderings? Comments?
- #2
Now we start the next problem, but we need to keep the previous code from getting in the way. We do that by commenting it out. Show it. (Select the lines you want commented out and click on the comment out icon. Note that there is an un-comment out icon also.)
Instead of me showing you how to do more, keep going.
- Questions? Wonderings? Comments?
Next Time
- Questions over Unit II; work on Unit II practice
- Questions? Wonderings? Comments?
Logistics
Unit II Practice Examples
The intent of today's class was to respond to student questions about the Unit II practice activity and then to proceed to having them share their solutions to some of the items.
A number of the items were discussed. For some the solution approach was identified but no code produced. For some, the solution was discussed and code to implement the solution was produced.
A key idea to be gleaned from the discussion is that thinking about solving the problem, then thinking about coding is useful. Similarly, experience in using the basic numeric and string operators and functions is necessary if we are to solve bigger problems later on.
When questions were no longer asked there was only about 25 minutes of class time remaining, so we class time was devoted to getting closer to done with the all the items.
Next Time
- Review/share Unit II work (student's randomly called on to share code, bring it to class)
- Questions? Wonderings? Comments?
- If time, CD-IIa