Pedro Esgueira

+ Follow
since Jul 09, 2019
Merit badge: grant badges
For More
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 Pedro Esgueira

Hi Piet, thanks

Of course! The method type needs to match the return type.. *facepalm. I was thinking that the error was in my boolean logic... but its the return type. Got it.
4 years ago
Hi Piet, yes

return (!weekday || vacation)

4 years ago

On codingbat exercise

1) I'm getting the error message incompatible types: boolean cannot be converted to java.lang.String.  I've researched a bit, but I'm not entirely sure why I'm apparently trying to convert a boolean to a String.
2) Also I interpreted they were asking me to code for the following checks:  
- the string not to be empty
- the n to be within a certain range
but their solution has no conditions.

The problem reads:
"Given a non-empty string and an int n, return a new string where the char at index n has been removed. The value of n will be a valid index of a char in the original string (i.e. n will be in the range 0..str.length()-1 inclusive).

missingChar("kitten", 1) → "ktten"
missingChar("kitten", 0) → "itten"
missingChar("kitten", 4) → "kittn"

My solution:

Their solution:

4 years ago
Thanks Jeanne, Brecht

I see now the confusion I was making in my mind. Of course, they are just parameters, I shouldn't want to limit them by "overwritting" their argument value to a specific value.
4 years ago

I'm starting the coding bat problem series on Java

You can find the first exercise in this link
The problem reads:
"The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation. We sleep in if it is not a weekday or we're on vacation. Return true if we sleep in.

sleepIn(false, false) → true
sleepIn(true, false) → false
sleepIn(false, true) → true

My code:

Issue: My code is incorrect (it tells me it is correct for 75% of the cases, not the 100% of the cases). It only becomes correct once I take away the assignments:

Why? Don't we need to initialize these booleans to true? (isn't the default value of a boolean false, otherwise?). And why does my solution not fit 100% of cases?

4 years ago
Hi Brecht, thanks, I know see what you mean with the parameter variables not being the same. For the rest I will digest it a bit more, and eventualy come back to you.

4 years ago
Hi all, and thanks for the feedbak

I've edit my initial post where it says PS to provide the full name and authors of the book, and to give the specific question/answer for exercise 6.

**** ex 6:
hi Campbell: I've noticed the parameters in roar() receive object references passed from main. My question is: those references were defined in main, thus local scope: so how can roar() read them?
hi Brecht: you mention the parameters are not the same: but in that case, if they are not the same and they have not been defined elsewhere, how can the answer to this exercise be "roar roar!!!"?

**** ex 32/33
hi Knute: thanks! ok, so it's simply that .format() is both a method to LocalDateTime and to DateTimeFormatter. Gotchya

4 years ago

***** Exercise 6 *****
(PS: The book name is OCA Oracle Certified Associate JAVA SE 8 Programmer I Study Guide from Jeanne Boyarsky and Scott Selikoff)
(PS: The exact question of the exercise below 6 below is: "What is the result of the following code? //Answer is: roar roar!!!)

I'd expect this code not to work: the variables roar1 and roar2 are defined inside the main() block of code, hence they have local scope.
I know that "smaller contained blocks can reference variables defined in the larges scoped blocks"(page 32), but that's not the case as roar() and main() are at the same level.
Question: How can roar() sucessfully reference to them?

***** Exercises 32/33 *****
On exercises 32/33 we have "inverse" ways of printing the desired formatted date:
I will not print the enire code, as the relevant part for my question is the order in which the exercises use .format, the LocalDateTime f variable, and the DateTimeFormatter d variable.
Exercise 32 codes System.out.print(d.format(f));
Exercise 33 codes System.out.print(f.format(d));

I'd expect the way exercise 33 prints it to be incorrect, but it isnt. I then also decided to print ex 33 they way I expected it to be and it also works. It seems more intuitive we'd apply .format(f) to a date, then the other way around.
Question: Why is interchangeable?

4 years ago
Hi Campbell,

Campbell Ritchie wrote:By the time you reach line 4, the new value of m is visible. That is 8, 7, 6.

Ok, I see, so the post-increment operator becomes visible because the next line is a new expression (so: it's per expression, not per loop, that I need to think about this operator). Thanks
4 years ago

I thought I was understanding post-unary increment/decrements, but apparently not.


My doubt:
I'd understood from the explanation on the book on page 58 that "if the operator is placed after the operand, [...], then the original value of the expression is returned, with operator applied after the value is returned." The examples of page 59 consubstantiate this. Hence, I thought that in the first iteration while (9 > 1)  m would be 9, and 8 or 7 in the second or third loop, respectively.
But I'm wrong, and 've compiled and checked for each loop the value of m to be 8, 7 and 6, respectively.

What am I not grasping about post-unary operators?

4 years ago

Campbell Ritchie wrote:Remember a declaration is type and variable name;...

Hi Campbell and Ivana,

Thank you for your feedback. Simple, I understand now after having run through mazes in my head xD.
I wasn't understanding how we were informing java that x was long in the first variation: I basically forgot that we can have multiple declarations in the same line in the form of type variableA = value, variableB = value.

4 years ago
Hi everyone,

I'm now on page 82 of the OCA Guide, where the authors exemplify tricky loop variations to watch our for for the exam.

the code: (I'm omitting the code within the curly braces, as it's not relevant for my doubt):

Example 3. Redeclaring a Variable in the Initialization Block

My doubt: in both variation we say that int x=0 and x=4, so why do we talk about a duplicate definition in the first variation, but not in the second variation?

(I have somewhat an idea but I'm not sure:
a) I know variables in the initialization block must be of the same type.
b) So, I'm assuming that on the first variation x=4 is interpreted as a long, because of  long y = 0; (?) Is numeric promotion happening here? And if so, I had the wrong idea of numeric promotion: I though it would only happen where there is some operation between the two variables, but it's more general to the expression.
c) So, on the second example by taking long y = 10 outside of the initialization expression, we are allowing x=4 to remain an int, and therefore, not be considered a duplicate declaration but only a reassignment to value 4?)

4 years ago

the code:

The authors start by incrementing the x and returning it to the expression, resulting in

My question: Post-unary operators (expression++, expression--) have precedence over pre-unary operators (++expression, --expression) (page 52). So shouldn't Java first start with x-- resulting in the following

4 years ago
Hi Ganesh, that was it!

Actually, I was coming to update my post, because I'm now on page 60 of the OCA 8 Guide book, and the authors say "An assignment operator is a binary operator that modifies, or assigns, the variable on the left-hand side of the operator, with the result of the value on the right-hand side of the equation."

And then I get it confirmed with your feedback

On line 6 one = two; here = is an assignment operator syntactically right-associative which assign right side value to left side variable

So this is basically how Java "decides" in my ex1). It's just the nature of the assignment operator. Java reads one as a reference variable, and two as the underlying value - the object "b".

Thank you so much for your help!
4 years ago