Win a copy of Learning Regular Expressions this week in the General Computing forum!

Junilu Lacar

Sheriff
+ Follow
since Feb 26, 2001
Junilu likes ...
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
Forum Moderator
Junilu Lacar currently moderates these forums:
Columbus OH
Cows and Likes
Cows
Total received
201
In last 30 days
2
Total given
73
Likes
Total received
1748
Received in last 30 days
30
Total given
315
Given in last 30 days
2
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Junilu Lacar

Newer versions of the JDK include the JShell REPL. Just fire it up and paste your code snippet. As long as the code snippets can actually be compiled, JShell will do the necessary "wrapping" so you don't even have to worry about writing a main() method.

Here's what my session looked like on the command line (I use a Mac but it's going to be pretty much the same if you're on Windows):
7 hours ago
I would tighten up that code like so:

The comment on line 18 can be eliminated by extracting that statement to another private method but I'd be fine with leaving the comment: It explains *why* that statement is there.
10 hours ago
The JLS has more information about Class objects for Arrays. Some interesting facts there, actually.
1 day ago
Also, "Anonymous cast" is not A Thing™ in Java, as far as I know. In fact, now that I think about it, it's a contradiction in terms. If you're going to cast something, you have to know exactly what you're going to cast it to. So yeah, I'm pretty sure now that "anonymous cast" is not a thing in Java.
1 day ago
The expression ((Dog) new Hound()) is an object reference expression, i.e., an expression that evaluates to an object reference. ((Dog) new Hound()).bark() is a method invocation expression.  A method invocation expression uses an explicit reference or an implied reference (this). What you have used so far prior to seeing this example is an object reference expression that uses a variable reference. That's a very common way to have an object reference but it's not only way, as you have seen.

It's the same thing as with math and algebraic expressions:

Let x = 7
(x + 5) = 12  (this is an algebraic equation that uses the variable "x")

((14 / 2) + 5) = 12 (this is a mathematical equation that is equivalent to the above)

In the second example, you just calculate the value in line, without using the variable x. Kind of the same thing that's happening in your code example.
1 day ago
@OP: it's very difficult to understand your problem if you don't give us your actual code and use "abstract" names instead, like "classname" and "subclass" and "parameterone" and "NotWorkingFunction".

Please post relevant snippets of your *actual* code, so we can understand your problem better. Don't make us work too hard to understand your problem. We are all volunteers here, which means we don't get paid to answer questions. We do this because we want to and we like doing it. If you don't help us and make us work too hard to try to understand your problem, then the motivation to help is greatly reduced.
1 day ago

Randy Maddocks wrote:

Piet Souris wrote:the middle statement of the if() is the so called 'continuation' condition


Oracle refers to the middle as the termination expression - go figure.  


The tutorial refers to it as the "termination expression". However, the JLS simply refers to it as "Expression" - https://docs.oracle.com/javase/specs/jls/se10/html/jls-14.html#jls-BasicForStatement

The important thing to remember about this expression, regardless of how it is referred to, is that it must evaluate to true in order for the loop body to be executed. If it evaluates to false, then the loop terminates. I don't know why the tutorial refers to it as the "termination expression" since it does seem more intuitive to say it is the "continuation expression" given the way it behaves.
1 day ago
Check out the official tutorial on the for-statement: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

The second part of the for-statement header is a boolean expression, also referred to as the "termination expression" in the tutorial. If this expression evaluates to true then the loop body will be executed. The expression is first evaluated after the loop initialization and subsequently, after the loop increment.

It appears part of your confusion has to do with the relational operator "<=" which is the "less than or equal to" operator. If you wanted "greater than or equal to" then use ">=" instead.
1 day ago
No problem, Michael.

Google has come a long way. Anymore, it seems like it's just a matter of knowing how to ask the question that will get Google to find all the relevant answers for you.

It even knows how to answer the ultimate geek question: What is the answer to life, the universe, and everything?
1 day ago

Stephan van Hulst wrote:I'd focus on solving the problem before making optimizations.


I'm with you on premature optimization but to me, that's a logic thing. When searching for prime factors of N, you stop/start looking at N/2. Same thing. I don't consider understanding the domain of your solution as "optimization".
1 day ago
Try searching for using relative resource paths in Java

Also, ItDoesntWorkIsUseless (←click that link to learn why and what you can do instead)
1 day ago

Nick Hulse wrote:Write a method largestDivisor ... what I have so far:


How is printing the contents of an array going to help solve the problem? Try thinking through your approach first and work out an algorithm by hand. Once you have an idea of how you'd do things yourself, it's easier to translate that into instructions for the computer to follow.
1 day ago
"counting down from the argument"
"counting down from one less than the argument"

You might want to consider whether or not the largest divisor of any number is ever going to be more than half of it.
1 day ago
Growth mindset: The more dumb mistakes I make, the more I learn. The more I learn, the better I get. You're looking at a guy who can make a lot of dumb mistakes.

Fixed mindset: That was so obvious, any idiot can see that. I'm worse than an idiot.

Lose your ego and practice egoless programming to develop a growth mindset
2 days ago
It's an old technique and I hardly see anyone using CRC cards anymore but Class-Responsibility-Collaborator is a great way to figure out what kind of object reference fields you'll need in your class. Kent Beck and other XPers were taught this technique by Rebecca Wirfs-Brock and I think it's a technique worth having in your toolbox.
3 days ago