Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp

Elaine Byrne

Ranch Hand
+ Follow
since Jul 07, 2017
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
7
Received in last 30 days
1
Total given
52
Given in last 30 days
4
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Elaine Byrne

Ah, thanks Ron, I didn't think to look at the documentation for the implemented interfaces (I think inherited methods are sometimes listed in these docs, and I had assumed it woud be the case here too)
I saw this a couple of weeks ago and decided to try it out before reading further - fiddling with it sporadically, I eventually got a not-very-concise solution (of unknown efficiency)  and signed up with HackerRank to submit it, link = https://www.hackerrank.com/challenges/two-characters/submissions/code/113330918 .  I note that I can access Stephan's link when signed in (though I haven't gone through it properly yet to compare with my code).

I thought it would be fun (it was), but it took me embarrassingly long. False start with an approach that first considered character frequencies, and might correlate with some comments I later read on the problem's Discussion page, but where I got bogged down for some reason. Then switched to brute-force checks on all character-pair-combination as per salvin's summary (post #2), but with an initial iterative removal of chars that already had 'runs' in the string; I though the initial process (which may be similar to what Stephan mentions in his first paragraph, but iterative) would be optional, and maybe improve efficiency on average, but removing  it exposed bugs in the ability of the rest of my code to handle all strings, and I gave up trying to untangle them. Finally I replaced the offending logic aspect with something more straightforward and confirmed that it worked with or without the optional initial section (which I didn’t include in my upload).

On a tangent, I didn't realise before seeing salvin's second post (#4 in thread) that there was a "chars()" method in String. I see now it's offered on a string by my IDE, but it doesn't seem to be listed in https://docs.oracle.com/javase/8/docs/api/java/lang/String.html (unless I'm missing something obvious) - that seems odd?
Congratulations, Jeanne!
3 weeks ago
As Adrian says, running the code from learning materials or mock exams and tweaking it to explore any further questions /tangents that might occur to you is a key activity. I usually keep these explorations, including any comments I make about what I discover, and any titbits I pick up looking online to help clarify points about the language, to look back at - it's frightening how I can forget about them after a while. The only other notes I make are the occasional summary or mnemonic diagram to try to clarify or make memorable pesky bits I have mental blocks about remembering. Having said that, I have to go over much of the book(s) I use numerous times to hammer things into my long-term memory - as soon as I finish one section I've at least temporarily forgotten other aspects!

Sabrina Cux wrote:

E Byrne wrote:Just change "numbers[left]" to "left" on Line 19,  "numbers[right]" to "right" on Line 20, analogous to the changes on Lines 21 and 22?


I tried that but, in both cases (pair[0]=numbers[right] or just right) it gives me back a dirty array, like This is my issue.


As Dave implies, this would be the result of printing an array object, i.e. and I don't know where you're doing that. Maybe you could post the latest version of your code? What I meant was that lines 19 and 20 would be:                    
1 month ago

Sabrina Cux wrote:

E Byrne wrote:I was going to suggest then referencing the array returned from the method call and printing the indices that have been put in the 2-element array:

but I realise see the numbers are 0 and 1, rather than the 0 and 2 that I would have expected. (Same as printed from within the method). So I'm not sure if the code is doing what you want. I haven't thought too much about the logic, as I got a bit lost on initial think-through)


Yes, I noticed that too. But I soon found out that was because of on line 10, I removed it and it's working fine now.


Ah, yes - I should have noticed that myself!
1 month ago
I was going to suggest then referencing the array returned from the method call and printing the indices that have been put in the 2-element array:

but I realise see the numbers are 0 and 1, rather than the 0 and 2 that I would have expected. (Same as printed from within the method). So I'm not sure if the code is doing what you want. I haven't thought too much about the logic, as I got a bit lost on initial think-through)
1 month ago
Just change "numbers[left]" to "left" on Line 19,  "numbers[right]" to "right" on Line 20, analogous to the changes on Lines 21 and 22?

PS - A minor comment: You don't actually need to make an object of the class on which to call the method, as it is static, i.e Lines 5 and 6 can be replaced by
1 month ago
Maybe another option is to make a Jar file, which can be run by the JVM on double clicking its icon. Here's what I do in the NetBeans IDE to obtain the file:
1. Click on the Clean And Build icon
2. Open the "dist" folder (same level as the "src" folder in the project folder for the app) and copy the file represented by the java icon (on mouseover it says "Executable Jar fie")
(Then send/paste it wherever you want that has access to the JVM and double click to run)

(+Welcome to the ranch   )
1 month ago
Hi folks - I did the Java OCP 7 to 8 upgrade exam (1Z0-810) today and only got 60 questions (in the usual 150 minutes) cf. the 81 I expected. I was a startled  but provisionally happy  camper and worked away, but mentioned it to the test centre staff when I finished. I confirmed when I got home that the Oracle website still displays it as 81 https://education.oracle.com/upgrade-java-se-7-to-java-se-8-ocp-programmer/pexam_1Z0-810.

In addition, there was a glitch getting me started as the providers could not initially log me into the assigned computer (for 40 minutes!   ) and I'm wondering if there was some additional snafu involved so that I was served an ‘older’ version of the exam, as I think the Q number was reduced to 60 at one point in the past. Or is it possible that Oracle genuinely went down to 60 again and have not updated their website?

If it's the first possibility - an error - I wonder if my cert could be revoked and I'll have to sit the exam again? I wish I could enjoy the relief without this nagging fear!  
Thanks Piet.

Piet Souris wrote:
... A last remark: that Supplier works here is due to the fact that String has a parameterless constructor.


Indeed, another example being ArrayList::new (returns an empty ArrayList), cf. Integer::new does not compile.
Thanks Rob - just for clarification, that line was from the book's answer text which I was quoting per request to supply more details. (In retrospect, perhaps I should have put it in quote tags/format.) I agree with the authors' implication that it might be the most obvious usage, though. I didn't go looking beyond the 2 extra assignments from the question options that I discussed, but your listing indeed demonstrates that are many other possibile functions this method ref could be applied to.
Thanks again.
I think my follow-up speculation re the 1-arg constructor overlaps with your explanation, and I'll sleep    on the stack/return thought