Hi,
I am new to java programming. Is it possible to declare a variable of type "double and should not be negative"?
Could you please help me this. Thanks!!
Printable View
Hi,
I am new to java programming. Is it possible to declare a variable of type "double and should not be negative"?
Could you please help me this. Thanks!!
The Java Language Environment
Short answer: no. You can use conditionals to prevent it from being negative though.
From my understanding you wouldn't declare 'double not negative' but declare a double and then set a condition that if it is negative the program prompts the user to enter a different double.
If it's part of a program and isn't receiving user input then either convert it to a positive (all I'd do is multiply it by 2 then divide by 2, loss of a little accuracy perhaps though) or set some sort of
condition on it.Code:if(i<0)
~Karenthian~
Sorry, what I should have written is either times by -2 or squared. My bad.
~Karenthian~
An easier approach would be to multiply it by -1. perhaps it's even possible with the unary operator +
Assuming you have a nice class defined that will hold this data, and a nice setter that will set this attribute of objects of this nice class, then you'd simply add a check inside the setter that the parameter is actually positive. Throwing an exception if not.
Code:public class MyNiceClass {
private double myAttribute;
/**
nice bit of javadoc in here which says that myAttribute cannot be negative.
*/
public void setMyAttribute(double myAttribute) {
if (myAttribute < 0) throw new IllegalArgumentException("NEGATIVE!!"); // Or some other reasonable one
this.myAttribute = myAttribute;
}
}
I'd extend the Number class and create an immutable non-negative double class; something like this:
kind regards,Code:class PosDouble extends Number {
private double d;
public PosDouble(double d) {
if (d >= 0)
this.d= d;
else
throw new IllegalArgumentException(d+" < 0");
}
// the Number methods go here, and toString() etc.
}
Jos
Instead of creating a class that throws exceptions, why not create a class that automatically changes it? Surely that's more useful?
I see how an exception is useful if the numbers are set and you need to know if they aren't negative, but if they simply 'have to be' negative set up an exception then add one of the given methods for converting it. That way you can make a method to check and possibly convert any number you want, and simply call it for each given number.
Yours stating the obvious,
~Karenthian~
The reason for throwing the exception is because the value is wrong.
Changing it (multiplying by -1 for example) is merely a case of guessing what the value should be. The plain fact is (and all you know is) that you have received an invalid value. Where it comes from is irrelevant.
If your double value should be non-negative then accepting a negative value is disguising another problem in your system...that being where this negative number (that shouldn't exist) is coming from.
So yes, it should be an exception.
True, and together with the fact that the class is immutable, throwing an unchecked exception at construction time guarantees that such an object will always be valid. Think of it, if d is negative, storing -d instead is just an arbitrary hack; it could've stored 42 just as well because that is also a positive value. But maybe people want to show their cleverness by showing the most tricky ways to make d a non-negative value.
kind regards,
Jos