How to avoid this problem?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kinghippo423
    New Member
    • Sep 2007
    • 7

    #1

    How to avoid this problem?

    I have an object receiving a short in argument. This argument is a code for a product and I want this number to be only between 1 and 1000.

    If I do this:

    Class a = new Class((short)96 5);
    System.out.prin tln(a.getArgume nt());

    This will return 965 as supposed

    Class a = new Class((short)12 00);
    System.out.prin tln(a.getArgume nt());

    This will cause my exception to pop and tell the user to use a code between 1 and 1000. Until here everything is fine. Now comes my problem:

    Class a = new Class((short)66 000);
    System.out.prin tln(a.getArgume nt());

    This will return 464. I want my exception to pop here too. I want to keep my short typevbecause it is useless to take 32 bytes for only a number in the 4 digits mamxium.

    How to solve this? For reference, this is my Class. Sorry it's in French but you will be able to understand easily my code don't worry !

    Thanks a lot

    package TP1;

    public class Article
    {
    public static void main(String[] args)
    {
    Article a = new Article((short) 66000,"Pen",(fl oat)23.45);
    System.out.prin tln(a.getNumero Article());
    }

    private short numeroArticle;
    private String descriptionArti cle;
    private float prixArticle;

    public Article(short pNumeroArticle, String pDescriptionArt icle, float pPrixArticle)
    {
    setNumeroArticl e(pNumeroArticl e);
    setDescriptionA rticle(pDescrip tionArticle);
    setPrixArticle( pPrixArticle);
    }

    public short getNumeroArticl e()
    {
    return numeroArticle;
    }

    public String getDescriptionA rticle()
    {
    return descriptionArti cle;
    }

    public float getPrixArticle( )
    {
    return prixArticle;
    }

    public void setNumeroArticl e(short pNumeroArticle)
    {
    if(pNumeroArtic le > 1000 || pNumeroArticle <= 0)
    throw new IllegalArgument Exception("Le numéro de l'article doit être compris entre 1 et 1000");
    else
    numeroArticle = pNumeroArticle;
    }

    public void setDescriptionA rticle(String pDescriptionArt icle)
    {
    descriptionArti cle = pDescriptionArt icle;
    }

    public void setPrixArticle( float pPrixArticle)
    {
    prixArticle = pPrixArticle;
    }
    }
  • madhoriya22
    Contributor
    • Jul 2007
    • 251

    #2
    Originally posted by kinghippo423
    I have an object receiving a short in argument. This argument is a code for a product and I want this number to be only between 1 and 1000.

    If I do this:

    Class a = new Class((short)96 5);
    System.out.prin tln(a.getArgume nt());

    This will return 965 as supposed

    Class a = new Class((short)12 00);
    System.out.prin tln(a.getArgume nt());

    This will cause my exception to pop and tell the user to use a code between 1 and 1000. Until here everything is fine. Now comes my problem:

    Class a = new Class((short)66 000);
    System.out.prin tln(a.getArgume nt());

    This will return 464. I want my exception to pop here too. I want to keep my short typevbecause it is useless to take 32 bytes for only a number in the 4 digits mamxium.

    How to solve this? For reference, this is my Class. Sorry it's in French but you will be able to understand easily my code don't worry !

    Thanks a lot
    Code:
    package TP1;
     
    public class Article 
    {
    public static void main(String[] args) 
    {
    Article a = new Article((short)66000,"Pen",(float)23.45);
    System.out.println(a.getNumeroArticle());
    }
     
    private short numeroArticle;
    private String descriptionArticle;
    private float prixArticle;
     
    public Article(short pNumeroArticle, String pDescriptionArticle, float pPrixArticle)
    {
    setNumeroArticle(pNumeroArticle);
    setDescriptionArticle(pDescriptionArticle);
    setPrixArticle(pPrixArticle);
    }
     
    public short getNumeroArticle()
    {
    return numeroArticle;
    }
     
    public String getDescriptionArticle()
    {
    return descriptionArticle;
    }
     
    public float getPrixArticle()
    {
    return prixArticle;
    }
     
    public void setNumeroArticle(short pNumeroArticle)
    {
    if(pNumeroArticle > 1000 || pNumeroArticle <= 0)
    throw new IllegalArgumentException("Le numéro de l'article doit être compris entre 1 et 1000");
    else
    numeroArticle = pNumeroArticle;
    }
     
    public void setDescriptionArticle(String pDescriptionArticle)
    {
    descriptionArticle = pDescriptionArticle;
    }
     
    public void setPrixArticle(float pPrixArticle)
    {
    prixArticle = pPrixArticle;
    }
    }
    Hi,
    Use code tags while posting code.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Do not cast to short here
      [CODE=java] Article a = new Article((short) 66000,"Pen",(fl oat)23.45);[/CODE]

      Your setMethod that you are calling from the constructor is already doing the check so there is no need to check there as well.

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        (Is there an error in the QUOTE-Tag?)
        Quote from kinghippo423:
        [CODE=java]
        package TP1;

        public class Article
        {
        public static void main(String[] args)
        {
        Article a = new Article((short) 66000,"Pen",(fl oat)23.45);
        System.out.prin tln(a.getNumero Article());
        }

        private short numeroArticle;
        private String descriptionArti cle;
        private float prixArticle;

        public Article(short pNumeroArticle, String pDescriptionArt icle, float pPrixArticle)
        {
        setNumeroArticl e(pNumeroArticl e);
        setDescriptionA rticle(pDescrip tionArticle);
        setPrixArticle( pPrixArticle);
        }

        public short getNumeroArticl e()
        {
        return numeroArticle;
        }

        public String getDescriptionA rticle()
        {
        return descriptionArti cle;
        }

        public float getPrixArticle( )
        {
        return prixArticle;
        }

        public void setNumeroArticl e(short pNumeroArticle)
        {
        if(pNumeroArtic le > 1000 || pNumeroArticle <= 0)
        throw new IllegalArgument Exception("Le numéro de l'article doit être compris entre 1 et 1000");
        else
        numeroArticle = pNumeroArticle;
        }

        public void setDescriptionA rticle(String pDescriptionArt icle)
        {
        descriptionArti cle = pDescriptionArt icle;
        }

        public void setPrixArticle( float pPrixArticle)
        {
        prixArticle = pPrixArticle;
        }
        }[/CODE](End of Quote)

        As I'm sure you've guessed, it's a problem with the type short. A simple
        [CODE=java]
        System.out.prin tln("Max: " + Short.MAX_VALUE );
        System.out.prin tln("Min: " + Short.MIN_VALUE );
        [/CODE]will tell you, that the maximum value for short numbers is 32767 and the minimum is -32768. So the short number 32768 equals the short number -32768, as the line
        [CODE=java]
        System.out.prin tln((short)3276 8 == (short) -32768);
        [/CODE]will prove.

        To solve this problem, I'd change the class, so that it takes integers and then checks, if they are larger than Short.MAX_VALUE . If so, they should be set to a short number, which is too big but in range.

        Greetings,
        Nepomuk

        Comment

        • kinghippo423
          New Member
          • Sep 2007
          • 7

          #5
          I tried what you siad but it doesn't work. It is possible to give a working little example of using short arguments and controlled its value to be only between 1 and 1000 no matter what is the value of the arguments?

          I'm so lost right now. BTW, if I transform my short into int, I get the same problem because if i go over limit i will have the same problem. I will give a int over 1000 but still be between 1 and 1000 for Java.

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            Originally posted by kinghippo423
            I tried what you siad but it doesn't work. It is possible to give a working little example of using short arguments and controlled its value to be only between 1 and 1000 no matter what is the value of the arguments?

            I'm so lost right now. BTW, if I transform my short into int, I get the same problem because if i go over limit i will have the same problem. I will give a int over 1000 but still be between 1 and 1000 for Java.
            Here's an example:
            [CODE=java]
            public void setNumeroArticl e(int value) throws IllegalArgument Exception
            {
            if(value >= 1 && value <= 1000)
            {
            // set the value, which is converted to short
            }
            else // throw an exception
            }
            [/CODE]It's a very simple test, but it does the job.

            The trick is, to cast it to short after checking the size.

            Greetings,
            Nepomuk

            Comment

            Working...