Wednesday, September 29, 2010

notes from lecture

Hint for the Loop assignment:
look into
=
<>
keywords: Not, While, Until
Not y = 5
y <> 5

Do
Loop While I = 6

same as
Do
Loop Until Not I = 6

Do
Loop Until I <> 6

     _____
x = v  3

we started the section called Numbers
Constant as Opposed a Variable

constant value never changes

Const PI As Double = 3.14159

avoid Magic numbers
instead of * 4
we would write * ArraySize

scope:
Local variables
Global (class-level) variables

scope: where a variable has a definition
related to lifetime: how long a variable exists

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim x As Integer
        x = 6
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' this is a syntax error
        'Button1.Text = x
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    End Sub
End Class

Public Class Form1
    ' this is a class-level
    ' or non-local, or global
    ' variable
    Dim x As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        x = 0
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' this is NOT a syntax error
        x = x + 1
        Button1.Text = x
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    End Sub
End Class


the With construct:
  With lstResults.Items
    .Clear()
    .Add(Math.Sqrt(5 * b + 1))
    .Add(Int(a ^ b + 0.8))
    .Add(Math.Round(a / b, 3))
  End With


Alternatively:

lstResults.Items.Clear()
lstResults.Items.Add(...)
lstResults.Items.Add(...)
lstResults.Items.Add(...)


Office Hour:
Wed 3-4 Adjunct Room, across from Cs Dept Office
joshwaxman@gmail

Trace through a program

Assignment 6
"Numbers"
questions 30 - 46
60-72
even numbers only
NOT even assigned yet

Monday, September 27, 2010

Tutors and tutor schedule

is available here.

code from class

Public Class Form1
    Private Sub btnTokenize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTokenize.Click
        Dim Sentence As String
        Sentence = TextBox1.Text & " "

        'Dim startPos, spacePos, wordLen As Integer
        'Dim word As String
        'startPos = 0
        'spacePos = Sentence.IndexOf(" ", startPos)
        'wordLen = spacePos - startPos
        'word = Sentence.Substring(startPos, wordLen)
        'lstOutput.Items.Add(word)

        'startPos = spacePos + 1

        'Dim midterm1, final1 As Integer
        'Dim midterm2, final2 As Integer
        'Dim midterm3, final3 As Integer
        'Dim midterm4, final4 As Integer

        'Dim midterm(3) As Integer
        'Dim final(3) As Integer
        'Dim mid_avg As Integer

        'Dim total As Integer
        'For Each x In midterm
        '    total = total + x
        'Next
        'mid_avg = total / (UBound(midterm) + 1)

        Dim words() As String = Sentence.Split(" ")
        For Each x In words
            lstOutput.Items.Add(x)
        Next
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        btnTokenize.Focus()
    End Sub
End Class

Wednesday, September 22, 2010

lecture 6 notes

* For loop
* How to use debugger
* assignments
* Controls and Events

Do Loop
For Loop

syntactic sugar

Dim I as Integer = 1
Do
listBox1.Items.Add(word1)
I = I + 1
Loop While I < 16

debugger
* setting a breakpoint
breakpoint: a place in your program where execution stops

Step Over; alternatively, F10

hover over variable to see its current value.
also, Watch window
you can change variables in watch window, see value and data type of variables and expressions

For I = 1 To 15
listBox1.Items.Add(I)
Next

Assignment 5

No date announced yet for it yet, but just putting it out there so that you can start on this if you want.

Read through the chapter in the book about Controls and Events.

In the section called "Visual Basic Events" do the even exercises, # 38, 40, 42, 44

The due date for assignment 2 and 3

is moved until next Monday, rather than midnight this Wednesday night.

I'll try to give answers, either in lecture or in a post on the blog, after each due date.

Monday, September 20, 2010

lecture 5 notes

some hints for assignments 2 and 3

assignment 2:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim degreeType As String
        degreeType = TextBox1.Text
        If degreeType = "Celsius" Then
            ' convert to other type

            ' output statement
        Else
            ' convert to other type

            ' output statement
        End If
    End Sub
End Class

assignment 3:
see the spreadsheet on Blackboard for the strategy

Loops:
Be careful about infinite loops

Public Class Form1

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

        Do
            answer = InputBox("Pete and Repeat were on a boat. Pete fell off. Who was left?", "Important question", "Repeat")
        Loop While answer = "Repeat"
    End Sub
End Class

