Win a copy of Emmy in the Key of Code this week in the General Computing forum!

Pablo Napoli

Ranch Hand
+ Follow
since Jun 28, 2019
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Pablo Napoli

I looked that I took literaly the exercise and what's going on is that I'm not shutting down the service.
Hi everyone!. I have a question about the exercise bellow that i coded of page 346 (jeanne and scott book) because result2 does not finish when i run it and I dont know why, being that the book does not specify it. Just, in order to make the test, I changed MINUTES by SECONDS for result2.

What the book says is: The first task is scheduled 10 seconds in the future, whereas the second task is scheduled 8 minutes in the future.

Hi Tim. Thank you so much for your reply. It was really interesting. If I should take some points about you said, I'd chose these:
* if you can recover, it's better to catch the exception, report it, repair it, and go on.

* Exactly where to deal with an exception is often the challenge.

* The best place to catch something is the highest level you can do something about it.

Now I'm studying the chapter Exceptions and assertions of Ocp study guide and in page 291 about using multi-catch it says: "It is common to log the error and convert it to a different exception type". And in the example it converts an Exception to a RuntimeException:
} catch(Exception e){
throw new RuntimeException(e);

Wouldn't it be a bad practice?. I mean, If i have an Exception is because it's about an important matter and as you said, I want to obligate the caller to deal with this issue. So making this conversion who is gonna call this method might not notice it until the exception happens. It's just my point of view according to what we were talking about.

Thanks mate!
Hi guys. I'm preparing my ocp certification (that also takes Exceptions topic) but I think is better to make the question here because it's more related with the basis of the topic.

I will try to explain myself with an example that I was thinking: It's common to use IlegalArgumentException in setter methods. So we can have a code snippet like the following:

So up to this point I figure, what if there is another programmer that will invoke setAge() method and cannot see the method's implementation with the IllegalArgumentException?. I mean, thinking in design, it would be better not to use any RuntimeException or turn it to a checked exception because what's the meaning of throwing an exception if you are not obligated to deal with that. In this case is not good ending the program just because someone made the mistake of passing an incorrect age. I'd prefer show a message to the user letting him/her know about what's going on. And to achieve that If I throw a checked exception I'm saying to the other programmer: "If this case happens, you have to let the user knows".

I'd like to know your opinion because I'm a bit confuse with this case. Anyway, if I'm right, what's the real meaning of unchecked exceptions?.

Yes, I got "black" too and I'm compiling with Java8. Maybe it does not appear on the website cause still nobody found this errata. But also is apparently wrong exercise 31.
Hi Ritchie. Yes, for both exercises, 31 and 32.

Hi guys. My answer was option B but in the book says D, Whereby I coded it and see that I was right. I will attach the diagram from question 31 where this exercise is based.

32. Given the snippets of resource bundles in question 31 from when we compiled the application, what is the result of the following?

Locale.setDefault(new Locale("en"));
ResourceBundle rb = ResourceBundle.getBundle("Buggy");

A. null
B. black
C. white
D. The code throws an exception at runtime.

Book's answer: D. Since no locale is specified, the code tries to find a bundle matching the default locale. Two resource bundles match the language English. The Java class one is used since it is present. However, it does not contain a key color , nor does its parent. Java does not allow looking in a properties file resource bundle once it has matched a Java
class resource bundle. Therefore, it throws a MissingResourceBundleException , and Option D is the answer.

Right answer: Option B.
Hi guys. This is a errata of the book OCA/OCP JAVA SE8 Programmer Practice Test by Jeanne and Scott.

Answer's book:
B. Since no locale is specified, the code tries to find a bundle matching the default locale. While none of the resource bundles match English United States, two do match the language English. The Java class one is used since it is present. Since the Java resource bundle for English doesn’t have a key wheels , we go up to the parent resource bundle. The default Java resource bundle does have the key wheels with the value 4 , so Option B is correct.

Right answer: Option C Because by not matching with for not having found the key "wheels", it will continue with (default locale is en_US) so in this file it can find that key and retrieves its value.

I checked it compiling it.
Hi everyone and sorry for the late reply. Yes, I was wrong with the code that I posted. The line that I wanted to show you should be: "public static <T> void make(List<? extends T> list){". So again I'm gonna paste the code below.

Error message: error: incompatible types: CAP#1 cannot be converted to Number
       for(Number n : list){
 where T is a type-variable:
   T extends Object declared in method <T>make(List<? extends T>)
 where CAP#1 is a fresh type-variable:
   CAP#1 extends T from capture of ? extends T
1 error

So what I want to say is:
1-  Test2.<Number>make(l); : I'm indicating that T should be taken as Number.

2- public static <T> void make(List<? extends T> list){ : As T is taken as Number, I could translate it as: public static <Number> void make(List<? extends Number> list){

3 for(Number n : list){ : I don't understand why it does not compile because I'm looping a List referring each element with a Number type.

So this is my question and I tried to be more clear than before.

Thanks again to all those who replied my post.
Hi guys. I'm reviewing my summaries from the chapter of generics of OCP Study Guide by Jeanne and Scott and pracicing some exercices I came up with the exercise below that I can't totally grasp why doesn't it compile.

The error message is:
error: incompatible types: CAP#1 cannot be converted to Number
for(Number n : list){
 where T is a type-variable:
   T extends Object declared in method <T>make(List<? extends T>)
 where CAP#1 is a fresh type-variable:
   CAP#1 extends T from capture of ? extends T

It's weird for me because here Test.<Number>make(l); I'm saying that T shoud be taken as Number and then here make(List<? extends T>) I say that the wildcard should extend Number so I don't know what's wrong with this for(Number n : list){.

If I change make(List<? extends T> by make(List<? extends Number> it does compile but in regards what I can "read" of the code at first it should compile.

Please, can someone show me what I'm not finding out?. Thanks as always!
Thanks guys for your replies!. They were very useful for me to understand that there is no a logical cause to this happens but it is rather a "design resolution". Anyway it could be something very tricky if OCP designers test try to play with this.

Anyway, I like to add my opnion. I think Java is not such a clear language about conversions between different types. I expose my thought because I'd like to know your opinion (you guys are like jedi masters for me) so if I'm wrong I could change it. But this is not the only case tht I've seen.
For instance, it's a tricky example that I could never clarify:

In this code it's possible getting a Double by the addition of n+d and then convert it to int.

But here I can't make the convertion:

For sure we can say that in the fist one we made an addition but anyway I think it's not clear because for different cases I cannot cast from a wrapper to a primitive.
Hi guys. I'm studying with the OCP Practice test book and from chapter 14 ex.30 I got a question related to DoubleToIntFunction.

Because I don't understand how is possible that a lambda implementing that functional interface can receives either Double or double (while the signature is applyAsInt​(double value) ) whereas with other functional interfaces you should to respect the input parameters from the lambda to match with the signature of the interface. For example:

On the other hand with DoubleToIntFunction:

Great!!!. For people that never saw all this stuff before I think it's a bit hard, because it's like a change of paradigm, where you should focus more on what to do so rather than how it do it, but the book explains it very clear. Just I was stuck because I haven't had in mind the theory that I read at all.

Stream.generate( () -> "Thank you!").forEach(System.out::println);
Hi guys. I'm studying chapter 4 of ocp Study Guide by Jeanne and Scott and inspired in one exercise of page 201 I did another one to understand more how peek() works but I can't understand what's going on.

First of all I want to say that I noticed it by chance that if I add count() method at the end, the stream works as I expect, but my question is why?.

As you can see, now the output is [alex, anna, toby] but if I remove count() method, the result is [], so neither element is added to the list.

Thank you guys, I'm really breaking my head trying to solve it but still nothing.
Hi guys. This time I wanted to ask you about if currently developers are using most of the stuff from Java 8 like lambdas and streams. I was developing before (like almost 4 years ago) for a company and we didn't use even Java 8. So still I'm not developing again (I wanto to pass the ocp exam before) so I'd like to know for you all if in your projects you are using all of these "new" features.

For me it's being a bit hard to assimilate all of these things because it's like a change of paradigm but It's really interesting anyway.