CS13 Calculator Project #2

Using "Visual Basic", create a form with 2 textBox inputs, a listBox for output,
and the following buttons: For each button, create a handler that does each of the following steps.
If possible, "modularize" the handlers by using separate functions or subs
for input, processing, and output.
  1. Converts the two inputs to integer values,
  2. perform the indicated arithmetic calculation,
  3. then output the result value (following some text to say what it is).
Upload both VB files (form1.vb & form1.designer.vb) to your portfolio at http://www.suffolk.li/cs13/71cs13/students/{YOURNAME}/$/

Below are two examples of handlers for the ADD button.
(The first example is not modularized; the second example is)


    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                         Handles btnAdd.Click
        '''' Sum the two numbers.

        '''' DECLARATIONS:      
        Dim first As Double
        Dim second As Double
        Dim answer As Double

        '''' INPUT ''''
        first = Val(Me.txtFirst.Text)   ' Convert string to #
        second = Val(Me.txtSecond.Text)

        '''' PROCESSING ''''
        answer = first + second         ' Add numbers

        '''' OUTPUT ''''
        Dim s As String
        Dim t As String
        s = "The sum is "
        t = Format(answer)              ' Convert # to string
        Me.lstAnswers.Items.Add(s + t)

    End Sub


The second code example is fully "modularized" to simplify its "re-use" for other buttons.


    Private Sub btnAdd_Click( ..., ... ) Handles btnAdd.Click
        '''' Input two numbers, process them, produce labelled output.
        Dim first as Integer = getFirst()        '''' INPUT ''''
        Dim second as Integer = getSecond()
        Dim s as String

        '''' PROCESSING ''''        
        answer = calcAdd( first, second )
        s=  "The sum is "
        '******  FOR RE-USE, CHANGE THE ABOVE TWO LINES
        '******  (AND WRITE A NEW calc FUNCTION).

        call saywhat ( s, answer )               '''' OUTPUT ''''
    End Sub
    Function calcaAdd( a as Integer, b as Integer) as Integer
        '''' Add the two numbers.
        Dim result as Integer
        result=  a + b
        return result
    End Function


    '======= INPUT METHODS ========'
    Function getFirst() as Integer
        '''' Get the first input; convert to integer value. ''''
        return Val(Me.txtFirst.Text)   ' Convert string to #
    End Function
    Function getSecond() as Integer
        '''' Get the 2nd input; convert to integer value. ''''
        return Val(Me.txtSecond.Text)   ' Convert string to #
    End Function

    '======= OUTPUT METHODS ========'
    Sub getFirst( msg as string, v as integer )
        '''' Display a message followed by a value.
        Me.lstAnswers.Items.Add( msg + Format(v) )
    End Sub