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)965);
System.out.println(a.getArgument());
This will return 965 as supposed
Class a = new Class((short)1200);
System.out.println(a.getArgument());
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)66000);
System.out.println(a.getArgument());
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",(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;
}
}
5 1428
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)965);
System.out.println(a.getArgument());
This will return 965 as supposed
Class a = new Class((short)1200);
System.out.println(a.getArgument());
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)66000);
System.out.println(a.getArgument());
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",(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.
Do not cast to short here - Article a = new Article((short)66000,"Pen",(float)23.45);
Your setMethod that you are calling from the constructor is already doing the check so there is no need to check there as well.
Nepomuk 3,112
Recognized Expert Specialist
(Is there an error in the QUOTE-Tag?)
Quote from kinghippo423: -
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;
-
}
-
}
(End of Quote)
As I'm sure you've guessed, it's a problem with the type short. A simple -
System.out.println("Max: " + Short.MAX_VALUE);
-
System.out.println("Min: " + Short.MIN_VALUE);
-
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 -
System.out.println((short)32768 == (short) -32768);
-
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
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.
Nepomuk 3,112
Recognized Expert Specialist
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: -
public void setNumeroArticle(int value) throws IllegalArgumentException
-
{
-
if(value >= 1 && value <= 1000)
-
{
-
// set the value, which is converted to short
-
}
-
else // throw an exception
-
}
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: harry |
last post by:
Hi,
I have a program that runs on multiple client pc's. Occasionally one or more
of those pc's use VPN to connect to another corporate network. When using
VPN they need to set proxy server in...
|
by: |
last post by:
I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of
a new pointer)
It has no sense (it is dangerous) to allocate an...
|
by: Carlos Andr?s |
last post by:
Hi everybody.
I've got a problem. I'd like to avoid opening a new window when you
have pressed the shift key and you click in the left button of the
mouse. I've tried the next solution, in the...
|
by: Pushkar Pradhan |
last post by:
I want to time my matrix multiply code (in MFLOPS). I want to run the
code 100,000 times or some other big number.
This can be done 2 ways (for and while loops):
timer1 = time(NULL);
for(n = 0;...
|
by: Scott Yost |
last post by:
I have a managed class A which I import via a DLL.
public __gc class A
{
public B otherClass;
}
And another class C which is just in a CPP file - not in the DLL.
#include <b.h>
class C
| |
by: rover8898 |
last post by:
Hello all,
I have a programming problem/challenge.
Let explain my scenario:
I have a C program (distributed accross many files and functions) that
is responsible for handling hardware. This...
|
by: gzaxar |
last post by:
Hi to all in forum.
It is my first post here.
I am quite new in MsAccess programming.
Here is a problem which i am facing to.
I want to keep records of employees CV's. More specifically i want...
|
by: visu |
last post by:
I am currently working on my personl website a completely DB driven
web application. in that i ve updating the images thru my admin
panel .. but i am getting the old images not the update one when...
|
by: StephQ |
last post by:
This is from a thread that I posted on another forum some days ago.
I didn't get any response, so I'm proposing it in this ng in hope of
better luck :)
The standard explanation is that pointer...
|
by: howa |
last post by:
Since it is part fo the standard, why I always heard that we should
avoid iframe?
any comments?
thanks.
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |