Piet Souris

Bartender
+ Follow
since Mar 08, 2009
Forum Moderator
Netherlands
Cows and Likes
Cows
Total received
186
In last 30 days
1
Total given
130
Likes
Total received
1068
Received in last 30 days
9
Total given
150
Given in last 30 days
2
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Piet Souris

hi Du,

You are right, overloading in answer B is correct. I just tried:

outcome:
in Foo
in Bar


Edit: I just noticed that Jeanne, in her reply, mentions that B is not a correct override (which is correct). But the exercise mentions overload in B.

Tim Holloway wrote:Fortunately, we have lambdas these days, so anonymous inner classes are no longer essential.


They never were, you could always write an outer class and instantiate it.

And I use them regularly. For instance:
I do not have that book, so can't say what the book has in mind, but List::stream  does not take any parameters. Therefore, this is legal:

Perhaps this topic is helpful here: question of parameters
For example: the method 'Stream.of' does take a parameter, so this is possible:

Anyway: I can't think of any case that would make 'flatMap(List::stream)' work.
hi Hanna,

well, you must add '101' to get 0.
2 days ago
Another possibility is to copy your image, draw your sprites in this copy and draw this copy in your panel, calculating from the panels size where that copy should be drawn. You could even keep your bordersize unchanged, thereby scaling your image. All this can be done in a backgroundtask, if necessary.
3 days ago
That lock- button is right below the post reply-button. Not the first time I locked a topic.

Perhaps there are no OCAJP people anymore... but just in case there is one left and reading this topic: what if we want to keep the 'toMap' method?
3 days ago
What is that last code supposed to do? If I run it on the array [4, 3, 2, 1] I get an AIOOB-exception, and if I run it on [-4, 3, 2, 1] we get [ 1, 16, 0, 0].
4 days ago

Stephan van Hulst wrote:I'd use the same solution as Piet, just slightly differently:


That is a fine generic solution, but it works only when the values are unique. We had that in Jeannes case, but that will not be true in general. So, the result should be something like:

For the OCAJP people among us: how must Stephans code be adjusted?
4 days ago
You can also say that a flatMap maps an element to a stream of other elements. So it forms a sort of 1-to-many-relation. Suppose we want to form the pairs (1, 1), (1, 2), ..., (1, 5), (2, 2), ..., (2, 5), ... (5, 5).
We can say that if we have a, say, 3, then we want to map this 3 to the pairs (3, 3), (3, 4) and (3, 5). That can be done with:

with

Now, we do not only want to do this for the value of 3, but also for the values 1 up to 5. For each of those numbers, we map that number to the stream above. Since we map each of the elements 1, ..., 5 to a stream of P's, we need a flatMap. So we get:
4 days ago
Shorter is

return nextInt(prompt, 0, Integer.MAX_VALUE);

see Campbells earlier method
5 days ago
hi Liliane,

pleasure is all mine!

Don't despair! As I said, when dealing with method references, I always imagine what lanmbda would do the same, and lambda's have the big advantage that it makes clear what parameters are involved.

But even so, I remember, when doing the OCAJP, that I struggled for a long time with exactly that same question. It took me quite some time (months) before the whole matter became clear to me.
Yes. Here is another example:

suppose I have a class PietsProbeersels. (I do, and in this class I try out many things). And suppose I have a static method:

Now, I can do two things in my code:

Now, the first one is okay. PietsProbeersels.reverseOrder DOES return a Comparator<Integer>. However, rhe second one gives me this error in NetBeans:

And if you interprete the second form as:

then it is clear why we get that error-message.  
hi Liliane,

this is how I try to remember this stuff:

everytime I see a method-reference, I think of the corresponding lambda. So, say we have String::length, then I think of the lambda:

And if we have a Comparator comp, then Comparator::reverseOrder would be for me:

Now, this fails for two reasons: our Comparator 'comp' does not have a method 'reverseOrder', and even if it did, 'reverseOrder' does not take any parameter.

What about Comparator::reversed? That would be in lambda-speak:

Again this fails, since although the method 'reversed' exists, it does not take any parameter. What would work is:

but you might as well leave out this '::compare'.

If we think of the method 'charAt' of the String class, then we can say that string.charAt is a method that takes two parameters: a String s and an int (or Integer) t, and so we can write:

Suppose that we have a String s. Then s::charAt is a Function of only one parameter (the int or Integer t), and so we can write:

Writing

would fail. Why?

Well, as said, that is how I remember the stuff. It IS nevertheless confusing.
hi Fred,
welcome to the Ranch and enjoy the stay!

First thing that comes to mind is the Taylor series: sin(x) = x - x^3/3! + x^5 / 5! ...., that you can calculate for some terms. How many terms: you have to experiment to find what accuracy you want.

Another one, for 0 <= x <= PI:  sin(x) = 4*x * (PI - x) / (PI ^2) (that's a quadratic approach. You set sin(x) = a * x^2 + b * x + c, and then calculate a, b and c for the points x = 0, x = PI / 2 and x = PI). If that is not good enough, then try a cubic approach.
5 days ago