Campbell Ritchie

Marshal
+ Follow
since Oct 13, 2005
Merit badge: grant badges
Cows and Likes
Cows
Total received
366
In last 30 days
0
Total given
1042
Likes
Total received
8312
Received in last 30 days
23
Total given
684
Given in last 30 days
3
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Campbell Ritchie

That sounds right for selection sort. But remember the end index will be the same as the size of the array.
5 hours ago
Welcome to the Ranch

I think you are confusing yourself with the names of the parameters for that method. You called them minIndex and maxIndex, but they aren't the indices of the minimum or maximum. If you called them startIndex and endIndex, maybe you would find the method easier to understand.
Do you want the two indices to work on the start‑inclusive end‑exclusive convention? If you were at a more advanced stage, you would probably verify that the two indices are within the bounds of the array before starting the method.
Yes, it would be a good idea to call the method findMinIndex() or similar.
7 hours ago

Stan Belen wrote:. . . this is a legacy DTO.

So, suppose it's used in a lot of places. . . .

The idea of encapsulation and loose coupling is that you can change the implementation of the object without affecting how it is perceived by other code via its interface.
Writing a static method and disguising it as an instance method does, of course, make that result so much harder to achieve.
Welcome to the Ranch

That code won't compile outside of JShell.
Please remind yourself what the value of the expression i++ is. Are there any circumstances when it is the same as that of i or different from i?
Go through the different statements with a pencil and paper. Count how many times i is incremented starting with line 8.
11 hours ago
Please don't stat with code. Start with the algorithm you are going to use. What is the technique for filling a spiral matrix?
11 hours ago

Junilu Lacar wrote:. . . Why isn't the design such that we can just write this:

?

Of course: Junilu said it long before I did.

Stan Belen wrote: . . . A Person's address is stored as one of the attributes . . .

That looks like a Map of some sort. Please consider whether you can design such a Map.
Are you taking defensive copies of mutable fields?


  • might be null
  • might not be null but it might not contain an address
  • might not be null and it might contain an address
  • Decide how many attributes are essential, remembering some People lack some attributes, LandLinePhoneNumber being a common one.
    Don't stand for information lacking those essential attributes. I would throw an exception if passed a null.

    . . .. . .

    I am afraid, that looks wrong in two ways. That method should be called on the object and should therefore not need the Person parameter. What you wrote could readily be converted to a static method, which makes me suspicious about it.
    You should also avoid big methods.
    Well done. Please show us the working code.
    1 day ago
    You are still using an if‑else to set up the value to switch on. I think this exercise isn't going to teach you any programming.
    1 day ago

    Maris Lakss wrote:Thanks a lot . . .

    That's a pleasure

    That line is very error‑prone; it is possible to get two double values which should represent the same result, but where the == operator returns false.
    1 day ago
    I like Piet's suggestion, which shows how to use ?:

    Tim Holloway wrote:. . . switch . . . but I think that this sort of range-checking is still beyond it. . . .

    Since each label after case has to be a compile‑time constant or an enum element (Java® Language Specification (=JLS) link), you still can't express ranges, and you can't use a double. You can write. . . but if anybody can think of worse code than that. i would like to see it.

    I hop nobody is trying to teach people that sort of code.
    1 day ago
    A switch requires you to provide cases. Please work out which cases you are going to use; I pesonally think that exercise is pointless and you should is if‑else as being much simpler.
    How you you get the two results of −0.4? Please show us what you tried and didn't work.
    1 day ago
    Welcome to the Ranch

    Please tell us where that question comes from.
    1 day ago