Unit II:  CS 1130 (VB)

J. Philip East — Fall 2017

Day 4 — Unit II: IPO

Logistics

Unit I

Hopefully, you created the project for the Unit I practice.   Do you have any questions or issues with the assignment?   [ Addresss any responses ]

Unit II Overview

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. A key idea is the concept of variables. Please review my background information on variables to provide a good understanding of variables from a computer science perspective.

I've noted before that the goal of computer programs is to produce some needed information or to oversee some activity. Our goal is in this program is to oversee the activity of producing a number of bits of information.

Actions

There are essentially three actions that computers can do. The can accept input (typically from some user), produce output (again, often to a user), or they can manipulate data (often input from the user).

Data input

Input from the user in Visual Basic is accepted through the use of an InputBox() in our code. We indicate what we wish the user to enter, e.g., InputBox("Name?") and the inputbox function shows a pop-up window requesting the information which allows the user to type in the desired information.

Alternatively, we can get input through the use of form controls, mostly the text box control. The user types something in a textbox and we write code that gets a copy of what was typed in the textbox.

Just getting the input doesn't accomplish much, we need to do something with it. That is addressed below.

Data manipulation (assignment statement)

Manipulating or process of data typically happens via the 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 and functions. 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.

One difficulty in thinking about assignment statements is the fact that variables can appear on both sides of the assignment operator (the equal sign). When we see a variable on the right-hand side of the equal sign, we should think of it as a value. When we see a variable on the left-hand side of the equal sign, we should think of it as a storage location. For example in the statement above (count = count + 1) we should interpret count on the right side as telling the computer to find the location called count and place its value here. And, for count on the left side as telling the computer, once you have determined a value, put a copy of it in the location called count.

I think of variables as being names on scratch paper that I either go look at to see what value is there (variable to the right of the equal sign) or a place where I erase or cross out the value that is there and place a new value (variable to the left of the equal sign). (I suspect that may be a little confusing.) Any questions/wonderings?

Output

The third kind of action is output. In Visual Basic, output typically occurs through the use of a message box (a pop up window) or by placing information in some control on a form (e.g., in a text box). For both these approaches the output will be a "string". The string value to be reported/output can be stored in a string variable or constructed as part of the output action. If the value is stored in a variable, an assignment statement (discussed above) will be used. If the value is constructed as part of the output action, it will be constructed following the rules used in making assignments (discussed below).

Output via a message box is similar to using the input box. You just indicate the value and ask the message box object to show it, i.e.,

    MessageBox.show(stringVariable)

If you wish to construct the output value it will appear as something like

    MessageBox.show("This string will be displayed in the message box")

or

    MessageBox.show("The correct answer is " & answer.ToString)

The "stringVariable" shown in the first chunk of code would require constructing the value before asking the message box to show it. Constructing string values can be much more involved than shown here but would be essentially the same whether you do it before assigning the value to a variable or by just including the construction as the output occurs.

Output via a text box is much the same as the assignment, i.e.,

    txtVariable.Text = stringVariable

If you wish to construct the output value it will appear as something like one of the following.

    txtVariable.Text = "This string will be displayed in the message box"

or

    txtVariable.Text = "The correct answer is " & answer.ToString

Variables and Expressions

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 Do you know why?
	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 Do you know why?

The computer has rules for the order in which the operators are applied:

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?

Input data and assignment

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.

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 others).

Functions return values. When you see a function name you should think of it as a value. You will need to understand what that value is and what part the parameters used in the function call (inside the parentheses) play in determining that value.

Next Time

Day 5 — Unit II: IPO work day

Logistics

Unit II Intro (continued)

I'm going to discuss the "string" functions/methods that we did not get to last time. But first,   Any questions on variables, the assignment statement, numeric operators, numeric functions?   Any questions on variables, the assignment statement, numeric operators, numeric functions?   [ respond to questions ]

String data, operators, and functions

Computer programs don't only deal with numeric data; sometimes they process string information like names, addresses, etc. Additionally, Visual Basic output is typically done with strings. Thus, it is critical that you be able to work with string data. Your next assignment will involve string data and reporting results. (You can refer back notes for last time for additional info on the following.)

(Converting and) Reporting results

Since most output requires what is being communicated be a string value, it is important to know how to build strings. We will see examples of doing so as we look at examples items for the exercises.

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 notes directory. You'll need to download and unzip the file (vb_unit-II_start.zip), then find the solution (.slnand start the project. Any questions on doing that?

(Once everyone seems ready)

(at 4:05 pm) Unit I Competency Demo

[ Have students save their work and prepare for CD-Ia ]

You have the rest of the class period (and perhaps longer if needed to complete the competency demo for Unit I. I have directions for the task here.   [ Hand them out. ]   Questions?   Okay, go to work. If you have difficulty understanding what is desired please ask. The goal here is to see if you can do the work, not to see if you can understand my directions on first reading.

Next Time

Day 6 — Unit II Practice

Logistics

String formatting

For those of you who want to be able to control how your output looks, ... You can format numbers in a variety of ways. We mentioned currency, but it is not the only choice. Some others are included below plus you can build you own formatting descriptor if you wish.

All the formatting options are indicated inside quotes in the ToString() function, e.g.,   ,   ,   ,   ToString("#,##0.00"). The letters specifying the kind of formatting can be either upper or lower case.

Questions? Wonderings? Comments?

Students work on Unit II practice activity

The rest of class time today will be used to work on the learning activity. An almost-complete solution report is available in the notes directory (under the title Unit-II_Report.txt. You can use it to check that your code produces the same result as mine (and, thus, we hope, is correct).

But first, do you have any questions or wonderings about the assignment or VB or Visual studio?   [ Address any questions students have. ]

Next Time

Day 7 — Unit II Questions (& CD?); Unit III Intro ?

Logistics

Questions RE Unit II

Any questions or issues with the Unit II learning activity?   [ Respond to any student questions over Unit II. ]

Sample competency demo

I have not shown you a sample competency demo for Unit II, so we'll do that now and you can ask questions. [ Show sample CD II and respond to questions. ]

Work time on Unit II

[ Students work on Unit II (or the sample comp. demo items. ]

Next Time

  • Questions? Wonderings? Comments? See you Thursday.
  • -->