Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.

Piet Souris

Master Rancher
+ Follow
since Mar 08, 2009
Netherlands
Cows and Likes
Cows
Total received
93
In last 30 days
1
Total given
65
Likes
Total received
510
Received in last 30 days
17
Total given
51
Given in last 30 days
1
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Piet Souris

You can further simplify the code that lets you only select suitable graphic files. Have a look at the class 'FileNameExtensionFilter'. It is very easy to use.
1 week ago
Forgot to mention: if a Callable signals that it is finished, then immediately calling its future.get() didn't work. So, there is some time lag there, looking into it to see how long it takes before the future is up to date.
A possible scenario would be a GUI program running from the EDT, and that tries to mimic CompletableFuture.anyOf. For instance a chess program that investigates the possible moves, but one Callable detects a forced mate or some very favourable move. In that case you don't want to spill costly time. Or when you simply want to update your GUI every time a task finishes, and you don't want to let the tasks do that for some reason.

I described a way in an earlier post, I gave it a shot, works very nicely.

Oops... my reply was meant for Vaibhav. Sorry for the confusion.
But why insisting on Future.get()? Some solutions have been peoposed that do not use this get() and do not block in any way.
Useful would be to make a method 'public static List<Integer> getPrimes(int limitInclusive)' that contains the code that is now in main. Then you can use it whenever you need it, with

Another way, interesting as a coding exercise, is that a number N is prime if it is not a multiple of any known prime number that is smaller than N. We know that 2 and 3 are primes, so we can start with a List<Integer> primes containing 2 and 3. Now, if a number N is not a multiple of any number in our List, then it is a prime number and can be added to the list, et cetera. It is certainly not as fast as the Sieve, but it makes for a short and elegant solution. Give it a try!
1 week ago
hi Emma,

you gave an implementation of the famous Sieve (of Eratosthenes). That is the simplest (and fastest) way I know. Maybe you could replace the multiplication by a summation, to get all the multiples, that will probably be just a little faster.

If you really want to make it easy on yourself: have a look at the BigInteger class, that has this method 'probablyPrime'. Believe it or not, but using a java 8 stream and this BigInteger, you would only need just one line of code!
1 week ago
You could have a class that implements Runnable, and when it has finished, fires a PropertyChange to the calling method. That can then do a non blocking future.get (if you make sure that that future can be derived from the PropertyChangeEvent). Anyone know if such a class already exists? A SwingWorker already has a bound property about the state, so you could use a SwingWorker for that (see the API).

Have you looked at the API of a CountDownLatch, whether that is suitable? Problem seems to be that it will also be blocking until all Runnables have finished.
In your code snippet you create a new Say by implementing the hello() method. This is incorrect, it must be the say() -method.
Eric gave some nice eamples, but if I limit myself to your code, you might do it this way:
2 weeks ago
First of all, I have not looked at the replies so far, so I might bring old news that others already have explained.

Secondly: if I compare the code to my code in my previous reply, I see that they store 'shift' numbers as well, like I did, but they put them in the array as soon as possible, making it look like there is inly one number stored (in temp), while I stored them in an array, inputting them in the end. Hmmm...

As I said, I never liked this kind of solutions, with all that index juggling, they make it hard to follow what is going on. So, I suggest to look at two variants, and see how the process should go on.
But formost: you use the statement:
from = (j - k) % size;
Now, that is dangerous, since when j - k < 0, the result will also be negative, causing an IndexOutOfBoundError.
So, use
from = j - k;
if (from < 0) from += size;

Now, lets look at an array of length 8, and with a shift of 3 and a shift of 4, and see what should happen. I use more meaningful names than you used. Instead of j, I use 'to', instead of d, I use 'from', and insead of i, I use 'loopnr'.
And I refer to 'shift' as name for the shift taking place. It makes the code a little more readable. I also use the variable 'stop', that determines when a new loop should start. So here goes:

8, 3, gcd = 1
loopnr 0
to = size - 1 - loopnr = 7
temp = a[to]
stop = (to + shift) % size = 2
entering the loop, naming only the indices from and to for brevity
7 -> temp
4 -> 7
1 -> 4
6 -> 1
3 -> 6
0 -> 3
5 -> 0
2 -> 5
break the loop, since index 'from' = stop, so end with
temp -> 2
finished

Now for length 8, with shift 4, and gcd = 4
loop 0
to = size - 1 - loopnr = 7
stop = 3 (= (7 + shift) % 8
7 -> temp
3 -> 7
since 3 = stop break the loop
temp -> 3
loop 1
to = size - 1 - loopnr = 6
stop = 2
6 -> temp
2 -> 6
since 2 = stop, break the loop
temp -> 2
loop 2
to = 5
stop = 1
5 -> temp
1 -> 5
break the loop since 1 = stop
temp -> 1
loop 3
to = 4
stop = 0
4 -> temp
0 -> 4
break the loop
tmp -> 0
finished

Now, if we implement that in code, we get:




2 weeks ago
Well,

if your intention is this:

that is impossible, as Stephan writes, since C is a class ad not a Functional Interface. And implementing a class that implements both A and B is also impossible, as I wrote. But do prove me wrong: do you have a concrete implementation?

Last thing I can think of: what about

Try it!
2 weeks ago
I think a Quantity class is a bit much., though it makes fr a very nice example!

@Stepan
In your first post, what would the 'T' in' Quantity<T>' represent? You mention a Quantity<Apple, Float>, but wouldn't it make sense to have T represent a Unit of some kind, for instance 'kilo', percentage, intrinsic et cetera?
2 weeks ago
I totally underestimated the role of GCD. That made me having to save 'Shift' numbers, instead of just 1. Clever guys, these GeeksForGeeks!

But I never like methods that do a lot of this index juggling. It is often difficult to get it right and makes the code hard to follow. I mentioned a method in the Collections class, that we can use to let Java do all the hard work. The two problems (what are problems?) are that that method works with Lists, and therefore leaves the input array unmodified. But suppose we have two methods: List arrayToList(int[] array' and 'int[] listToArray(List list)' we could have:

And 'shift' can be positive or negative, to detrmine the direction of the shift.
2 weeks ago
I understand your algo, although it is hard to say where it goes wrong. It is too simple, and when you save the rightmost element to the variable temp, you put that temp too early back into the array. Furthermore, your routine behaves reasonably with a shikt of 2, but fails for higher shifts. Lastly, whenever the shift is odd, the code crashes.

So here is the algo in my words (if wrong, let me know). Lets start with an array of length 7, and a shift of 3. For temporary storage, let Integer[] backup = new Integer[input.size]. Fiurthermore, to simplify the code, lets make shift to lie between 0 and size.
We do 3 blocks of shift, equal to the shift parameter (here 3), with blocknumbrs from 0 to 2
Within each block:
    let the index 'to' be equal to: size - 1 - blocknr
    let the index 'from' be equal to 'to - shift'
    save input[to] to backup: backup[(to + shift) % size = input[to]
    now, while (index from > 0) do:
        input[to] = input[from]
        to = from
        from -=k
     end while
next block
then, put the backup into input again
Code:

It should also work with a negative shift, so a rotateLeft.

But what is wrong with this code?
2 weeks ago
Well, you cannot have a class that implements both A and B, since that class would have to implement both 'void test()' and 'int test()', and these methods have the same signature.

What IS possible, is this:
(try it out and see if it answers your question)
2 weeks ago