Please make a use of Debugging Tools and Trouble Shooting Tips.

Please also follow the instruction of how to submit program assignments.

Week 1:

  • Read the Preface of the textbook (xvii - xxxii) . Due by Tuesday, February9.
  • Read and complete Tutorial 1. Due by Tuesday, February 9.
  • Read and complete Tutorial 2. Due by Thursday, February 9
  • SubmitMyRobot Baba. Due on Monday, February 16.
  • E-mail me at from your own e-mail account. I will use the e-mail address to contact you with additional course information. In the message:
  • Write your official name.
  • Specify if you want to be excluded from the Web grades page.
  • If you want to be included, write a grade ID that you can identify your grades.
  • Example:

Subject: I want to be on the Web grade sheet.
My Name: Boom
Grade ID: Nizwa

VB .Net

Debugging Tools

[Display a Value of an Object] [Built-in the Debugging Tool]

To find out what is going on inside of the program, you can use different tools.

Display a Value of an Object to Isolate an Error
  1. Add a TextBox in the GUI to display the value. You may name it as Debug1 as shown below. (Once the debugging is completed, delete it.)
  2. Add a code right next to where you suspect the problem is located. To confirm your hypothesis, add a code that displays the current value of the object (variable or control) to Debug1 TextBox. For example, you suspect sngSalesAmt may be linked to an error. Your code could be: Debug1.Text = sngSalesAmt. It is show below.
  3. Run the program. You will see the current value of txtSalesAmt in the Debug1 TextBox. If the program "supposed" to show other value than what is shows, then you know the problem is before the debugging code. That information should assist you to isolate the error. In the example, if the current value supposed to be other than 2, then you know the error is before the debugging code. To narrow the search for the problem, move the debugging code up a few codes, then run the program again.

Usingthe Built-in Debugging Tool
  1. Using the mouse pointer, click on the gray bar next to the code you want to start the debugging process. Then click on the gray bar next to the code where you want to stop the process. The first bullet indicates the starting position while the second bullet indicates the ending position of the debugging process.
  2. Run the program. You will notice that the program runs as usual up to the point where you inserted the first debug bullet.
  3. From there, press F11 for Step Into.

    In this way, you can see exactly what happens code by code.
  4. Press F11 until the end of the debugging process. The program will resume the "normal" process after the ending point of the debugging point.

VB .Net

Trouble Shooting

[Build Error of Main Sub] [Blue Underline in the Codes Window]

Build Error of Main Sub

  1. When you first run your program you may encounter an error message saying:

    This happens because you changed the form name from Form1 to something else (and that is the right thing to do.) as shown below.

    In this example, I changed the form name to frmSample. Be sure that you change the form name in the Solution Explorer window as well as the form Properties window as shown below.

    This could can be fix by following steps.
  2. After clicking Yes on this window
  3. Click on OK on the following window.
  4. Double-click on the item says 'Sub Main" was not found....
  5. Click on the name of the form that you changed to, and click on OK.
  6. Run the program again.

Blue Underline in the Codes Window

You may see a blue underline under a variable name as shown below. There are two possible scenarios

Undefined Variables

It indicates that the variable is not defined. In the example, I defined a variable as intMyNumber, but I did not define intYourNumber. That is an error since all variables must be defined before use.

Undefined Control

Another example is the undefined control name as shown below.

txtOutput has a blue underline because it has a different name on the GUI as shown below.

To fix it, either change the name in the Properties window, or in the codes window to match the name in the GUI and codes.

How to submit program assignments:

  1. Use appropriate control and variable names using prefixes and meaningfulness. e.g. frmLadyOrTiger, lblFirstName, and strLastName.
  2. At the top of the codes, document
  3. Course ID
  4. Your name
  5. Assignment ID
  6. Date

For example:
'MIC 110
'Pat Petersen
'GPA from chapter 3
'April 1, 2005
Public Class frmLadyOrTiger
Inherits System.Windows.Forms.Form

  1. At the top, document what each procedure and function does. e.g.
    'The procedure converts an input temperature from Celsius to Fahrenheit, and it displace the converted temperature.
    Private Sub btnConvertCtoF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick.Click
    ...
    ...
    End Sub
  2. Document the input, process, and output apects of all important codes. e.g.
    'Compare the input temperature with 100.
    'If the temperature is higher or equal to 100, then display "Very hot!",
    ' otherwise, display "Not too hot!"
    'The intTemperature is from the txtTemp. And the output goes to lblMessage.
    If intTemperature >= 100 Then
    lblMessage.Text = "Very hot!"
    Else
    lblMessage.Text = "Not too hot!"
    End If
  3. Copy and paste the entire codes of the form to a Word document.
  4. Copy and paste screen shots of the application (Alt + Print Screen). Be sure to include before and after shots of each executions. Use reasonable test input data to demonstrate the correctness of the program, e.g. for a program converting a room temperature from celcius to fahrenheit, use -100, -10, 0, 10, and 100 with + and -100 being extrem cases.

