Class Codes


Our very first program we wrote in class was a variation of the highly popular "Hello World" application which is a first among many programmers. In it we are displaying "hello bcom level 200" in the console. We make use of "Console.Read()" to prevent the console application from exiting immediately and allows it to wait for the user to press the enter key before exiting.

Module Module1
Sub Main()
        Console.WriteLine("hello bcom level 200")
        Console.Read()
End Sub
End Module


In this example we declare 3 variables so that we can add two numbers together and store their answer in a third variable. Basically all we did was to declare the variables > assign values to the variables > assign the value of the addition of the two variables to the answer and then display the result.l

Module Module1
Sub Main()

Dim number1 As Integer
        'declaring the variable number1, the datatype is integer which is a whole number without decimals. A declaration is making a memory space available for use, it also helps us to set a datatype for the variable we have created
        Dim number2 As Integer
        Dim answer As Integer

        number1 = 2
        'assignment, we are giving a value to our variable, this line can be interpreted as "the variable number1 is assigned the value 2"
        number2 = 5

        answer = number1 + number2
        'assigning  the combined (added) values of number1 and number2 to the variable answer

        Console.WriteLine(answer)
        'we are displaying the value of the variable answer
        Console.Read()
        'we are preventing the console from exiting immediately after running the codes, until we press enter

End Sub
End Module




In this example we have taken the simple addition of numbers to a slightly higher level. Here we are requesting the user to make an input of any whole number and our application will be able to take those numbers and add them together to display the results. Note that if you want the program to be able to handle decimals we must change the datatype from integer to double.

Module Module1
Sub Main()

        Dim number1 As Double
        Dim number2 As Double
        Dim answer As Double

        Console.WriteLine("please enter any whole number")
        'informing the user to give an input
        number1 = Console.ReadLine()
        'assigning the user input to the variable number1

        Console.WriteLine("please enter another whole number")
        number2 = Console.ReadLine()

        answer = number1 + number2
        Console.WriteLine(answer)
        Console.ReadLine()

End Sub
End Module



i this example we had just taken a look at methods, we know that there are two types of methods; a subroutine and a function. the major difference between the two is that a subroutine cannot return a values whereas a function can.

If we look care fully we have multiple subroutines defined by the "Sub" in-front of the method name. since the Sub Main() subroutine always starts first, in order to display the other subroutines we need to "call" them. We call methods (Subroutines and functions) by stating their name with () at the end. For example below we called on the displayhello subroutine by calling the method in the main view ... displayhello(). that is basically what needs to be done to ensure continuity.

These codes are similar in content to assignment 2. It would be advisable to take your time and read it.

Module Module1


 Sub Main()


        displayhello()
        'at this point i am calling the subroutine displayhello
        addition()
        subtraction()
        displaygoodbye()
        Console.Read()


    End Sub

    Sub displayhello()
        'this is  a subroutine, a subroutine is a type of method
        'methods allow us to segment our codes and gives us a more
        'organised program
        Console.WriteLine("hello")
    End Sub

    Sub displaygoodbye()
        Console.WriteLine("goodbye")
    End Sub

    Sub addition()
        'the purpose of this subroutine is to add two numbers and display the result
        Console.WriteLine("welcome to the addition subroutine")
        Dim a As Integer
        Dim b As Integer
        Dim c As Integer
        Console.WriteLine("enter number 1")
        a = Console.ReadLine()
        Console.WriteLine("enter number 2")
        b = Console.ReadLine()
        c = a + b
        Console.WriteLine(c)
    End Sub

    Sub subtraction()
        'the purpose of this subroutine is to subtract one number from another and display the result
        Console.WriteLine("welcome to the subtration subroutine")
        Dim a As Integer
        Dim b As Integer
        Dim c As Integer
        Console.WriteLine("enter number 1")
        a = Console.ReadLine()
        Console.WriteLine("enter number 2")
        b = Console.ReadLine()
        c = a - b
        Console.WriteLine(c)
    End Sub


