Hey guys, I am having trouble building this class, my teacher is not very helpful. Here is the code I have been working on, I am still confused on methods, constuctors, properties, etc. He gave me a wierd example to try to follow. Here are the requirements my teacher wants as well as the code:

. Design a class named Rectangle to represent a rectangle. The class contains:
• Two double data fields named dblwidth and dblheight that specify the width and height of the rectangle. The default values are 1 for both dblwidth and dblheight.
• A string data field named strcolor that specifies the color of a rectangle. Hypothetically, assume that all rectangles have the same color. The default color is white.
• A no parameter constructor will be created which represents the default rectangle.
• A three parameter (width, length, color) will be created.
• Three properties (width, length, and color) will be created.
• A method named getArea() will be created to find the area (area = width * length).
• A method named getPerimeter() will be created to find the perimeter (perimeter = 2 * width + 2 * length).
Write a test program in Windows that asks for the width, length and color of a rectangle and find the area and perimeter.

Public Class Rectangle

    Private dblWidth As Double
    Private dblHeight As Double
    Private strColor As String
    Public Sub New(ByVal vwidth As Integer, _
                    ByVal vlength As Integer, _
                    ByVal vcolor As String)
        width = vwidth
        length = vlength
        color = vcolor
    End Sub
    Public Property width() As Integer
        Get
            Return dblWidth()
        End Get
        Set(ByVal value As Integer)
            dblWidth = value
        End Set
    End Property

    Public Property length() As Integer
        Get
            Return
        End Get
        Set(ByVal value As Integer)

        End Set
    End Property

    Public Property color() As String
        Get

        End Get
        Set(ByVal value As Integer)

        End Set
    End Property

    Public Sub getArea()
        Dim area As Integer = width * length

    End Sub

    Public Sub getPerimeter()
        Dim perimeter As Integer = (2 * width) + (2 + length)

    End Sub
End Class

Recommended Answers

All 5 Replies

Hereafter you can find an example of the class definition.
Some hints:
* The internal class values, and those of the propertires must match in the type of variable
* You need Functions instead of Subroutines to return values.

'
'   The Rectangle class:
'
'   This class will use the system.drawing to handle the rectangle color.
'   You shoul need td add the system.drawuing .net componenet to your project references
'   If otherwise required, you can use a string to hold the color name.
'
Public Class clsRectangle
    '
    '   The class internal width
    '
    Private dblWidth As Double
    '
    '   The class internal heigth
    '
    Private dblHeight As Double
    '
    '   The class internal color
    '
    Private clrColor As System.Drawing.Color
    '
    '   Default constructor, without parameters
    '
    Public Sub New()
        '
        '   Default height assigned to the class internal heigth
        '
        Me.dblHeight = 1D
        '
        '   Default width assigned to the class internal width
        '
        Me.dblWidth = 1D
        '
        '   Default white color assigned to the class internal color
        '
        Me.clrColor = Drawing.Color.White
        '
    End Sub
    '
    '   Contructor with parameters: 
    '       dblwidth: a double indicating the desired width of the rectangle
    '       dbllength: a double indicating the desired heigth of the rectangle
    '       rectanglecolor: a system.drawing.color to paint the rectangle
    '
    Public Sub New(ByVal dblwidth As Double, _
                    ByVal dbllength As Double, _
                    ByVal rectanglecolor As System.Drawing.Color)
        '
        '   Assign the width to the internal class width
        '
        Me.dblWidth = dblwidth
        '
        '   Assign the height to the internal class height
        '
        Me.dblHeight = dbllength
        '
        '   Assign the color to the internal class color
        '
        Me.clrColor = rectanglecolor
        '
    End Sub
    '
    '   The property Width will handle a double representing the internal class width
    '
    Public Property Width() As Double
        Get
            '
            '   Return the internal class width
            '
            Return Me.dblWidth
        End Get
        Set(ByVal value As Double)
            '
            '   Assign the new value of the internal class width
            '
            Me.dblWidth = value
        End Set
    End Property
    '
    '   The property Length will handle a double representing the internal class heigth
    '
    Public Property Length() As Double
        Get
            '
            '   Return the internal class height
            '
            Return Me.dblHeight
        End Get
        Set(ByVal value As Double)
            '
            '   Assign the new value of the internal class width
            '
            Me.dblHeight = value
        End Set
    End Property
    '
    '   The property Color will handle the system.drawing.color representing the internal class color
    '
    Public Property Color() As System.Drawing.Color
        Get
            '
            '   Returns the internal class color
            '
            Return Me.clrColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            '
            '   Assign the new value of the internal class color
            '
            Me.clrColor = value
        End Set
    End Property
    '
    '   Function getArea will return a double representing the curren rectangle area
    '
    Public Function getArea() As Double
        '
        '   Returns the product of the internal width by internal height (lengh)
        '
        Return Me.dblWidth * Me.dblHeight
        '
    End Function
    '
    '   Function getPerimeter will return a double representing the perimeter length
    '
    Public Function getPerimeter() As Double
        '
        '   Returns the sum of twice the intenal width and twice the internal height (length)
        '   
        Return (2 * Me.dblWidth) + (2 * Me.dblHeight)
        '
    End Function
End Class

In the test program you'll need to instantiate the class using some thing like:

'
' for void instantiation
'
Dim MyDefaultRectangle as clsRectangle = new clsRectangle()
'
' for valued instantiation (Ie: Width=22.5, Height(length)= 33.42 and color Violet)
'
Dim MyValuedRectangle as clsRectangle = new clsRectangle( 22.5, 33.42, New System.Drawing.Color(Drawing.Color.Violet)
'

In the program it is giving me an error that says "Type.System.Color has no constructors. Here is the code in the form and what you provided for the class:

Public Class Form

    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click

        Dim MyDefaultRectangle As clsRectangle = New clsRectangle()
        Dim MyValuedRectangle As clsRectangle = New clsRectangle(22.5, 33.42, New System.Drawing.Color(Drawing.Color.Violet))
    End Sub
End Class

Here is the class:

Public Class clsRectangle

    ' The class internal width

    Private dblWidth As Double
    Private dblHeight As Double
    Private clrColor As System.Drawing.Color

    ' Default constructor, without parameters

    Public Sub New()

        ' Default height assigned to the class internal heigth

        Me.dblHeight = 1D

        ' Default width assigned to the class internal width

        Me.dblWidth = 1D

        ' Default white color assigned to the class internal color

        Me.clrColor = Drawing.Color.White

    End Sub

    ' Contructor with parameters:

    ' dblwidth: a double indicating the desired width of the rectangle

    ' dbllength: a double indicating the desired heigth of the rectangle

    ' rectanglecolor: a system.drawing.color to paint the rectangle

Public Sub New(ByVal dblwidth As Double, ByVal dbllength As Double, ByVal rectanglecolor As System.Drawing.Color)

        ' Assign the width to the internal class width

        Me.dblWidth = dblwidth

        ' Assign the height to the internal class height

        Me.dblHeight = dbllength

        ' Assign the color to the internal class color

        Me.clrColor = rectanglecolor

    End Sub

    ' The property Width will handle a double representing the internal class width

    Public Property Width() As Double

        Get

            ' Return the internal class width

            Return Me.dblWidth

        End Get

        Set(ByVal value As Double)

            ' Assign the new value of the internal class width

            Me.dblWidth = value

        End Set
    End Property

    ' The property Length will handle a double representing the internal class heigth

    Public Property Length() As Double

        Get

            ' Return the internal class height

            Return Me.dblHeight

        End Get
        Set(ByVal value As Double)

            ' Assign the new value of the internal class width

            Me.dblHeight = value

        End Set
    End Property

    ' The property Color will handle the system.drawing.color representing the internal class color

    Public Property Color() As System.Drawing.Color

        Get

            ' Returns the internal class color

            Return Me.clrColor

        End Get
        Set(ByVal value As System.Drawing.Color)

            ' Assign the new value of the internal class color
            Me.clrColor = value
        End Set
    End Property

    ' Function getArea will return a double representing the curren rectangle area

    Public Function getArea() As Double

        ' Returns the product of the internal width by internal height (lengh)

        Return Me.dblWidth * Me.dblHeight

    End Function

    ' Function getPerimeter will return a double representing the perimeter length

    Public Function getPerimeter() As Double

        ' Returns the sum of twice the intenal width and twice the internal height (length)

        Return (2 * Me.dblWidth) + (2 * Me.dblHeight)
    End Function

End Class

I believe the Color Class Doesn't allow constructors, which means you
omit the keyword "NEW", Instead call the class directly. eg.

Color.Violet

You may also want to include an import statement on top of your class
to avoid quantifying the code.

Imports System.Drawing

Great! Now how do i get the area and the perimeter and are from clsRectangle to the area and perimeter boxes?

Great! Now how do i get the area and the perimeter and are from clsRectangle to the area and perimeter boxes?

I assume you mean text boxes. There is a string property called "Text" that you can set. When you do, you can set that value to the returned property or function to display it and that shows up on your text box.

That's assuming the box is visable and has a contrasting forecolor so you can see the text. (Default) Since this is intended as a display box make it read only.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.