on creating a class

  • 14 years ago
    Hello  In my Deitel book  there is this exercise. (p442)

    (This is an application that allows user to enter the lengths of the 3 sides of a triangle)
     "Define a constructor that will take the three lengths of the three sides of the triangle as arguments. Create 3 properties that enable clients to access and modify the lengths of the three sides.If the user enters a negative value that side should be assigned a 0 value"
     
    I have gotten  this far:

    Public Class Triangle

    'declare integers for each of the 3 sides

    Private m_intSideOne As Integer

    Private m_intSideTwo As Integer

    Private m_intSideThree As Integer

    'Triangle constructor, sides supplied

    Public Sub New(ByVal SideOne As Integer, ByVal SideTwo As Integer, ByVal SideThree As Integer)

    SideA = SideOne

    SideB = SideTwo

    SideC = SideThree

    End Sub

    'property SideA

    Public Property SideA() As Integer

    Get

    Return m_intSideOne

    End Get

    Set(ByVal Value As Integer)

    'if the value entered is a negative

    If (Value > 0) Then

    m_intSideOne = Value

    Else

    m_intSideOne = 0

    End If

    End Set

    End Property

    Public Property SideB() As Integer

    Get

    Return m_intSideTwo

    End Get

    Set(ByVal Value As Integer)

    If (Value > 0) Then

    m_intSideTwo = Value

    Else

    m_intSideTwo = 0

    End If

    End Set

    End Property

    Public Property SideC() As Integer

    Get

    Return m_intSideThree

    End Get

    Set(ByVal Value As Integer)

    If (Value > 0) Then

    m_intSideThree = Value

    Else

    m_intSideThree = 0

    End If

    End Set

    End Property

    End Class

    but then there is this :

    "Create two more properties in the Triangle class. One determines wheather the sides form a right angle. The other an equilateral triangle. These properties are considered read only because you only define a Get accessor....."

    this last requirement is giving me the problem. I would have just written it as an If... statement in the application not in the Class.

    any advice much appreciated as always.

    DA

  • 14 years ago

    You could do something like:

    Public ReadOnly Property IsEquilateral() As Boolean
    Get
    Return (Me.SideA = Me.SideB) AndAlso (Me.SideB = Me.SideC)
    End Get
    End Propery

    Public ReadOnly Property IsRightTriangle() As Boolean
    Get
    'If C is not always the hypotenus,
    'You will need to find the longest side
    'And treat that as SideC
    Return Me.SideA ^ 2 + Me.SideB ^ 2 = Me.SideC ^ 2
    End Get
    End Property














    The reason for doing this in the class is to provide a common function so the user doesn't have to do it himself.  But this is more as an example anyway and not a "real-world" class...




  • 14 years ago

    dear Nimpo :
    thanks for the feedback, it really helped me on my way. This is what I cooked up after a long lean look: maybe you can see any way that i might have done it better ? i think the calculations for what makes a Right Triangle are way off, but like you said, its more of an example and has shown me how to create a class and how to make an object of it.

    Public Class Triangle
       'declare integers for each of the 3 sides
       Private mintSideOne As Integer
       Private m
    intSideTwo As Integer
       Private mintSideThree As Integer

       'Triangle constructor, sides supplied
       Public Sub New(ByVal SideOne As Integer, ByVal SideTwo As Integer, ByVal SideThree As Integer)

          SideA = SideOne
          SideB = SideTwo
          SideC = SideThree
       End Sub

       'property SideA
       Public Property SideA() As Integer
          Get
             Return m
    intSideOne
          End Get
          Set(ByVal Value As Integer)
             'if the value entered is a negative
             If (Value > 0) Then
                mintSideOne = Value
             Else
                m
    intSideOne = 0
             End If

          End Set
       End Property

       Public Property SideB() As Integer
          Get
             Return mintSideTwo
          End Get
          Set(ByVal Value As Integer)
             If (Value > 0) Then
                m
    intSideTwo = Value
             Else
                mintSideTwo = 0
             End If

          End Set
       End Property

       Public Property SideC() As Integer
          Get
             Return m
    intSideThree
          End Get

          Set(ByVal Value As Integer)
             If (Value > 0) Then
                mintSideThree = Value
             Else
                m
    intSideThree = 0
             End If
          End Set
       End Property

       Public ReadOnly Property IsEquilateral() As Boolean
          Get
             Return (Me.SideA = Me.SideB) And (Me.SideB = Me.SideC) AndAlso (Me.SideA = Me.SideC)
          End Get
       End Property

       Public ReadOnly Property IsRightTriangle() As Boolean
          Get
             'If C is not always the hypotenus,
             'You will need to find the longest side
             'And treat that as SideC
             Return (Me.SideA = Me.SideB)Or (Me.SideA = Me.SideB)Or (Me.SideB=Me.SideA) Or (Me.SideB=Me.SideC) _
             Or (Me.SideC=Me.SideA) Or (Me.SideC = Me.SideB)
          End Get
       End Property


    End Class



















































































  • 14 years ago

    The calculations I gave should be correct.  An equilateral triangle is one in which all 3 sides have equal lengths (so if A = B and B = C then you don't even have to check if A = C.  AndAlso is slightly faster than And in some cases because if the first condition is False, it doesn't bother evaluating the second, since it's irrelevant).

    A rigth triangle is one in which one angle = 90 degrees (pi/2 radians).  Every right triangle has the property so that A^2 + B^2 = C^2 (C must be the side opposite the right angle/the longest side/the hypotenuse, they are all the same things).  Thus, if this statement isn't True, you do not have a right triangle.





Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Measuring programming progress by lines of code is like measuring aircraft building progress by weight.” - Bill Gates