There used to be a user here with a signature saying something like this:-Ganesh Patekar wrote:. . . What is the solution to make compiler happy? . . .
But we are not trying simply to keep the compiler happy, we are trying to write good code. Fortunately the suggestions everybody has made will help no end. I shall make another suggestion:-Ain't the compiler not happy, ain't nobody happy.
Yes completely agreed because by assigning values to a and b in catch block makes greedy compiler happy ( Means compiles successfully) but that is logically incorrect, we won't get what we are expecting from this program.Campbell Ritchie wrote:But we are not trying simply to keep the compiler happy, we are trying to write good code.
All things are lawful, but not all things are profitable.
All things are lawful, but not all things are profitable.
All things are lawful, but not all things are profitable.
Knute Snortum wrote:It is best to declare variables as close to when they are used as possible. Currently, a, b, and c are declared outside of any method, so they are visible in all methods.
Ganesh Patekar wrote:Yes your going well only few changes IMO
class name always starts with capital letter so Triangle rather than triangle look good. I don't feel you need to create separate message method just to print message, you can move that code in getUserInput method. Instance variables sideA, sideB and sideC are declared in class so they are accessible to all instance methods of this class, you don't need to pass variables to validateTri method like this You can use them directly by thier name in validateTri and PyTheory methods like below.
So you can remove parameters of those two methods and can invoke method without any arguments like this
Suppose user entered value for sideA = 10 then in below code you are again assigning sideA = 10 ( value passed int side1 as parameter to PyTheory method ) where sideA already had value 10 assigned to it in getUserInput method so no need to reassign same value again. Would be much readable If you put circular brackets around each condition in If statements. I'm afraid you haven't considered the situation when user enters wrong input like string rather than int number, you better work on Campbell's suggetion and Knute's challenge
All things are lawful, but not all things are profitable.
Oh yes yes, I always name final static in this way but missed. Thank you for noticing.Knute Snortum wrote:If a variable is static and final, I'd call it READER rather than reader.
Campbell Ritchie wrote:I would suggest that you can overload your getXXX methods to permit ranges, then you can have error messages like, “Value must be between 1 and 100”.
Oh yes yes, I think I better name It, isInRange something like that.Fred Kleinschmidt wrote:If you name a method getInt(...), it should return an int.
That method will return an int betwen 0 and 100 (I think that is what I suggested originally) and will only print the error message for a valid int but an invalid value (e.g. -1, 1000). That method may need to throw an Exception; I shall leave you to work out when it should throw the Exception.Ganesh Patekar wrote:. . . overload that method by writing another method
public void getInt(String msg, int fromValue, int toValue ); which will only print appropriate message like, “Value of age must be between 1 and 100”. Is this what you were saying? . . .
I wasn't clear.A few minutes ago, I wrote:. . . print the error message for a valid int but an invalid value (e.g. -1, 1000). . . .
No. The idea of the loop is that it never throws an InputMismatchException. You take the ordinary method, which returns an int, and wrap it in something. I shall give you a hint:-What is going to happen in the latter case? How are you going to get out of it?Ganesh Patekar wrote:@ Campbell I hope this is what you meant
Opps!Campbell Ritchie wrote:What is going to happen in the latter case? How are you going to get out of it?
Oh yes your right,Paweł Baczyński wrote:Don't return magic numbers like that.
What if the method call was: and the user typed -1?
get schwifty. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|