This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Cattle Drive and the fly likes Java-4b technique Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Java-4b technique" Watch "Java-4b technique" New topic
Author

Java-4b technique

Paul Ralph
Ranch Hand

Joined: Aug 10, 2000
Posts: 312
My design in 4b was to chop the large number into three-digit segments. These segments would then be stored in an array. The array would be looped through to print each segment.
When I created an array I got the following nitpick:
> int digitArray[] = new int[4];

Why use an int array? It seems that if you store everything in one int, it will still be easy to
get the information out.

My view is that you could use a long (because the number could be 12 digits long), but defeats my whole purpose.
Later, as I try to loop through the array...

> //loop through the array and...
> for ( i = 3; i >= 0; i--)

Whenever I have a loop that is this short, I often think that the program would be simpler without
a loop at all ....

My loop contained 12 lines (including comments) between the braces. Would it have been better to repeat that code 4 times? Surely I'm missing something here...
Most of all the combination of these two comments suggests that my algorithmic approach is bad, but this was not addressed. Should I rewrite my program? I had another way of doing it, I'll see if I deleted it or not.
Thanks for your help
Paul R
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Paul I think your design is correct.
However rather break the number up in chuncks and use that specific chunck in a single integer. As for the nitpick on the loop, I got that one too , how about calling a method 3 times
Good luck
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Hi Paul,
I went the same way with a loop and an array and got picked for it too.Then I made a method that coded for each case... you guessed it, picked again, this time for repeating code.
"Would it have been better to repeat that code 4 times?"
It seemed to actually help to write out the repeating code like you said and look at it to see where a method could come out of it. Then, like Johannes suggested, I'll call it several times. I'm working on that right now: attempt number 5 (out of ? ).
Not sure that helps, hang in there,
Pauline
p.s. maybe there should be a special in-between graduation for the survivors of Say... or maybe a free drink at Johannes' bar? This is thirsty work.
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
Isn't it funny how we all seem to go for the loop to solve this problem.
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
I did the loop as well. The loop just seemed to make the code more concise IMO. I am still working on it without the loop so I'm not sure how I want to go with it.
Matthew Phillips

Matthew Phillips
Paul Ralph
Ranch Hand

Joined: Aug 10, 2000
Posts: 312
Thanks, everyone. Your comments really helped.
Now for part 2: Other than to be so told so by someone who has already been there, how should I have recognized there was a better solution? (I hope that makes sense)
Paul R
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Experience, Paul, experience, and I'm not being funny here.
The old KISS principle count's very strong. I also got nitpicked for using the loop. But I know for myself if I had to fix a bug in that class (Say) at 3 in the morning. I would have prefered the call of the method above the loop.
Remember one thing too. The solutions evolved over time, Paul admitted as much, dont expect to produce "perfect" code from the beginning.
Good luck
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Excellent points Johannes. I'd like to take up your mention of fixing bugs at 3AM.
Something that helps me a lot, but that I don't always manage to do, is to try and give up after a reasonable hour in the evening!
My tendency is to attack a nitpicked assignment as soon as I get it, which is usually at home after a full work day. It's so much fun to try to figure it out that I just keep at it... but when I get tired I get stupid, period.
Those times when I stay up too long and insist on sending in an attempt in spite of fatigue usually result in a bunch of silly spacing and / or logic errors. Arrgh.
I'm trying to maintain better "study habits", juggling various activities remain a challenge.
How do the rest of you do it?
Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
Does this mean we're all a bit "loopy"?
I've just finished going through the exact same excercise, and at first I had the same reaction. When I finally got over it (i.e. got closer to the final solution), I started to realize that I need to think differently for Java. The lesson to be learned here is there are lots of ways to accomplish something!
BTW, my first attempt at Sayb had 137 lines, 2 loops, a global variable, and a bunch of other stuff. My final attempt has only 94 lines, no loops, and not a single string concatenation, so you can teach an old dog new tricks!
------------------
I'm a soldier in the NetScape Wars...
Joel


Wait a minute, I'm trying to think of something clever to say...<p>Joel
Michael Pearson
Ranch Hand

Joined: Mar 11, 2001
Posts: 351
I definitely agree with you Joel.
There's a different thought process to come up with the Cattle Drive solution for 4b.
My procedural experience with C, Pascal, and Assembly are working against me!
Good luck to the other 4b hopefuls!
Michael
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
I think I'm starting to understand why calling a method more than once is preferred over the loop approach, mostly from what you said, Johannes, about the KISS principle being what's really important here.
Is it safe to generalize as follows:
it's better to write a REALLY simple method that does ONE thing and then call it several times passing different parameters
?
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
add if the number of repetitions is not to high
I'd hate to find list of more than 5 calls to a method in a section of code.
ps is it is or are to high
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
thanks jdj, that makes sense (my loop ran only 4 times).

Pauline
ps it would be "...if the number of repetitions are not too high."
Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
Originally posted by Pauline McNamara:
thanks jdj, that makes sense (my loop ran only 4 times).

Pauline
ps it would be "...if the number of repetitions are not too high."

If "repetitions" were the item being referred to, then "are" would apply since it would be plural. But, "the number of repetitions" is a singular value since you are actually refering to "the number" and not "repetitions", so "is" would be more grammatically appropriate.

------------------
I'm a soldier in the NetScape Wars...
Joel
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
You're right Joel, dumb mistake on my part
Thanks for the correction.
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Hey thats how I actually worked it out, Joel. Turns out I didnt forget everything I learnt at school
Stuart Goss
Ranch Hand

Joined: Mar 21, 2001
Posts: 171
Hi all!
Before I read this thread and before I sent off my first attempt of Say 4b, I had broken the number up into three digit numbers and saved them in an array....
Hmmm, just like Paul at the top. And it seems to me, just like most of us. I'm really glad to see that we all make the same mistakes in the beginning.
I haven't found the solution here, but I learnt that there is no cause in using a loop that would only loop a few times.
Ah! And by the way: congratulations JdJ on the new responsibilities
Stuart
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Thanks Stuart
Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301

And my Dad said being an English Major would never pay off! (That's why I got a Philosophy Major as well...)
G.I.Joel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java-4b technique
 
Similar Threads
Say 4b just got harder...
Java-4b
4b structure
Java-4b Please
Java 4b technique, pt 2.