• 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

Modul-oh? (4b strikes back)

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working on attempt 2 for 4b, and Pauline suggested I post my question here. Here's a brief snippet of code:

Not the cleanest code, I know. However, I'm having a hard time figuring out how to apply Pauline's guidance (assuming I correct my own math mistakes ;x):


Hoooey, that is one complicated calculation! I keep getting lost trying to figure out what's going
on.


and


Yikes, here too! I'm wondering if you could simplify things with a little help from modulo.


I've never been a big fan of %, and I'm not sure how I would apply it here to make life easier. Any thoughts would be greatly appreciated.
Thanks!
dav
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So what you've got is equivalent to this. Looks pretty complicated. And you still have another line following it which is even more complex. I think I agree with Pauline.

Modulo is nothing more than "remainder after division." It looks like you're trying to do this manually with all the multiplication, division and subtraction stuff. Try thinking of it in this way: if you have a number "123456" and you want the thousands, you don't care about the "456" so you use divide to truncate the result to "123". On the other hand if you want the "456" and you don't care about the "123" part, you would use modulo.
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So what you've got is equivalent to this. Looks pretty complicated. And you still have another line following it which is even more complex. I think I agree with Pauline.

Modulo is nothing more than "remainder after division." It looks like you're trying to do this manually with all the multiplication, division and subtraction stuff. Try thinking of it in this way: if you have a number "123456" and you want the thousands, you don't care about the "456" so you use divide to truncate the result to "123". On the other hand if you want the "456" and you don't care about the "123" part, you would use modulo.
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what Marilyn said
what Marilyn said
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And just to latch on to one tiny thing you said, blow it way out of proportion, rant a little bit, and possibly inadvertantly make some folks uncomfortable, what does something like
I've never been a big fan of %
mean?
I know we're all entitled to our personal hangups and whatnot, but do you avoid the remainder operator because you don't fully understand it? you don't think other programmers you work with understand it? you just irrationally hate it -- case closed?
I'm reminded of a colleague who used to write SQL select statements with bunches and bunches of ORs:
select ...
from ...
where ( code = 'ABC' or code = 'DEF' or code = 'GHI' or code = 'JKL' )
and date = '25-MAR-2004'

instead of using (the more readable) IN to get the exact same results:
select ...
from ...
where code in ( 'ABC' , 'DEF' , 'GHI' ,'JKL' )
and date = '25-MAR-2004'
because a professor had once told their class that behind the scenes INs just get compiled down into ORs and this colleague of mine figured they'd just save the compiler that extra step.
So where does your deep-seated dislike of % come from?
 
dav mccartney
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. My name is David McCartney, and I'm a modulophobic.
It all started in my CS I class, when I had to figure out the results of a random number on a test.
I froze...Broke into cold sweats...Ran out crying... <sob>... the pain.
Okay... now that I've confessed my mock-confession, here's the true deal: For some reason, I don't like using the modulus. There are points when it seems intuitive to me to use, like 4a, but here, I'm just staring at it, and it's like there's something not fully wired in my brain to make it click as it should.
So, that's the "short" version of the "story". I'll be back at the step 1 support group next week.
[ March 25, 2004: Message edited by: dav mccartney ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David,
Perhaps creating a simple class that takes an input argument like Say does, uses the mod function and prints the result can be a test lab. Experiment with familiar operands and I bet you'll find you are making it harder than it is. At least that was my experience when first being exposed to it and deciding I just didn't get it.
Looking at the snippet of code above, you are already providing the underlying function that mod performs. You figured out the important part, from there mod is shorthand for it.
HTH
Craig
[ March 25, 2004: Message edited by: Craig Sutherland ]
 
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

Originally posted by dav mccartney:
For some reason, I don't like using the modulus. There are points when it seems intuitive to me to use, like 4a, but here, I'm just staring at it, and it's like there's something not fully wired in my brain to make it click as it should.


Like Craig said, you've got the hard part of this assignment figured out. At this point we're going after that oh-so-illusive readable code thing. And that's where using a combo of division and modulo can really help.
Maybe it helps to think of it this way. Let's say I have a number with a bunch of digits and I want to pull out certain parts of it. If I want to pull out parts to the left, I divide. If I want to pull out parts to the right, I use modulo.
Sticking those parts into a variable (whose name tells me what part it is) can help a lot just to keep track of where I am.
Once I get the first parts out, I can continue pulling more parts out of those first parts - dividing to get the stuff on the left, modulo-ing to get the stuff on the right.

Maybe that helps to get some "clicking" happening?
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does Pauline's post about pulling out parts make me think of the Re-Animator?
 
dav mccartney
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I received the following comment:

I'm wondering if you could slim down the method a little by calling parseNumber something like this:
parseNumber( numberIn , oneBillion , " billion " );


After each time I call parseNumber, I print "thousand", etc. as appropriate. While I can understand passing the string in, I'm not sure why I'd pass the oneBillion, which is final and static and declared at the beginning of the class. Also, by changing the signature, am I really enhancing anything that significantlly?
[ April 03, 2004: Message edited by: Jim Yingst ]
 
dav mccartney
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I guess the more I think about it, I don't see why parseNumber would be called as Pauline suggested. Perhaps printPosition where I pass in the number could benefit from a string, but the other doesn't make sense to me
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there Dav (is that pronounced the same way as we pronouce Dave in the UK? or is it (via the Welsh?) Dav (with a short a sound?))
Once you've printed the highest group of numbers you're not interested in them anymore, so remove them from the total and continue outputting the lower groups...think about that at least twice, and then have another think about it (I think Jason was starting to give up on me at that point!).
It took me some time to get the hang of what was required!
Good Luck.
All the best,
Kate!!
 
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


While I can understand passing the string in, I'm not sure why I'd pass the oneBillion [...]



Which steps get carried out several times in your method? Which one of those steps uses a number?

And what Kate said is right on:


Once you've printed the highest group of numbers you're not interested in them anymore, so remove them from the total and continue outputting the lower groups...think about that at least twice, and then have another think about it



I've been wondering how you pronounce "dav" too.
 
dav mccartney
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
:blink:
:rereads Kate's and Pauline's posts x 4
:blink:
Nope. Not clicking. I'll step away from it for a bit I guess. I don't see that I repeat code as suggested. It also seems like I drop the billions position of the numbers if they exist after they're used, and don't reference the number with billions any more in my calculations using divsion or modulus. And still, passing oneBillion, a final static...
As for "dav"... I generally pronounce it "dave". Here in Ohio, you can add a bit of a drawl to it if you'd like... but I try not to.
[ April 03, 2004: Message edited by: dav mccartney ]
 
dav mccartney
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I sought some outside advice... here's what I got (thanks, oh wise instructor...):

... you could probably change it to three [similar] calls, something like
...Usually I prefer that you don't post code here. If you post great code, you will be robbing others of an education...Marilyn
...


Assuming this is the desired approach, which I'm sure it is since he's all instructor-like... it leads me to 2 questions:
1) Is this headed in the right direction?
2) Assuming it is the right direction... will there end up being a method that massages the number as it is passed, especially in the beginning when billions can kill an int? Or should I just plan on using longs for everything?
If this is the right approach, I guess I'm a bit disheartened. This approach wouldn't have occurred to me.... and I don't know if I should be worried about my abilities, or if I should just chalk it up to being a relative greenhorn in the programming realm...
[ April 03, 2004: Message edited by: dav mccartney ]
[ April 03, 2004: Message edited by: Marilyn de Queiroz ]
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Yes

2) Try it and see what you come up with. There are several ways this could be handled. There is not only one "correct" algorithm, but there are some that are better than others. We want to make sure that you end up with one of the better algorithms.

3) "This approach wouldn't have occurred to me.... "
So you're learning something. That's a good thing, I think. If you already knew everything, why would you go through the Cattle Drive and all this pain?
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For all it's worth Dav, just about everybody that approaches the problem the way you did, and gets the feedback that you have, responds the same way.
I know I did. The way that was suggested seem so foreign compared to how I wanted to do things that I wanted to fight it tooth and Lee Press-On nail (mine are usually really short and wouldn't do much damage ). However, I've seen the light my brother, and I am here to tell you that in similar situations at work, using that approach makes life simpler. Plus, it gives me something to debate with other co-workers, and that's always a good thing!
 
dav mccartney
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the post, Jason. I've got finals and final projects for about the next week, so I'm going to step away until I can look at the problem some more. Feels like I need a breath of fresh air
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic