Thursday, October 28, 2010

Some useful feedback

The question:
Hello Professor ive been having trouble with # 50 on the 3.4 strings hw assignment #7
i got the first half but dont know what to do after....

Public Class Form1

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        TextBox2.Text = Int(TextBox1.Text)
        TextBox3.text = decimal?????(textbox1.Text)

    End Sub
End Class

My answer:
there are two approaches you could use for this:

strategy #1, which i think they wanted people to use:
1) use a string function, IndexOf, to find the POSITION of the period character, "."
2) use the string function, SubString, to take a slice of the string up to the period
3) use the string function, SubString, to take a slice of the string from after the period until the end.

this fits in quite well with the contents of the section, which is strings and string functions.

you could also employ strategy #2:
1) Put the entire number into a Double variable, x.
2) What you did, calling Int or CInt to get the portion before the decimal. But get it from the variable x, rather than directly from the Text property of your textbox. Assign this value to y.
3) x minus y will be just the stuff after the decimal point. assign it to a variable z.
4) then, output all these results.

of course, there are better variable names than x, y, z.

Wednesday, October 27, 2010

lecture notes

An intro to subroutines

Sub DisplayName()
Dim FirstName As String = "Josh"
Dim LastName As String = "Waxman"
Dim FullName as String
FullName = FirstName & " " & LastName
MsgBox FullName
End Sub

Private Sub Button1_Click(...) Handles ...
DisplayName
End Sub

Private Sub Button2_Click(...) Handles ...
DisplayName
End Sub


----

Private Sub Button1_Click(...) Handles btnAdd.Click
  'Display the sum of two numbers
  DisplayOpeningMessage
  DisplaySum(2, 3)
End Sub

Sub DisplayOpeningMessage()
  lstResult.Items.Clear()
  lstResult.Items.Add("This program displays a sentence")
  lstResult.Items.Add("identifying two numbers and their sum.")
  lstResult.Items.Add("")
End Sub

Sub DisplaySum(num1 As Double, num2 As Double)
  lstResult.Items.Add("The sum of " & num1 & " and " _
                      & num2 & " is " & num1 + num2 & ".")
End Sub

formal parameters
as opposed to actual parameters
we COPY the actual parameters into the formal parameters


' Get Input

' Processing

' Output Results

top-down design
stepwise refinement
stack
FILO (first in, last out)
push operation (put onto top of stack)
pop operation (taking off top of stack)
call stack
stack frames
use this to understand firstpart, secondpart example

Exams, with answers

But you may wish to use these to study:
https://docs.google.com/document/pub?id=1ukHo2jysIGz1wXkkpsk80-d9n9vQIpzNb4lMqEUscJA

http://docs.google.com/View?id=ajbqhgmq9qdz_79dcp4v5v5

Monday, October 25, 2010

lecture notes

Format of the test
_____________________

10 short answers (30 points)
6 m/choice questions (30 points)
4 Debugging (20 points)
4 Programming questions (20 points)
1 Tracing question (10 points)

Dim pos as Long
pos = TextBox1.Text

if pos = 1 Then
  txtOutcome.Text = "Win"
elseif pos = 2 Then
  txtOutcome.Text = "Place"
elseif pos = 3 Then
  txtOutcome.Text = "Show"
elseif pos = 4 or pos = 5 Then
  txtOutcome.Text = "You almost placed in the money."
else
  txtOutcome.Text = "Out of the money."
endif

syntactic sugar
 Select Case


if position >= 1 Or position <= 3 Then
instead, in a Select Case, we could say
Case 1 To 3

Assignment 9
HW: "Select Case"
10-36, even

Public Class Form1
    Sub Moo()
        MessageBox.Show("Mooooooooo!")
        MessageBox.Show("Baaaaaaaaa!")
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Moo()
        Moo()

    End Sub
End Class

Wednesday, October 20, 2010

lecture notes

Midterm: November 3rd
Chapter 1-3; Chapter 5 (meaning decision making)

ElseIf will let us accomplish three-way (or more) branching

Private Sub btnShow_Click(...) Handles btnShow.Click
  Dim costs, revenue, profit, loss As Double
  costs = CDbl (txtCosts.Text)
  revenue = CDbl (txtRev.Text)
  If costs = revenue Then
    txtResult.Text = "Break even"
  ElseIf costs < revenue Then
    profit = revenue - costs
    txtResult.Text = "Profit is " & FormatCurrency(profit)
  Else
    loss = costs - revenue
    txtResult.Text = "Loss is " & FormatCurrency(loss)
  End If
End Sub

10 gallon hat
we used And for intersection, Or for union

Chr(65) = "A"
Asc("A") = 65

Dim code As Integer
code = Asc(TextBox1.text)
If code < Asc("A") Or code > Asc("Z") Then
    txtOutput.Text = "not a capital letter"
Else
    txtOutput.Text = "a capital letter"
EndIf


Magic numbers
Asc("A") instead of 65
self-documenting

HW:
If Blocks
28-44, even


Assignment 6 is due this coming Monday

Monday, October 18, 2010

lecture notes

Decision making (ch 5)
we skip the chapter on "General Procedures" for now

Relational operators
>
<
>=
<=
=
<>

yields a Boolean type value (true or false)
Dim x as Boolean
x = 5 > 6
Button1.Text = x


x = 5 = 6
Dim y as Integer
x = y = 6
is y equal to 6? if it is, assign True to x
else assign False to x

context dependent

Comparing strings, lexicographical order
Compares it using ASCII value

gives a number to each symbol

Option Compare Text
Option Compare Binary

