Results 1 to 4 of 4

Thread: HW Help! Calculate Areas and Classes

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Posts
    1

    HW Help! Calculate Areas and Classes

    here are the instruction/requirements, i will post what i have already at the bottom, just wanted to provide this for reference

    Create a class Triangle with a constructor that takes the three sides as inputs. This class has three properties side1, side2 and side3 that are set inside the constructor.
    Create a function CalculateArea in this class. The area of a triangle is √(s(s-side1)(s-side2)(s-side3)) where s=(side1+side2+side3)/2. This function returns the area of the triangle.
    Create another function to obtain the message “This is a general triangle”.
    Create second class EquilateralTriangle that inherits Triangle that has a new constructor. The constructor should call the parent constructor by MyBase.New and passing the sides into the parent constructor.
    Override the function CalculateArea and the use the following formula to calculate area.
    √3/4 side1*side1
    Override the other function to obtain the message as “This is an Equilaeral triangle”.
    Create third class IsoscelesTriangle that inherits Triangle that has a new constructor. The constructor should call the parent constructor by MyBase.New and passing the sides into the parent constructor.
    Override the function CalculateArea and use the following formula to calculate area. Suppose side1=side3 and side 2 is different (find what isosceles triangle means using Google)
    side2*√(4*side1*side1-side2*side2)/4.
    Override the other function to obtain the message as “This is an Isosceles triangle”.
    Inside class TriangleCreatorForm, first create a reference of type Triangle. Do not instantiate it by using New.
    On click of button Create, make sure that the inputs are positive numbers between 0 and 100. Store the inputs in relevant variables. Based on side lengths, check whether the triangle is equilateral (all sides equal), isosceles (only two sides equal) or general (all others). Based on the type of triangle, create the instance of Equilateral Triangle, Isosceles Triangle or Triangle. For example, if your reference is triangleObj create by “Dim triangleObj as Triangle” in step 12, and you find that all the sides are equal, then create an EquilateralTriangle object by saying “triangleObj=New EquilateralTriangle(side1,side2,side3)”.
    This example illustrates that you can create a reference of type parent and instantiate an object of type child.
    Once you have instantiated the object triangleObj, call the calculateArea method to obtain the area of the shape and also get the message of the shape. Suppose the shape is Triangle. Then, the label should show a message “This is a general triangle with area 12.56765577”. Except “with area” all other parts in the message are received from the class Triangle.

    So i'm a little confused on what to do to make it work. I get three text box to put the dimension in and a button. with a label for display.

    This is what i have so far
    vb.net Code:
    1. Public Class TriangleCreatorForm
    2.  
    3.     Private Sub createButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles createButton.Click
    4.         Dim triangObj As triangle(s_side1, s_side2, s_side3)
    5.  
    6.     End Sub
    7.  
    8. End Class ' TriangleCreatorForm
    9.  
    10. Public Class triangle
    11.  
    12.     Public s_side1 As Double
    13.     Public s_side2 As Double
    14.     Public s_side3 As Double
    15.  
    16.     Public Property side1() As Double
    17.         Get
    18.  
    19.         End Get
    20.         Set(ByVal value As Double)
    21.             s_side1 = value
    22.         End Set
    23.     End Property
    24.  
    25.     Public Property side2() As Double
    26.         Get
    27.  
    28.         End Get
    29.         Set(ByVal value As Double)
    30.             s_side2 = value
    31.         End Set
    32.     End Property
    33.  
    34.     Public Property side3() As Double
    35.         Get
    36.  
    37.         End Get
    38.         Set(ByVal value As Double)
    39.             s_side3 = value
    40.  
    41.         End Set
    42.     End Property
    43.  
    44.     Function calculateArea() As Integer
    45.         Dim s As Double
    46.  
    47.         s = (s_side1 + s_side2 + s_side3) / 2
    48.         Dim area As Double
    49.  
    50.         area = Math.Sqrt(s * (s - s_side1) * (s - s_side2) * (s - s_side3))
    51.  
    52.     End Function
    53.     Function generaltriangle() As Double
    54.  
    55.  
    56.     End Function
    57.  
    58. End Class
    59.  
    60. Public Class EquilateralTriangle
    61.     Inherits triangle
    62.  
    63. End Class
    64.  
    65. Public Class IsoscelesTriangle
    66.     Inherits triangle
    67. End Class
    Sorry for the long post but I just don't know where to go from here. This is a homework so i don't expect answers, just guidance. Total Noob at this.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: HW Help! Calculate Areas and Classes

    Ok, so the next thing to do is jump right in with the next task. You aren't showing anything in the generaltriangle method, but I'll bet you know how to use a messagebox, so that one is easy.

    You have your inherited classes set up, roughly. The next request is to create a constructor for them. You have already written a constructor for the base, so go ahead and write one for the inherited class. You will almost certainly get a prompt from the compiler telling you what to do next.

    And so forth. Once you have created the constructors in the child classes, you should have a fair idea how to create the other functions.
    My usual boring signature: Nothing

  3. #3
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: HW Help! Calculate Areas and Classes

    I feel Dim triangObj As triangle(s_side1, s_side2, s_side3)
    should be Dim triangObj As New triangle(s_side1, s_side2, s_side3) Then add a Sub New() in your triangle class..

    Example:
    vbnet Code:
    1. 'This passes on the values of our trangle's sides.
    2. Dim triangObj As New Triangle(s_side1, s_side2, s_side3)
    3.  
    4. Friend Class Triangle
    5.  
    6.     Friend Property a As Double 'Leg 1
    7.     Friend Property b As Double 'Leg 2
    8.     Friend Property c As Double 'Hypotenuse
    9.  
    10.  
    11.     Sub New(ByVal a As Double, ByVal b As Double, ByVal c AS Double)
    12.         MyBase.New()
    13.  
    14.         Me.a = a
    15.         Me.b = b
    16.         Me.c = c
    17.     End Sub
    18.  
    19.     Friend Function Area As Double
    20.     End Function
    21.  
    22. End Class

    I believe this is what Shaggy Instructor was.. instructing lol But as he said, the designer should be informing you of all this.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: HW Help! Calculate Areas and Classes

    HA! I hadn't even noticed that. A constructor that takes the three sides as arguments is the first line of the first requirement, and I hadn't noticed that it had been skipped over completely.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width