End Module

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Module Module1

    Sub Main()
        Dim x As Double = 0
        Dim y As Double = 0
        Dim z As Double = 0
        x = 12
        y = 4
        z = x / y
        Console.WriteLine(z)
        Console.Read()
    End Sub

End Module


Module Module1

    Sub Main()
        Dim x As Double = 0
        Dim y As Double = 0
        Dim z As Double = 0
        Dim m As Double = 0
        Console.WriteLine("please input a numerator")
        x = Console.ReadLine()
        Console.WriteLine("please input a denomenator")
        y = Console.ReadLine()
        Console.WriteLine("please input a number")
        m = Console.ReadLine()
        z = x / y + m

        Console.WriteLine("the answer is " & z)
        Console.Read()
    End Sub

End Module

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 

Module Module1

    Sub Main()
        displaygoodbye()

        itstime()

        Dim a As Double = 0
        Dim b As Double = 0
        Dim c As Double = 0
        Dim d As Double = 0
        Dim e As Double = 0
        Dim f As Double = 0

        Console.WriteLine("enter a number")
        a = Console.ReadLine()
        Console.WriteLine("enter another number")
        b = Console.ReadLine()

        c = addit(a, b)
        d = subtractit(a, b)
        e = multiplyit(a, b)
        f = divideit(a, b)

        Console.WriteLine("addition: " & c)
        Console.WriteLine("subtraction: " & d)
        Console.WriteLine("multiplication: " & e)
        Console.WriteLine("division: " & f)

        Console.Read()

    End Sub

    Sub itstime()
        Console.WriteLine("it is time to close")
    End Sub

    Sub displaygoodbye()
        Console.WriteLine("goodbye")
    End Sub

    Function addit(ByVal x As Double, ByVal y As Double)
        'this function takes two variables and adds them together and returns the value of the addition
        Dim z As Double = 0
        z = x + y
        Return z
    End Function

    Function subtractit(ByVal x As Double, ByVal y As Double)
        Dim z As Double = 0
        z = x - y
        Return z
    End Function

    Function multiplyit(ByVal x As Double, ByVal y As Double)
        Dim z As Double = 0
        z = x * y
        Return z
    End Function

    Function divideit(ByVal x As Double, ByVal y As Double)
        Dim z As Double = 0
        z = x / y
        Return z
    End Function

End Module


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Module Module1
    'what we intend to do today
    'methods: subroutines and functions
    'control statements
    'use of different types of variables
    'arrays

    Sub Main()
        todayscodes()
        multiplication()
        Console.Read()
    End Sub

    Sub valsdaygreeting()
        Console.WriteLine("happy vals day")
    End Sub

    Sub todayscodes()
        Dim a, b, c, q As Double
        'these kind of variable is called a scalar variable and hold only one object
        Dim lovequotes(3) As String
        'this is an array that can hold multiple objects

        lovequotes(0) = "you are my lapaz toyota"
        lovequotes(1) = "you are the mosquito in my net"
        lovequotes(2) = "you are the queen of my heart"
        lovequotes(3) = "i love you, you are mine forever"

        valsdaygreeting()
        Console.WriteLine(lovequotes(0))
    End Sub

    Sub multiplication()
        Dim uservalues(2) As Integer
        Dim answer As Integer

        Console.WriteLine("number 1")
        uservalues(0) = Console.ReadLine()
        Console.WriteLine("number 2")
        uservalues(1) = Console.ReadLine()
        Console.WriteLine("number 3")
        uservalues(2) = Console.ReadLine()

        answer = uservalues(0) * uservalues(1) * uservalues(2)
        Console.WriteLine(answer)
    End Sub

End Module


------------------------------------------------------------------------------------------------------------------------------------------------------------------

Module Module1
    'methods:functions
    ' the difference between subroutines and functions is that a subroutinedoes not return a value but a function does
    Sub main()
        Dim userinput(1) As Integer
        'we have declared an array
        Console.WriteLine("number 1")
        userinput(0) = Console.ReadLine()
        Console.WriteLine("number 2")
        userinput(1) = Console.ReadLine()

        Console.WriteLine(addition(userinput(0), userinput(1)))
        Console.WriteLine(subtraction(userinput(0), userinput(1)))
        whichisbigger(userinput(0), userinput(1))
        Console.Read()
    End Sub

    Function addition(ByVal i As Integer, ByVal j As Integer)
        Dim a As Integer
        Console.WriteLine("addition")
        a = i + j
        Return a
    End Function

    Function subtraction(ByVal i As Integer, ByVal j As Integer)
        Dim a As Integer
        Console.WriteLine("subtraction")
        a = i - j
        Return a
    End Function

    Sub whichisbigger(ByVal i As Integer, ByVal j As Integer)
        'the if statement brings in conditionality to our program we
        'can now make decisions and produce an output based on those decisions
        If i > j Then
            Console.WriteLine("number 1 is greater")
        ElseIf j > i Then
            Console.WriteLine("number 2 is greater")
        Else
            Console.WriteLine("they are equal")
        End If
    End Sub

End Module


------------------------------------------------------------------------------------------------------------------------------------------------------------------

Module Module1
    'if katherines head is big true, if her head is small false

    Sub main()
        Dim holder(1) As String
        holder(0) = "big"
        holder(1) = "small"
        Dim userinput As String

        Console.WriteLine("enter 'big' or 'small'")
        userinput = Console.ReadLine()

        If userinput = holder(0) Then
            Console.WriteLine("true: you are correct, the head is big")
        ElseIf userinput = holder(1) Then
            Console.WriteLine("false: the head is actually big")
        Else
            Console.WriteLine("you are supposed to enter big or small, cant you follow instructions?")
        End If

        Console.Read()
    End Sub

End Module


------------------------------------------------------------------------------------------------------------------------------------------------------------------



Public Class Form1

    Private Sub btn_mu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_mu.Click
        multiplyit()
    End Sub

    Sub multiplyit()
        Dim ui(4) As Double
        ui(0) = txt_n1.Text
        ui(1) = txt_n2.Text
        ui(2) = txt_n3.Text
        ui(3) = txt_n4.Text
        ui(4) = ui(0) * ui(1) * ui(2) * ui(3)
        lbl_mu.Text = ui(4)
    End Sub

    Private Sub btn_addiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_addiv.Click
        addanddivideit()
    End Sub

    Sub addanddivideit()
        Dim ui(4) As Double
        ui(0) = txt_n1.Text
        ui(1) = txt_n2.Text
        ui(2) = txt_n3.Text
        ui(3) = txt_n4.Text
        ui(4) = (ui(0) + ui(1)) / (ui(2) + ui(3))
        lbl_addiv.Text = ui(4)
    End Sub

    Private Sub btn_great_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_great.Click
        Dim ui(4) As Double
        ui(0) = txt_n1.Text
        ui(1) = txt_n2.Text
        ui(2) = txt_n3.Text
        ui(3) = txt_n4.Text

        Dim x As Double = ui(0)
        If x < ui(1) Then
            x = ui(1)
        End If
        If x < ui(2) Then
            x = ui(2)
        End If
        If x < ui(3) Then
            x = ui(3)
        End If
        lbl_great.Text = x

    End Sub

    Private Sub btn_small_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_small.Click
        Dim ui(4) As Double
        ui(0) = txt_n1.Text
        ui(1) = txt_n2.Text
        ui(2) = txt_n3.Text
        ui(3) = txt_n4.Text

        Dim x As Double = ui(0)
        If x > ui(1) Then
            x = ui(1)
        End If
        If x > ui(2) Then
            x = ui(2)
        End If
        If x > ui(3) Then
            x = ui(3)
        End If
        lbl_small.Text = x
    End Sub
End Class

------------------------------------------------------------------------------------------------------------------------------------------------------------------

CODES FOR THE DENOMINATION QUESTION


Module Module1

    Sub Main()
beginning:
        Dim dblDenomination() As Double = {50, 20, 10, 5, 2, 1, 0.5, 0.2, 0.1, 0.05, 0.01}
        Dim dblAmountPaid As Double
        Dim dblItemCost As Double
        Dim dblBalance As Double

dataentry:
        Console.WriteLine("enter the cost of the item")
        dblItemCost = Console.ReadLine()
        Console.WriteLine("enter the amount paid for the item")
        dblAmountPaid = Console.ReadLine()

        If dblAmountPaid < dblItemCost Then
            Console.WriteLine("you dont have enough money")
            GoTo dataentry
        End If

        dblBalance = dblAmountPaid - dblItemCost

        'For intDenominationCounter As Integer = 0 To 10
        '    Console.WriteLine(intDenomination(intDenominationCounter))
        'Next intDenominationCounter

        For intDenominationCounter As Integer = 0 To 10

            Do While dblBalance >= dblDenomination(intDenominationCounter)
                dblBalance = dblBalance - dblDenomination(intDenominationCounter)
                Console.WriteLine(dblDenomination(intDenominationCounter & ", "))
            Loop

        Next intDenominationCounter

        GoTo beginning
        Console.Read()
    End Sub

End Module
------------------------------------------------------------------------------------------------------------------------------------------------------------------


Public Class Form1
    Private aa(1, 1), ab(1, 1), ac(1, 1) As Double

    Private Sub assignvalues()
        aa(0, 0) = t1.Text
        aa(0, 1) = t2.Text
        aa(1, 0) = t3.Text
        aa(1, 1) = t4.Text
        ab(0, 0) = t5.Text
        ab(0, 1) = t6.Text
        ab(1, 0) = t7.Text
        ab(1, 1) = t8.Text
    End Sub
   
    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        assignvalues()
        add()
    End Sub

    Private Sub btnsub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsub.Click
        assignvalues()
        subtract()
    End Sub

    Private Sub btnmul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmul.Click
        assignvalues()
        mul()
    End Sub

    Sub add()
        t9.Text = aa(0, 0) + ab(0, 0)
        t10.Text = aa(0, 1) + ab(0, 1)
        t11.Text = aa(1, 0) + ab(1, 0)
        t12.Text = aa(1, 1) + ab(1, 1)
    End Sub

    Sub subtract()
        t9.Text = aa(0, 0) - ab(0, 0)
        t10.Text = aa(0, 1) - ab(0, 1)
        t11.Text = aa(1, 0) - ab(1, 0)
        t12.Text = aa(1, 1) - ab(1, 1)
    End Sub

    Sub mul()
        t9.Text = (aa(0, 0) * ab(0, 0)) + (aa(0, 1) * ab(1, 0))
        t10.Text = (aa(0, 0) * ab(0, 1)) + (aa(0, 1) * ab(1, 1))
        t11.Text = (aa(1, 0) * ab(0, 0)) + (aa(1, 1) * ab(1, 0))
        t12.Text = (aa(1, 0) * ab(0, 1)) + (aa(1, 1) * ab(1, 1))
    End Sub

  
    Private Sub btn_altmul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_altmul.Click
        assignvalues()
        alternatemultiply(aa(0, 0), aa(0, 1), aa(1, 0), aa(1, 1), ab(0, 0), ab(0, 1), ab(1, 0), ab(1, 1))
    End Sub

    Sub alternatemultiply(ByVal a, ByVal b, ByVal c, ByVal d, ByVal w, ByVal x, ByVal y, ByVal z)
        t9.Text = (a * w) + (b * y)
        t10.Text = (a * x) + (b * z)
        t11.Text = (c * w) + (d * y)
        t12.Text = (c * x) + (d * z)
    End Sub

End Class

------------------------------------------------------------------------------------------------------------------------------------------------------------------ 

1 comment:

  1. Sir any notes on common Language Run time?

    ReplyDelete