The below question is confusing me, I am not sure if my thinking is right. This is the question
Given three straight lines a, b & c. They will be able to form a
triangle provided that sum of any two lines is always greater than the
third line (i.e. a + b > c and b + c > a and a + cb). Write a Java class Triangle with the following:
Attributes: length of the three sides of the triangle
Behaviour:
- Constructor that sets the length of the three sides to the values passed in. The constructor should throw an InvalidValueException
object when the values are not able to form a triangle.
- findArea() method to calculate the area of the Triangle object using the formula area = a +b+c
Just a heads up the next part of the question is to create a GUI. I already did that.
So the above question is it asking me to just simply get the values, but from where? Is it through the GUI? or Commandline base? the question was not specific so can I assume that simmply create variables that are ready to accept the values?
Why not just hardcode it for the moment in a test class or similar e.g.
In this scenario you’re interested in testing your code for various functions (valid vs. invalid) and so I would write a test (or multiple tests) that have particular sets of data e.g.
In the latter case you’d want to catch the exception and bail out of your program with an error if you don’t get an exception.
You might want to check out unit test frameworks to help you with this.
Edit: Your amended question now contains setters for the
Trianglesides. I think you’re better off making the fields read only, doing this on construction and throwing an Exception from the constructor. If you need setters, then you have to call thevalidate()method at some stage otherwise you’re potentially creating aTrianglewhich is invalid. Note that you can’t call the validator upon each setter invocation since you may be setting multiple sides and an intermediate state may be invalid.