I'm just starting to learn OOP w/ Java and ran into a problem, which got me to thinking, albeit probably too much. I've searched for an answer for some time, but finding the right question to ask is quite often the biggest challenge.
I've created a Class Triangle with two methods isTriangle() and typeOfTriangle(). My constructor has 3 parameters (side1, side2, side3).
This is my first experience where I realized a user could pass in parameters that would create an object that wasn't actually a Triangle as in the case where two sides added together are less than the third. I added logic to the constructor that would check to see if the actual parameters made a triangle. This did not at all seem like the right way to do it.
Question: Should the Triangle class be responsible for determining if the actual parameters create a viable Triangle? If so how? would that logic actually go in with the constructor?
Do you need to create the object first (non triangle) then determine if what you have created is viable with myTriangle.isTriangle(); ?
Doesn't seem as if the client/user should be responsible for determining if the Triangle class returned a viable Triangle. Shouldn't this be assumed? Although I've always heard assumption is the mother of all screw ups.
I didn't include code as its pretty rudimentary and I'm sure you all get the idea. Just not sure "where" this check should happen. Seems as if the class should not return anything if it's not a Triangle. I'm most likely way overthinking this.
Thanks for taking the time to read my post.