Joshua Soeng

Greenhorn
+ Follow
since Oct 04, 2018
Joshua likes ...
Java jQuery Python
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
20
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Joshua Soeng

Sorry for my slow reply. Thanks a lot everyone for your help and references, you've been very helpful, thank you.
5 months ago

Campbell Ritchie wrote:I trust you know that particular algorithm is often used as an example of exponential complexity. There are algorithms which run in linear complexity or even logarithmic complexity, but that algorithm runs in approx. 1.608ⁿ complexity. If you try it wih larger arguments, e.g. 30‑40, you will find it becomes inordinately slow and maky even run out of memory. For the most efficient algorithm, find Anne Kaldewaaij's book, about page 98. Kaldewaaij taught Rob.
I was taughtt that there is no such thing as fib(0), and that the sequence starts with fib(1) (=1) and fib(2) (=1). There are other Fibonacci series starting with different numbers, but what you have calculated is what people understand by Fibonacci series not otherwise specified.



Yes, I've read about the downside of recursion, and that we should treat recursion as a last resort, but I'm just curious about recursion.
Thank you for your information Campbell.
5 months ago

Rob Spoor wrote:For n = 5, you don't add (5 - 1) and (5 - 2) (or 4 and 3), you add fibonacci(4) and fibonacci(3). This is the same as (fibonacci(3) + fibonnaci(2)) + (fibonnaci(2) + fibonacci(1)), etc.



Thank you so much Rob.
5 months ago

Rob Spoor wrote:For n = 5, you don't add (5 - 1) and (5 - 2) (or 4 and 3), you add fibonacci(4) and fibonacci(3). This is the same as (fibonacci(3) + fibonnaci(2)) + (fibonnaci(2) + fibonacci(1)), etc.



Ah, so the logic is supposed to be like this?

5 months ago
Hi, I have a question about this Fibonacci Program using Recursion.
I've been comparing several codes for Fibonacci and I'm sure this code snippet is correct, however I'm missing the logic here.

So this is the code snippet:


If I go step by step:

n = 0;
return 0;

n = 1;
return 1;

n = 2;
return(fibonacci(2-1) + fibonacci(2-2)) --> return(fibonacci(1) + fibonacci(0)) = 1;

n = 3;
(3-1) + (3-2) = 3;

n = 4;
(4-1) + (4-2) = 5;

n = 5;
(5-1) + (5-2) = 7;

n = 6;
(6-1) + (6-2) = 9;

n = 7;
(7-1) + (7-2) = 11;

Result should be 0, 1, 1, 2, 3, 5, 8, ...
But why is it all messed up?
Could anyone please explain it to me where did I miss the logic?

Thank you.
5 months ago

Carey Brown wrote:The toString() method is inherited from Object. Object's toString() method returns the hash code for your object by default, which is what you were seeing. So to format the data to something other than the default you must override Object#toString() and write one of you own inside your Mountain class.
This is an example. You may decide to format the returned String differently.



Thank you very much Carey, it's all clear now. I really appreciate your help and explanation.
5 months ago

Carey Brown wrote:Your Mountain class needs a toString() method to format its output.



Thanks Carey, but could you please give me an example how to do that? or maybe there's a good reference?
5 months ago
Hello, I have a question about a lesson in HeadFirstJava book about SortMountains (chapter 16's "Sharpen your pencil").

So here's the code I've got:





The output:





While the book suggest that the output should be like this:





I have no idea where the mistake is. Any help?

Thank you.
5 months ago

Junilu Lacar wrote:Yes, you have a couple of misplaced closing braces. See the fixed code here: https://repl.it/@jlacar/RanchBeTheCompiler



Oh the code's changed a lot from the original in the book.
Thank you very much for your help Junilu, now it's clear.
6 months ago
Hello, I'm learning Java from Head First Java book and I'm currently on chapter 10's "Be The Compiler".
According to the "Solution" in the book, I should get this output:




From this code:



However I keep getting this error message:


It seems like the compiler mistakenly took this part as a method, while it was meant to be a constructor:


Did I miss something here?

Thanks.
6 months ago

Carey Brown wrote:No, those wern't hints. If they didn't make sense then perhaps you should google it.

Alternatively, you could make an even simpler program to experiment with how "break" works and figure it out from that.
Here's an example:
Output:



Thank you so much for this example Carey. I think I understand now.
I hope I do it the right way this time:

For this case:


Here's the step by step I made:
7 months ago

Knute Snortum wrote:

Joshua Soeng wrote:

Knute Snortum wrote:break doesn't mean "go back to the top of the loop," it means "immediately exit this block" which in this case is the for loop.


Yes I know that "break" means immediately exit this block, but then in this case doesn't it also means going back to the top of the loop because the next block should be executed only when the first block has been finished?

Please somebody give me an obvious answer because I've tried some alternatives but none of them assure me that it is the right answer.


It does mean going back to the outer block, but your output notes say "inner = 4 (again)."  That's not correct.  It should be "outer = 1."

And just a note: the way we do things in Beginning Java is to give hints.  This is so you can learn things for yourself.  However, Carey have been speaking plainly the last few posts.  If we're not communicating well, we'll try harder.



Yes, I'm sorry that I took it wrong. It's because I was struggling to understand what Carey meant and it seemed like nothing more than a hint to me, but after the last example I think I understand it now.
Thank you for your help.
7 months ago

Carey Brown wrote:Sounds like you're confusing "break" and "continue". "continue" resets the execution back to the top of the loop, however, it DOES NOT reset the loop variables.



Thanks for your answer again, but really, all you've given me was "hints", and you haven't given me any straight answer.
Please give me a "right" or "wrong", or better yet points out where I did wrong and how can I make it right.
7 months ago

Knute Snortum wrote:break doesn't mean "go back to the top of the loop," it means "immediately exit this block" which in this case is the for loop.


Yes I know that "break" means immediately exit this block, but then in this case doesn't it also means going back to the top of the loop because the next block should be executed only when the first block has been finished?

Please somebody give me an obvious answer because I've tried some alternatives but none of them assure me that it is the right answer.
7 months ago

Carey Brown wrote:Is there any situation where the 'if' would be true? If so, what action would that cause?

The code you supplied only has one print statement but your output apparently comes from multiple print statements. Could you post the actual code you are using?



Yes I understand what you're implying with:


But I just don't know where and when did that one step I missed.
My instinct told me that when the loop hits the break; it should go back one more time from the very beginning before it moves on the next.
So it would run like this:



x = 0; y = 30;
========================================
outer = 0
 inner = 4
   x = 6;
   y = 28;
   // it's broken right here so I go back to the beginning and...
 inner = 4 (again)
   x = 12; y = 26;
 inner = 3
   x = 18; y = 24;
 inner = 2
   x = 24; y = 22;

outer = 1
 inner = 4
   x = 30; y = 20;
 inner = 3
   x = 36; y = 18;
 inner = 2
   x = 42; y = 16;

outer = 2
 inner = 4
   x = 48; y = 14;
 inner = 3
   x = 54; y = 12;
 inner = 2
   x = 60; y = 10;



However I'm not 100% sure that's correct...
Is that correct Carey?
7 months ago