• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java-4b technique

 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't it funny how we all seem to go for the loop to solve this problem.
 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Paul Ralph
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right Joel, dumb mistake on my part
Thanks for the correction.
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thats how I actually worked it out, Joel. Turns out I didnt forget everything I learnt at school
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stuart
 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
reply
    Bookmark Topic Watch Topic
  • New Topic