To Notes

Lecture Summary for IT 236 on 6/27/06

 

Review Questions

  1. What is wrong with these two lines?
    y = a + b + c + d +_
        e + f + g + h
    Ans: The underscore must have a space before it to be a new line character.

  2. Why does ReadLine( ) on a line by itself work to hold the window for the user to read in a console application?
    Ans: Because when the ReadLine method is called, the application must wait for the user to enter the information at the keyboard before continuing. The ReadLine at the end of the application tricks the program into waiting for the user to press the Enter key, then throws away the information and terminates.

  3. Write a line that declares a form-wide global variable named count, initalized to 0.
    Ans: Private count As Integer = 0

  4. Write code to show a message box with message "Your lucky number is #." (replace # with the value of n), caption "Lucky Number", OK button only, and display the information icon. Ans:
    MessageBox.Show("Your lucky number is " & n, _
        "Lucky Number", MessageBoxButtons.OK, _
        MessageBoxIcon.Exclamation)

  5. True or false. All of the items in an array must be of the same type.
    Ans: True. An array is declared to contain objects of a specific type, although this type can be Object, which essentially means that the items can be of any type.

  6. Explain the difference between declaration and instantiation of an object.
    Ans: Declaration means to create a reference variable that will refer to the object like this
    Dim a As StreamReader
    Instantiation means create the actual object like this:
    a = New StreamReader("a.txt")
    These two steps can be combined as Dim a As New StreamReader("a.txt")
    or Dim a As ArrayList = New StreamReader("a.txt")

  7. What is the difference between a class and a module?
    Ans: A class is a template to create objects but objects cannot be created from a module.

  8. What is the index lower bound for items in an array?
    Ans: 0.

  9. Write a statement that prints the upper bound of the array a to the console. Ans:
    WriteLine(a.GetUpperBound(0))

  10. Write VB.NET code that creates an array containing the odd numbers from 1 to 1000. Ans:
    Dim a(449) As Integer
    For i = 0 To 449
        a(i) = 2 * i + 1
    Next

  11. Which Imports statement should be used to read from or write to a text file? Ans:
    Imports System.IO

  12. Explain the difference between an absolute and a relative path for a file.
    Ans: An absolute path starts from the root folder on a specific disk drive like this:
    picDisplay.Image = Image.FromFile("c:\a.jpg")
    A relative path is specified with respect to the folder that contains the .exe. file for the application. This is the bin folder for VS 2003 and bin\Debug for VB 2005:
    picDisplay.Image = Image.FromFile("a.jpg")

  13. Write the header for the Click event handler of the button btnSubmit for memory.
    Public Sub btnSubmit_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles btnSubmit.Click

    End Sub

 

Alternative Ways to Create an Array

  1. Dim a(3) As Integer

  2. Dim a() As Integer = {3, 5, 7, 9}

  3. Dim a() As Integer = New Integer(3) {}

  4. Dim a() As Integer = New Integer() {3, 5, 7, 9}

  5. Dim a() As Integer = New Integer(3) {3, 5, 7, 9}

  6. Dim a() As Integer
    a = New Integer(3) {}

  7. Dim a() As Integer
    a = New Integer() {3, 5, 7, 9}

  8. Dim a() As Integer
    a = New Integer(3) {3, 5, 7, 9}

 

String Objects

 

Practice Problems

  1. Write lines of code that print Yes if the string s begins with "a" and No otherwise. Ans:
    If s.Substring(0, 1) = "a" Then
        WriteLine("Yes")
    Else
        WriteLine("No")
    End If

  2. Write lines of code that print Yes if the string s ends with "a" and No otherwise. Ans:
    indexOfLast = s.Length - 1
    If s.Substring(indexOfLast, 1) = "a" Then
        WriteLine("Yes")
    Else
        WriteLine("No")
    End If

  3. Write a console application that inputs a string and outputs the string reversed. Ans:
    Dim t As Index = 0
    For i = 1 To a.Length - 1
        t = s.Substring(i, 1) & t
    Next
    WriteLine(t)

 

Subroutines vs. Functions

 

Three Types of Methods

  1. Event Handlers

  2. Builtin Methods

  3. User Defined Methods

 

User Defined Subroutines

 

User Defined Functions

 

Practice Problems

 

Reading from and Writing to Text Files

 

More PhoneBook Examples

 

The FlightInfo1 Project