Table + coffetable + array Learn programming Visual Basic (VB.net)



Lesson:

OOP More On Classes


Exercise:

Table + coffetable + array


Objetive:

Create a project named "Tables2", based on the "Tables" project.

In it, create a class "CoffeeTable" that inherits from "Table". Its method "ShowData", besides writing the width and height, must display "(Coffee table)."

Create an array that contains 5 tables and 5 coffee tables. The tables must have random sizes between 50 and 200 cm, and the coffee tables from 40 to 120 cm. Show all their data.


Code:

Imports System
Namespace Tables2
    Class CoffeeTable
        Inherits Table

        Public Sub New(ByVal width As Single, ByVal height As Single)
            Me.width = width
            Me.height = height
        End Sub

        Public Overrides Sub ShowData()
            Console.WriteLine("(Coffee table) Width: {0}, Heigth: {1}", width, height)
        End Sub
    End Class

    Class Table
        Protected width, height As Single

        Public Sub New()
        End Sub

        Public Sub New(ByVal width As Single, ByVal height As Single)
            Me.width = width
            Me.height = height
        End Sub

        Public Property Width As Single
            Set(ByVal value As Single)
                width = value
            End Set
            Get
                Return width
            End Get
        End Property

        Public Property Height As Single
            Set(ByVal value As Single)
                height = value
            End Set
            Get
                Return height
            End Get
        End Property

        Public Overridable Sub ShowData()
            Console.WriteLine("Width: {0}, Heigth: {1}", width, height)
        End Sub
    End Class

    Class TestTables
        Private Shared Sub Main()
            Dim debug As Boolean = False
            Dim myTables As Table() = New Table(9) {}
            Dim rnd As Random = New Random()

            For i As Integer = 1 To 10

                If (i Mod 2 = 0) AndAlso (i <> 1) Then
                    myTables(i - 1) = New Table(rnd.[Next](50, 201), rnd.[Next](50, 201))
                    myTables(i - 1).ShowData()
                Else
                    myTables(i - 1) = New CoffeeTable(rnd.[Next](40, 121), rnd.[Next](40, 121))
                    myTables(i - 1).ShowData()
                End If
            Next

            If debug Then Console.ReadLine()
        End Sub
    End Class
End Namespace



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