Dim x as String
x = "A"
' Convert to a number
' add 32 to it
' Convert back to a string
' A = 65; 65 + 32 = 97; 97 = a
' B = 66; 66 + 32 = 98; 98 = b

remember your truth tables from High School

de Morgan's laws

Do

Loop While x = 5 And (y = 7 Or z <> 2)

rewrite it using Until

Do

Loop Until Not(x = 5 And (y = 7 Or z <> 2))

Loop Until x <> 5 Or Not (y = 7 Or z <> 2)

Loop Until x <> 5 Or y <> 7 And z = 2

n = 4; answ = "Y"

(( 2 < n) And (n = 5 + 1)) Or (answ = "No")
(( True) And (False)) Or (False)
(False) Or (False)
False


Short Circuiting


Dim x as String
' maybe x = "bob"
If x.Length = 3 Then

' crashes

' but short circuiting cab be our friend
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As String
        If x IsNot Nothing And x.Length = 3 Then
            Button1.Text = x
        End If
    End Sub

This does NOT provide short circuiting
AndAlso OrElse

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As String
        If x IsNot Nothing AndAlso x.Length = 3 Then
            Button1.Text = x
        End If
    End Sub


If condition Then
  action1
  action2
  action3
Else
  action4
  action5
End If

If block

example 1 is find the Max



Private Sub btnFindMin_Click(...) Handles btnFindLarger.Click
  Dim num1, num2, min As Double
  num1 = txtFirstNum.Text
  num2 = txtSecondNum.Text
  If num1 < num2 Then
    min = num1
  Else
    min = num2
  EndIf
  txtResult.Text = "The smaller number is " & min
End Sub


example 2 is Nesting of ifs


next time, show how to remove the nesting in this example


If

Select Case

Wednesday, October 13, 2010

notes from lecture

String.Format
zone formatting
String.Format("{0, 10}{1, 3}{0,5}", "hi", 6)
TextBox1.Text = String.Format("{0, -10}{1, 3:N3}{0,5}", "hi", 0.6)

all these format function take in parameters of various types, but they ALL generate Strings

josh.txt
this will sit in a specific folder

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = String.Format("{0, -10}{1, 3:N3}{0,5}", "hi", 0.6)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim sr As IO.StreamReader
        sr = IO.File.OpenText("C:\vbfiles\josh.txt")
        Dim sentence As String
        sentence = sr.ReadLine()
        Dim myNumber As Double
        myNumber = CDbl(sr.ReadLine())
        TextBox1.Text = myNumber & ";" & sentence
        sr.Close()
    End Sub
End Class

how it will work with Loops
        Do While sr.Peek() <> -1

        Loop


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim t As String
        t = InputBox("Please enter a name", "Give a name", "Josh")
        TextBox1.Text = t
    End Sub

Wednesday, October 6, 2010

notes

Immediate window
X = 6
? X
6 {Integer}
    Integer: 6
Y = ""
? Y
"" {String}
    String: ""
? Y.Length
0 {Integer}
    Integer: 0
Y = "hello"
? Y
"hello" {String}
    String: "hello"
Y.ToUpper()
? Y
"hello" {String}
    String: "hello"
Z = Y.ToUpper()
? Z
"HELLO" {String}
    String: "HELLO"
? Y.ToUpper()
"HELLO" {String}
    String: "HELLO"
Y = "WORLD"
? Y
"WORLD" {String}
    String: "WORLD"
? Y.ToLower()
"world" {String}
    String: "world"
Y = "               Hello                         World"
The expression could not be evaluated because a build error occurred.Build Failed. For a list of build errors see the Error List. For detailed information about a build error select an item and press F1.
Y = "               Hello                         World"
? Y
"               Hello                         World" {String}
    String: "               Hello                         World"
Y = Y.Trim()
? Y
"Hello                         World" {String}
    String: "Hello                         World"


HW: String section
questions 24 - 36
questions 42 - 50
evens


book assumes option strict is on

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim result As String
        Dim t As Double = 6.75471
        result = FormatCurrency(t)
        Debug.Print(result)
        ' $6.75
    End Sub

result = FormatNumber(t, 2)

if you want to read up on Style for Format, read this article:
http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(MICROSOFT.VISUALBASIC.STRINGS.FORMAT);k(TargetFrameworkMoniker-%22.NETFRAMEWORK,VERSION%3dV4.0%22);k(DevLang-VB)&rd=true

String.Format is different from just regular Format!

Formatting Output with Zones
WWWWW
iiiii

Dim fmtStr As String = "{0, 15}{1, 10}{2, 8}"

Monday, October 4, 2010

notes

assignment 4:
due next monday, Oct 11

assignment 5:
due wednesday, Oct 13

String data type
collection of character

4 is an integer literal
"today" is a string literal

today in the example is a string variable

Primitive types vs Reference types

A Double, Integer, Boolean are primitive types

A String is a reference type, starts with Nothing (equivalent to NULL). when assign a string value to it, it points to that value on the heap.

If it is nothing and you try to use it, you will get a NullReferenceException

"" is the empty string. it is not Nothing

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim today As String
        today = ""

        With lstOutput.Items
            .Clear()
            .Add(today.Length)
            .Add("today")
            .Add(today)
        End With

    End Sub

        If today IsNot Nothing Then

        End If

What is an exception? Why should I use it?

instead of:
step 1
check for errors and fix
step 2
check for errors and fix
step 3
check for errors and fix
step 4
check for errors and fix

        Try
            step 1
            step 2
            step 3
            step 4
        Catch ex As Exception
            attempt to fix the problem
        End Try