Answer to Question #284650 in Visual Basic for xxx

Question #284650

Write a VB class, named ‘Shape’, with the following properties and methods:

Properties (with get and set)

-Width (integer) : read-write

-Height (integer) : read-write

-Type (enumerated types: ShapeType: rectangle, triangle, square) : read-only

Methods

-Area() : return the area of the shape

The Shape class should have a constructor to construct a new Shape object using user-supplied values.


1
Expert's answer
2022-01-04T01:09:47-0500
Imports System.IO


Enum ShapeType
    rectangle
    triangle
    square
End Enum


Class Shape
    Private _width As Integer
    Private _height As Integer
    Private _type As ShapeType




    Public Property Width() As Integer
        Get
            Return _width
        End Get
        Set(ByVal value As Integer)
            _width = value
        End Set
    End Property


    Public Property Height() As Integer
        Get
            Return _height
        End Get
        Set(ByVal value As Integer)
            _height = value
        End Set
    End Property
    Public ReadOnly Property Type() As ShapeType
        Get
            Return _type
        End Get
    End Property


    Sub New()


    End Sub
    Sub New(ByVal width As Integer, ByVal height As Integer, ByVal type As ShapeType)
        Me._width = width
        Me._height = height
        Me._type = type
    End Sub


    Function Area()
        If Type = ShapeType.rectangle Or Type = ShapeType.square Then
            Return Me._width * Me._height
        End If
        Return Me._width * Me._height * 0.5
    End Function
End Class


Module Module1
    Sub Main()
        Dim rectangle As New Shape(5, 4, ShapeType.rectangle)
        Dim square As New Shape(5, 5, ShapeType.square)
        Dim triangle As New Shape(5, 5, ShapeType.triangle)


        Console.WriteLine("The area of the rectangle: " + rectangle.Area().ToString())
        Console.WriteLine("The area of the square: " + square.Area().ToString())
        Console.WriteLine("The area of the triangle: " + triangle.Area().ToString())




        Console.ReadLine()
    End Sub




End Module

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS