|
-
Dec 14th, 2011, 11:34 PM
#1
Thread Starter
New Member
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:
Public Class TriangleCreatorForm
Private Sub createButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles createButton.Click
Dim triangObj As triangle(s_side1, s_side2, s_side3)
End Sub
End Class ' TriangleCreatorForm
Public Class triangle
Public s_side1 As Double
Public s_side2 As Double
Public s_side3 As Double
Public Property side1() As Double
Get
End Get
Set(ByVal value As Double)
s_side1 = value
End Set
End Property
Public Property side2() As Double
Get
End Get
Set(ByVal value As Double)
s_side2 = value
End Set
End Property
Public Property side3() As Double
Get
End Get
Set(ByVal value As Double)
s_side3 = value
End Set
End Property
Function calculateArea() As Integer
Dim s As Double
s = (s_side1 + s_side2 + s_side3) / 2
Dim area As Double
area = Math.Sqrt(s * (s - s_side1) * (s - s_side2) * (s - s_side3))
End Function
Function generaltriangle() As Double
End Function
End Class
Public Class EquilateralTriangle
Inherits triangle
End Class
Public Class IsoscelesTriangle
Inherits triangle
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.
-
Dec 15th, 2011, 11:04 AM
#2
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
 
-
Dec 15th, 2011, 01:52 PM
#3
Hyperactive Member
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:
'This passes on the values of our trangle's sides. Dim triangObj As New Triangle(s_side1, s_side2, s_side3) Friend Class Triangle Friend Property a As Double 'Leg 1 Friend Property b As Double 'Leg 2 Friend Property c As Double 'Hypotenuse Sub New(ByVal a As Double, ByVal b As Double, ByVal c AS Double) MyBase.New() Me.a = a Me.b = b Me.c = c End Sub Friend Function Area As Double End Function 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.
-
Dec 15th, 2011, 02:25 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|