Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
is it possible to have a variable set in the group of Z4. So that if i had the following code
"int" x = 3;
x = x + 1;
the value of x would be 0. I know I can use mod 4 but I was wondering if you can change "int" to make it mod 4
I know I can use mod 4 but I was wondering if you can change "int" to make it mod 4.
It is not possible in Java.
(In fact, I cannot think of any mainstream programming language where you can do this. In Ada and one or two other languages you can define types that are subranges of an integer type, but I don't recall one where you could define a range type with modular arithmetic.)
Here are some alternatives:
Define a class with static helper methods that perform arithmetic on Z4 values stored as int.
Define a class whose values represent Z4 values with a range of arithmetic methods.
Just do the arithmetic using regular int and integer operators, and convert to Z4 at the appropriate time by masking out all but the 2 bottom bits.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I'm curious if there are seeds for java.util.random which have weird/surprising properties. This has little practical use, but I'm still curious.
edit: by weird/surprising properties I mean repeating values or atypical patterns.
Yes!
The java.util.Random object's constructor takes in a long as an argument. A bit of sifting through internal classes, we find that this long is processed through the following function:
private static long initialScramble(long seed) {
return (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1);
}
The goal of this function is to scramble the seed enough so the resulting seed is uniformly distributed across the long range (or something like that).
All we have to do is find an input to this function where it will produce an output of 0 to break things 😊
A bit of brute forcing and I was able to find the long -9223372011639871891
, Using this long as an input, the function returns 0!
Now, we can define a random like the following:
var random = new Random(-9223372011639871891L);
The first call to random.nextInt() will always be zero, no matter the range!
Initial calls to other random functions (nextDouble, nextFloat, etc.) will also produce VERY low values (but not exactly zero)
Of course, this all only applies to the first call to the "next" functions, but still cool regardless!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I created a same sized - stack based simple calculator for adding 2 operands. I want to know whether my addition becomes incorrect when specific (valid) values are entered. The 2 stacks take integer values and are having the same number of digits (i.e. 400 [3 digits] and 900 [3 digits]).
It depends on the algorithm you're using. From the question it is not clear, but let's assume your calculator can perform basic arithmetic. First off, you want to test each operation separately, because they have different equivalence classes of their inputs. For example, for multiplication it would be: 0, 1, minimum and maximum values, and their negations. Testing almost always will not be exhaustive, but using equivalence classes, you can pick one value from each class to make sure that each class is covered with a test.
Back to your question, you may use min/max values, and anything that you think may break your code.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If I have an integer that will have always a positive number less than 16 , can I just cast it to byte
int i = 5;
byte b = (byte) i;
or should I have unexpected behaviour when converting it back to integer on different devices?
Thanks
No, you won't get unexpected behaviour converting a byte between 0 and 15 into an int on different platforms. One of the strengths of Java is that it precisely defines what happens with such conversions, so that they are always platform independent.
You could always use the Integer class and use byteValue.
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#byteValue()
which simply does
return (byte)value;
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have to deal with a domain object that's real name is 351K-Report. According to the Java naming convention its forbidden to use a number at the beginning of an identifier.
I don't want to fully spell out the number. And, I also think that it's a bad idea to place an underline in front of the number.
But what is the recommended alternative?
UPDATE
There are also other reports, like SpecReport, TopReport, LF10Report and so on. So I'm very doubtful that inverting parts of the noun changes the meaning of the whole project.
Maybe reverse it. For example:
report351K
That would be very bad..
Imagine this:
int 1d = 3;
double d = 1d * 2;
What would be d?
Alternatives:
Since variables that begins with _ usually indicates for class member, I would use report351K.
if you really want to do this then _351KReport but I don't think you should do this. try to make something meaningful of it and at the same time is convineient to Java
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
The first column will have serial number, the second, third, and fourth will have x-coordinate, y-coordinate and velocity. I might have to retrieve any of the fields, given the serial number. Say I want to get the y-coordinate of serial number 7, or the velocity of serial number 10. One way is to have an inner class with x,y and velocity and map serial numbers to objects of the inner class. But it makes things look complicated and retrieving a particular field value seems complex. Any better solution?
Create a key object which implements hashCode() and equals() correctly. It appears that your serial numbers might be directly usable.
Create a data object with all fields you will need to store, and a getX() and setX() for each field x.
Create a Map<KeyObject,DataObject> and use that with your (key, data) pairs.
Be very careful to remove outdated objects or you will have a memory leak.
What about using Arrays or Map (Collections -> Map ) ?
With arrays: just create one array with the dimensions you need. In your case:
Array[number_of_itens_to_store][3]
// 3 => 0 = id, 1 = x, 2 =y, 3 = speed
With Maps: take a look at http://download.oracle.com/javase/6/docs/api/java/util/Map.html . You can use your ID as the K (key) and an array as the value.
You can take a look at Collections (http://download.oracle.com/javase/tutorial/collections/index.html) and even create your own collection, just following the tutorials.