Function WriteRectangle Learn programming Visual Basic (VB.net)



Lesson:

Functions


Exercise:

Function WriteRectangle


Objetive:

Create a function WriteRectangle to display a (filled) rectangle on the screen, with the width and height indicated as parameters, using asterisks. Complete the test program with a Main function:

WriteRectangle(4,3);

should display
****
****
****

Create also a function WriteHollowRectangle to display only the border of the rectangle:
WriteHollowRectangle(3,4);

should display
***
* *
* *
***


Code:

Imports System
Public Class exercise129
    Private Shared Sub WriteRectangle(ByVal width As Integer, ByVal height As Integer)
        For i As Integer = 0 To width

            For j As Integer = 0 To height
                Console.Write("*")
            Next

            Console.WriteLine()
        Next
    End Sub

    Private Shared Sub WriteHollowRectangle(ByVal width As Integer, ByVal height As Integer)
        For i As Integer = 1 To height

            For j As Integer = 1 To width

                If (i = 1) OrElse (i = height) Then
                    Console.Write("*")
                Else

                    If (j = 1) OrElse (j = width) Then
                        Console.Write("*")
                    Else
                        Console.Write(" ")
                    End If
                End If
            Next

            Console.WriteLine()
        Next
    End Sub

    Private Shared Sub Main(ByVal args As String())
        WriteRectangle(4, 3)
        Console.WriteLine()
        WriteHollowRectangle(3, 4)
        Console.ReadLine()
    End Sub
End Class



Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.