MyRobot - Baba

Write a pseudocode and draw a corresponding flowchart for an imaginative program called MyRobot. You are going to design a procedure for a robot named "Baba" which needs the MyRobot program in order to perform tasks. In MyRobot program, you will write detailed instructions for Baba in pseudocode on how to get a morning newspaper from the front yard and bring it to the breakfast table at 6 a.m. everyday except weekends. Getting the newspaper is the first task of the day for Baba.

First, review basic rules of pseudo code and flowchart on the lecture notes.

Baba (object)has limited capacities. It can see, walk, open/close door, lift, release, get the current time and day, and make decisions (method). Using above methods, draw a flowchart, and write pseudo code. The steps of pseudocode and flowchart should match. Of course, you won't be able to write every single detail instruction, such as angles, speed and so on, but that is OK since your don't want to be too detail. You need to reach a balance between excessive and insufficient detail. In some cases you only need a skeleton of the logic and in other cases you may want to show the intricacies of your algorithm. A rule of thumb is to write what is necessary to understand and communicate the essential parts of your algorithm without becoming bogged down in detail.

Flowchart:

Pseudocode

Pseudocode

Objectives:
  • To introduce common words and keywords used when writing pseudocode.
  • Six basic computer operations.
  • To define the three basic control structures as set out in the Structure Theorem, and illustrate the three basic control structures using pseudocode
  • To illustrate converting flowchart to pseudocode and vice versa.

Why use pseudocode?
  • Once pseudocode is created, it is simple to translate it into real programming code.
  • Opportunity to detect any logic error prior to actual coding, which is a lot more expensive and time consuming.
  • Used for planning the programming.

Common words and key words:
  • What can your computer do? Calculate, Read, Print, Convert.....
  • How do you instruct the computer to do what you intend?
  • Keywords: The words that are predefined and reserved to a computer to execute certain instruction. Example: Print, Read, and Compute. Keywords are reserved by a computer programming language, and they contain some particular definition and instruction. Examples in Visual Basic are Private, Sub, End, and Print.
  • Common words: Any other English words to complete the instruction. They include names of variables, constants, file names and so on. Common words are created and defined by a user.

Six basic computer operations:
1. A computer can receive information.
  • When a computer is required to receive information or input from a particular source, whether it be a keyboard, a disk or any other device, the verbs Read and Get are used in pseudocode. Read is usually used when the algorithm is to receive input from a record on a file, while Get is used when the algorithm is to receive input from the keyboard.
  • Example:
  • Read student name (from the student master file).
  • Get system date (from the computer system).
  • Read student ID number.
  • Get order.

2. A computer can put out information.
  • When a computer is requested to supply information or output to a device, the verbs Print, Write, Put, Output or Display are used in pseudocode. Print is used to send the output to the printer, while Write is used for writing to a file. For putting the information to a screen, the common keywords are Put, Display or Output.
  • Example:
  • Print "End of the Output"
  • Write student record to master file
  • Put out name, address and post code
  • Output grade; Display "an input error occurred, please re-enter."
  • In each example, the data to be put out is described concisely using mostly lower-case letters. (Why?)

3. A computer can perform arithmetic operation.
  • A programmer may use actual mathematical symbols or the words for those symbols.
  • For example: "Add score to total_score" is same as "total_score = total_score + score" (programming languages use mathematical equations to assign values to memory locations. In this case, a new value of total_score is assigned to a memory location named total_score by adding score to the current (not new) value of total_score)
  • The following symbols can be used in pseudocode: + for Add, - for Subtract, * for Multiply, / for Divide, ( ) for Parentheses
  • For more example:
  • Divide total_score by student_count
  • class_average = total_score / student_count
  • Compute C = (F - 32) * 5 / 9
  • When writing mathematical calculation for the computer, the order of operation should be considered; otherwise you may end up with incorrect values.
  • The order of operators are following:
  • ( ) : Values within parentheses are always evaluated first. Ex:
  • ^ : Exponentiation (raising a number to a power) is second Ex:
  • - : Negation (creating a negative number) is third. Ex:
  • * and / : Multiplication or division is fourth. Ex:
  • \ : Integer division (a.k.a. Div) is fifth. Ex:
  • Mod : Remainder division is sixth. Ex:
  • + or - : Addition and subtraction are last.
  • Example: For an expression Total = 10 + 15 * 2 / 4 ^ 2 -(2 + 3) , the order of computation is following:
  • Total = 10 + 15 * 2 / 4 ^ 2 -(2 + 3)
  • Total = 10 + 15 * 2 / 4 ^ 2 -(5)
  • Total = 10 + 15 * 2 / 16 -(5) ( 5 as a negative value)
  • Total = 10 + 15 * 2 / 16 - 5
  • Total = 10 + 30 / 16 - 5
  • Total = 10 + 1.875 - 5
  • Total = 11.875 - 5
  • Total = 6.875