Assignment 4:
HW: Modify the above to count how many times the person chose "Repeat". Output this somewhere at the end.

HW: Your program requires a password to get into it. That password is "swordfish". At the start of your program, prompt for the password and keep asking them until they finally say "swordfish".

Due date: Next Wednesday

Sunday, September 19, 2010

Don't forget!

Assignment #1 (with three parts) is due Monday night! Right now, only about 1/10 of the class has handed it in. In case it is because you don't know how to submit via BlackBoard, I'll demonstrate this in class on Monday.

Assignment #2 and #3 are due on Wednesday night. I'll be discussing a bit of strategy for assignment #3 (the rain in Spain) in class Monday, but if you can have something to discuss tomorrow with your lab instructor, you can get a jump on it.

Wednesday, September 15, 2010

notes for lecture #4

If conditionals
String data type
Methods

Calculate student's grade
* Midterm grade
* Final grade
* Bribe amount

Text property of a textbox has data type of String
Automatically, implicitly, Convert from one data type to another
CDbl
Option Strict On
Val

Option Strict On
Public Class Form1

    Private Sub btnCalcGrade_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalcGrade.Click
        ' declare variables
        Dim midtermGrade, finalGrade, bribe As Double
        Dim semesterAvg As Double

        ' get input
        midtermGrade = Val(txtMidterm.Text)
        finalGrade = Val(txtFinal.Text)
        bribe = Val(txtBribe.Text)

        ' processing
        If bribe >= 50 Then
            semesterAvg = 100
        Else
            semesterAvg = (midtermGrade + finalGrade) / 2
        End If

        ' print out output
        lblAverage.Text = CStr(semesterAvg)
    End Sub
End Class

HW: convert from f to c, from c to f, based on an if and the value in a textbox

HW: Take the text "The rain in Spain falls mainly on the ground" and separate it into separate words. Print each word on a separate line in a ListBox.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim s As String
        s = "hello"
        s = s & " " & "world"
        s = s.ToUpper()
        s = s.Replace("WORLD", "EARTH")
        Dim i As Integer
        i = s.IndexOf("EARTH")
        MessageBox.Show(i.ToString())
        s = s.Substring(6, 5)


        Button1.Text = s
    End Sub
End Class

Monday, September 13, 2010

notes for lecture #3

1) Draw your GUI
2) Write your code

A bug
A syntax error causes build errors
A logic error
Runtime errors


Hello world program
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = "Hello, world!"
    End Sub
End Class

Plato perfect age
Option Explicit On
Public Class Form1

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

        ' Get input
        manAge = TextBox1.Text

        ' Processing
        wifeAge = manAge / 2 + 7

        ' Display output
        Label3.Text = wifeAge
    End Sub
End Class

I'm naming controls (though not other variables) using "Hungarian"

camelCase

Apply a 10 point curve
Option Explicit On
Public Class Form1

    Private Sub btnCalculateCurve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculateCurve.Click
        Dim grade As Integer
        Dim curvedGrade As Integer

        ' Get input
        grade = txtGrade.Text

        ' Processing
        curvedGrade = grade + 10

        ' Display output
        lblCurvedGrade.Text = curvedGrade
    End Sub
End Class

Tuesday, September 7, 2010

Looking for a note-taker for this class

The Office of Special Services is requesting a volunteer to drop off a copy of their notes for CS80 once a week.

If you are interested, come to me, and I will give you a letter to bring to Kiely 171, so you can sign in with them and receive the note-taker guidelines. They will be pleased to give letters of recommendation for your assistance with this matter.

Some notes for lecture #2

syntax: the rules of the language. what is a verb, what is a noun, the order in which various words need to appear.

variable:
a "box" in memory with a name and a value. that value can change over time.

declaration:
Dim x as Integer

data types:
Integer, Long
String
Single, Double
Boolean

Initialization:
Dim x as Integer = 3

GUI: Graphical user interface
draw GUI, then VB code as the "codebehind"
asignment statement. use = operators. stuffs a value into a variable. does NOT establish a relationship, as if it were an equation.

Thursday, September 2, 2010

Assignment 1

For each of these, make a VB program, complete with GUI.
1) Given speed and time, calculate distance and display it.

2) Given a temperature in Celsius, convert to Fahrenheit and display it.

3) Given a title, first name and last name, calculate and display it.

I haven't announced a due date for this homework yet.
Submit it via blackboard, once you have completed it.