David Cone wrote:I had fun doing the "Barking Dog" exercise with a single if statement. If we were allowed to do it using the ternary operator (which we haven't learned yet) the solution could be done in a single statement:
Yes between my last msg and reading yours I figured out that most boolean methods can just be the word return followed by boolean math. I wrote some beauties last night or the night before.... both I guess. My favorite was the leap year problem. Mostly because it made me think.... I cut down my code several times... I'd be working on a completely different problem and something about writing the current solution would prompt me to realize I had redundant checks in my previous code. I'm enjoying the course so far. I did send in a query to the teacher to see if it's OK to use things we haven't officially been taught in our solutions (like the ternary operator, which is still a valid construct for non-boolean methods that return strings.)Campbell Richie wrote:This (old style guide link) is what you should write:-
I started Java programming 5/21/2021. I learn best by example. I don't ask any questions on here that I haven't tried to solve on my own first.
You don't need to be fully immutable to prevent reassignments of variables. You simply have to mark the declaration with the keyword final. The compiler will complain bitterly if you assign that variable any number of times except once.David Cone wrote:. . . While I have the code. . . . immutable, if I accidentally try and assign a new person to "exRoomy" Java will sound the buzzer. . . .
Quite right Make all variable names, except those with a very short scope, clear to anybody reading the code (at least if they speak the same language as you).1. I have meaningful names . . .
The object itself hasn't got a name; it is the reference that has a name, the name of its variable. Yes, it is a feature that you can't have two variables of the same type in scope simultaneously. (But you can have a field and a local variable with the same name.)2. I can't accidentally reuse an object name. . . .
Both. Careful; you would have to learn the whole of the collections framework (well, the commonly used classes anyway), and that is a big task. Java™ Tutorials section.I do look forward to learning about these HashSets Paul (or you or maybe both) talked about. . . .
Thank you.the word return followed by boolean math. . . . the leap year problem. . . .
Thank you all . . . Peace . . .
Junilu Lacar wrote:...
You'll do yourself and others a huge favor if you write this kind of code instead:
A guy named John Woods once wrote "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” Keep that in mind when you start writing code that others may have to read or maintain.
I started Java programming 5/21/2021. I learn best by example. I don't ask any questions on here that I haven't tried to solve on my own first.
David Cone wrote:Any thoughts?
David Cone wrote:And I will further add, I don't know if I ever would have thought of this.
That is what C books printed in the 1970s and 1980s taught. Another thing was that screens were very small and it was necessary to cram as much code as possible onto the tiny screens available. More recent experience has taught that this approach is completely mistaken.David Cone wrote:. . . using the least CPU cycles vs using the simplest code. . . .
I think you have worked out already, that gets you the smallest prime factor; you want to go from the root downward to find the largest prime factor. Maybe it will be more efficient to have a supply of ready‑made prime numbers, maybe calculated from a Sieve of Eratosthenes. Don't know.. . . factors starting with 2 and working your way up to the square root . . .
If i is a factor of j and i > √j, then i can only be a prime number if j is a prime number. If you haven't found any factors by the time you reach √j, you have got a prime number.if none of the factors above the square root are prime . . .
That is probably so you can pass the code to an automatic checker to test that the result is correct. Receiving a correct result doesn't constitute proof that the code is correctly written. Also insisting on a particular implementation can force you into bad design, e.g. insisting you use a single method.it had to be done in one method. . . .