4. A computer can assign a value to a variable or memory location.
  • There are three cases where you may write pseudocode to assign a value to a variable or memory locations
  • To give data an initial value. The verbs Initialize or Set are used.
  • To assign a value as a result of some processing, the symbol " = " is used.
  • To keep a piece of information for later use, the verbs Save or Store is used.
  • Example:
  • Initialize total_score to 0: total_score = 0
  • Set student_count to 0: student_count = 0
  • total_score = total_score + score1
  • student_count = student_count + 1
  • class_average = total_score / student_count
  • Store class_average in class_average_quiz1

5. A computer can compare two variables and select one of two alternative actions.
  • For this operation, special keywords are used: If..Then or If..Then..Else.
  • The comparison of data is established in the If clause, and the choice of alternatives is determined by the Then or Else options. Only one of these alternatives will be performed.
  • Example:
If student is part_time Then
add 1 to part_time_count
Else
add 1 to full_time_count
EndIf
If the student is a part-time student, then "add 1 to part_time_count" is performed. Otherwise, the computer skips to the Else clause to perform "add 1 to full_time_count" instruction.
6. A computer can repeat a group of actions.
  • When there is a sequence of processing steps that need to be repeated, keywords Do While and EndDo, are used. And processing stems in between those keywords should be repeated as long as the condition for initiation is met.
  • Example:
Do While student_total < 30
Read student record
Print student name, address
Add 1 to student_total
EndDo
In this example, the set of instruction in the Do While loop will be performed repeatedly as long as student_total is less than 30. And each time computer goes through the loop, the value of student_total will be incremented by 1, in which eventually will make the student_total to be equal to 30, and terminate the loop.
The Structure Theorem: How does a computer know what to do first and to do next? Go to vb book for graphical samples.
  • Sequence:
  • The sequence structure controls the straightforward execution of one processing step after another. For example, within a module, a computer executes the first line of instruction first before the second. And it processes the following instruction in the order of appearance unless it encounters a selection (If..Then) or a loop (WhileDo).
  • Example:
Statement 1
Statement 2
Statement 3 ….
  • Selection:
  • The selection structure (If .. Then) represents the decision-making abilities, and allows a programmer to set what will be the next step depends on the condition.
  • Example:
If condition k is true Then
step x is the next
Else
step y is the next
EndIf
  • As a result, only one step, either x or y, will be performed and the other step will be skipped.

  • Repetition:
  • A loop (Do While) executes a same set of instruction as long as the initial condition for the loop is met. At the end of a loop, a computer goes back to the beginning of the loop and tests the condition. If the condition is met, it goes through the loop one more time. If the condition is not met, then it terminates the loop and continues to execute instructions from the end of the loop.
  • Usually, a loop contains a counter which increments or decrements each time the loop is executed. And the counter with the new value is used in determining whether the look should be repeated or terminated.
  • Example:
The variable student_data acts as a counter.
student_total = 0
Do While student_total < 25
Read student_record
Print student.name
Print student.address
student_total = student_total + 1
EndDo
  • Question: Based on the above codes, if there were 25 students in the file, would this algorithm print all 25 students? Count how many time the loop occure.
Modification:
student_total = 1
DoWhile student_total <= 25 And student_record > " " Read student_record
Print student.name
Print student.address student_total = student_total + 1
EndDo
Convert the following flowchart to pseudocode.

Pseudocode Example
This is the pseudocode for a Game of Monopoly, including one person's move as a procedure:
Main Procedure
Monopoly_Game Hand out each player's initial money. Decide which player goes first. Repeat Call Procedure Monopoly_Move for next player. Decide if this player must drop out. Until all players except one have dropped out. Declare the surviving player to be the winner.
Procedure Monopoly_Move
Begin one's move. Throw the dice. Move the number of spaces on the board shown on the dice. If the token landed on "Go to Jail," then go there immediately. Else if the token landed on "Chance" or "Community Chest," then draw a card and follow its instructions. Else follow the usual rules for the square (buying property, paying rent, collecting $200 for passing "Go", etc.). End one's move.

[5.3] Pseudocode Example

This is the pseudocode for a Game of Monopoly, including one person's move as a procedure:

Main Procedure Monopoly_Game

Hand out each player's initial money.

Decide which player goes first.

Repeat

Call Procedure Monopoly_Move for next player.

Decide if this player must drop out.

Until all players except one have dropped out.

Declare the surviving player to be the winner.

Procedure Monopoly_Move

Begin one's move.

Throw the dice.

Move the number of spaces on the board shown on the dice.

If the token landed on "Go to Jail,"

then go there immediately.

Else if the token landed on "Chance" or "Community Chest,"

then draw a card and follow its instructions.

Else

follow the usual rules for the square (buying property,

paying rent, collecting $200 for passing "Go", etc.).

End one's move.

Pseudocode Example

Monopoly Flowchart