# Benedict Wong

Greenhorn
since Jun 23, 2017
Biography
I'm slowly developing a passion for programming and CS, though I'm still very green.

A self learner, usually through books and online resources.

One thing I respect very much about the community is that there are so many people out there willing to just share their knowledge. So peace to all.
For More
Singapore
Cows and Likes
Cows
0
In last 30 days
0
Total given
0
Likes
2
0
Total given
23
Given in last 30 days
0
Scavenger Hunt
Ranch Hand Scavenger Hunt
Greenhorn Scavenger Hunt

## Recent posts by Benedict Wong

Can I do it this way instead?

Rule 1: A triad of 3 digits.
Rule 2: 3rd number is 3 times of first number; 2nd number is twice that of first number.
Rule 3: Digits 1-9 must all be used without repetition.

First, I'll set the first digit of each number to 3, 6, 9 respectively, since they cannot go any higher in order to adhere to the Rule 1.
3xx
6xx
9xx

Then, for the first number, I'll try permutations of 3 with 1, 2, 4, 5, 7, 8. Multiply by 2 then see if Rule 3 holds. If yes, multiply by 3 and check Rule 3 again. If no, try next permutation.

The reason for this algorithm is because the quantity of numbers that I have to test will then reduce from a couple of hundreds to perhaps a couple of dozen.
6 years ago
Thanks for the explanation, Norm!
6 years ago
In the following code snippet, which is supposed to copy contents from the first file to the second file, something unusual occurred when I made a mistake.

On Line 6, when I accidentally changed args[1] to args[0], the contents of the first file totally disappeared. I expected fin and fout to be copying contents from the first file back to the first file, therefore contents of both files would have remained unchanged. So why did the contents of the first file disappear instead?
6 years ago
You're right, Dave. I'm reading that section in Java SE8. Seems like there are situations when normal try catch is still necessary. No wonder it is essential to learn both approaches.
6 years ago
Thanks Dave, that explains! I will keep your answer in mind when I reach the topic on Databases.
6 years ago
I've stumbled upon something that intrigues me again. I saw some code that actually had try catch blocks in a finally block, something like:

I understand that a finally block is supposed to contain cleanup code that executes regardless of whether any exceptions are thrown. So to have try catch blocks for cleanup code, I'm guessing the cleanup code must contain something rather complex that exceptions could occur there?
6 years ago

Campbell Ritchie wrote: some certification examples use incorrect indentation to confuse the candidates.

I totally concur with you on this, Campbell, and I think I'll do well to learn from the way you pay attention to all the details.

While a set of code might be syntactically correct, it does not mean the presentation conforms to best practices. Moreover, since Java is a free-form language, I think learning to adhere to proper style is all the more important. Something along the lines of Java Style Guide.

So I guess the exams give us an opportunity to train our eyes and learn to see through the "bad form"/booby traps and understand the logic behind.

Cheers!
6 years ago
Hi,

I ran the exact code that you posted and got the same error message.

It's a simple syntax error.

On Line 6 and Line 7, there should have been 2 closing curly braces; on Line 10, only 1 closing curly brace.

Cheers!

6 years ago
Hi Stephan,

Thanks for taking time out to respond to my post again.

I think I have understood the concept so I will try to explain it.

An abstract method requires a subclass to override its superclass, and method overriding takes place during runtime on object instances.

A static method on the other hand, is associated to its class rather than objects - which explains why it doesn't have a this reference. Since static methods aren't associated to any objects, they cannot be overridden.

Likewise, a class has to be instantiated before the abstract modifier can work on its methods, thus the abstract modifier cannot be used on static.

Therefore I could never implement a method that is both static and abstract.

I hope I'm understanding it correctly now. Cheers!
6 years ago

Campbell Ritchie wrote:Please always tell us where such quotes come from. . . . .

Thanks for pointing out my misstep, Campbell! I'll take note of it in my future posts.

Also, thanks for the resource link. I shall slowly digest the information.
6 years ago
Hi everyone,

I came across the following statements which I couldn't wrap my head around.

The abstract modifier can be used only on instance methods. It cannot be applied to static methods or to constructors.

Why is this so?

Could someone please explain to me? Thanks a lot!
6 years ago
Thanks Stephan!

That is why for primitive types, there's no need for new right? So as long as it is a reference type, new must be used whether in a Constructor or elsewhere.

Thanks for clearing my mental block!
6 years ago
Hello everyone, below is a code snippet in which I have a grammatical question.

On line23, I understand that in order to create a physical copy of FailSoftArray class, the new keyword must be used.

However, why is there a need for the new keyword to be used in a Constructor on line16?

Thank you.
6 years ago