Results 1 to 12 of 12
  1. #1
    vahini is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default Double with no negative values.

    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!!

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The Java Language Environment

    Short answer: no. You can use conditionals to prevent it from being negative though.

  3. #3
    Karenthian is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    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
    Java Code:
    if(i<0)
    condition on it.
    ~Karenthian~

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Quote Originally Posted by Karenthian View Post
    convert it to a positive (all I'd do is multiply it by 2 then divide by 2
    Huh? How does that convert a negative double value to positive?

    db

  5. #5
    Karenthian is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    Sorry, what I should have written is either times by -2 or squared. My bad.
    ~Karenthian~

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    An easier approach would be to multiply it by -1. perhaps it's even possible with the unary operator +

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    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.
    Java 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;
        }
    }

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    I'd extend the Number class and create an immutable non-negative double class; something like this:

    Java 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.
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    An easier approach would be to multiply it by -1. perhaps it's even possible with the unary operator +
    Or even better yet use the purpose built Math.abs function ;)
    Currently developing Cave Dwellers, a Dwarf Fortress/Minecraft style game for Android.

  10. #10
    Karenthian is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    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~

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    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.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. find the unique values of a double array
    By tyang in forum New To Java
    Replies: 3
    Last Post: 09-11-2011, 02:47 PM
  2. Negative,Positive and Zero
    By dipowo in forum New To Java
    Replies: 2
    Last Post: 04-04-2011, 05:49 AM
  3. Removing the double values?
    By Lund01 in forum Advanced Java
    Replies: 13
    Last Post: 11-17-2010, 11:34 AM
  4. Negative one for illegal/default values
    By diablo84 in forum New To Java
    Replies: 1
    Last Post: 08-08-2010, 07:58 PM
  5. mutliplicatio of double values
    By katkamravi in forum New To Java
    Replies: 2
    Last Post: 04-13-2009, 02:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •