tags:

views:

146

answers:

7

Use of if to simulate modulus operator

Rewrite this increment() method from the NumberDisplay() class without using the % modulus operator.

/** Increment the display value by one *  rolling over to zero if the limit is reached  */
public void increment() 
{
   if(value > limit);
   else
   value = (value + 1);
   value = 0;
}

well, i tested this out, i put value = (value + 1); it complied sucessfully, but the error came up as rollover was less then the amount. any help would be great!

+1  A: 

I suggest you read how to properly write an if statement.

Kaleb Brasee
his if statement works just fine... honestly what is it with this forum and 'best practise'? The problem is with his other statements.
Ambrosia
Writing a clear if statement isn't a "best practice", it's "introduction to programming". It may work, but nobody who has a good idea of what they're doing writes an if statement like that.
Kaleb Brasee
A: 

I guess you want

public void increment() 
{
   if(value >= limit)
     value = 0;
   else
     value = value + 1;
}

but it definitely smells like homework...

ammoQ
As I've commented to Ambrosia, given that this does indeed look like homework, I don't think that code without explanation is useful.
Jon Skeet
Jon: if the original poster haven't read or haven't understood TFM, what makes you believe that he will understand our explanations? Sometimes, people just need an example. At least, this is how I learn Tai chi chuan. ;-)
ammoQ
I believe that encouraging the poster to *think* is much more likely to help learn. I'm hoping that answers like mine have encouraged him to take a step back, look at the code he's got, think about what he really wants to do, and have another go.
Jon Skeet
Jon: Not everybody is such a clever guy like you.
ammoQ
@ammoQ: It's not about whether I'm clever or not. It's about whether it's better to explain what's wrong with the current code and try to help the OP think through how to get to the right code themselves, or whether to just give them code which they'll quite possibly cut and paste without learning anything.
Jon Skeet
A: 

I'm not quite sure exactly what your asking or what you mean by "limit" exactly, but I'm guessing (based on how I expect the mod operator to work) that perhaps you're off by one? Perhaps ">=" instead of ">" is what you're looking for?

Brian Knoblauch
+8  A: 

You're always assigning 0 to value, unconditionally. You've also got an empty "if" statement, which isn't a good sign - and hard to spot as you've just used ";" instead of braces.

Here's your current code rewritten with braces:

public void increment() 
{
   if(value > limit)
   {
   }
   else
   {
       value = (value + 1);
   }
   value = 0;
}

Now, rather than show you the code itself, I'll give you two hints:

  • How would value ever be strictly greater than the limit?
  • Why would you not want to change value at all if it's particularly high? What would you want it to become instead?
Jon Skeet
+1 for giving hints and not showing the solution.
Stephan202
And the reason for the downvote?
Jon Skeet
ok so i put greater than ">" limit so ">=" would fix the problem?
a) The braces weren't needed.b) I thought the semi-colon was cute, looked fine to me... did you miss it reading through the first time?c) A critique such as "use if(value <= limit) value++;" would have been better.
Ambrosia
No, it wouldn't - because you'll still be assigning 0 to `value` at the end whatever else happens. I suggest you stop thinking about the code for a moment, and work out what you would do on paper. Describe the steps in natural language (English or whatever your native language is) and then convert that into code.
Jon Skeet
@Ambrosia: a+b) The semi-colon is too easy to miss. I didn't miss it, but then I'm not a relative newbie. I consider it generally good practice to use braces everywhere. They're not *necessary* but they add clarity. c) I don't think that would have helped actually - I'm trying to encourage the OP to take a step back and think for themselves, rather than just giving the answer. What you've suggested isn't a "critique" - it's just saying what to do.
Jon Skeet
And yes, unknown, you are still messing things up with your value = 0 statement.
Ambrosia
What I am saying is that your post sounded critical but didn't deliver on a remedy. Additionally this place (as you should know with your experience here) is a place to get answers to questions, even if it is your homework.
Ambrosia
@Ambrosia - I disagree that the point is just to give answers; it's to give *knowledge*. "Teach a man to fish", and all that. +1 for Jon's solid answer in this regard.
Andrzej Doyle
@Ambrosia: It didn't give an *immediate* remedy - instead, it suggested lines of thought for the OP to follow. I believe that's a better long-term strategy than just posting the working code.
Jon Skeet
@Ambrosia - relaxing on braces may produce legal java code but it's a no-go for me as the resulting code is not clean, hard to maintain and hides implementation errors - as we've seen in the provided solution. And as it was homework - I think 'unknown' wants to learn, otherwise he'd asked SO community to do his homework ;) (and had received a downvote flood)
Andreas_D
+1  A: 

Hint: watch out for semicolons in strange places.

Hint 2: try using { and } with your if and else blocks, this will help you see the logic of the code.

rsp
+2  A: 

Without thinking in terms of code syntax and such, try to read what you put as if it were an English sentence. That should give you some idea, e.g.:

If the value is greater than the limit, then ...

JRL
A: 
public void increment()
{
    value++;
    if(value >= limit)
        value = 0;
}
Ambrosia
-1: How is just giving the answer to homework without any guidance or commentary actually going to help the OP to solve a problem next time?
Jon Skeet
Are you a tutor? The main reason of this place is to get answers.
Ambrosia
I'd like to think I'm a teacher, yes. While the main reason of this place is to get answers, I don't think the main point of homework is to get the right answer with no thought or learning.
Jon Skeet
You might want to look at the various homework-related questions on meta for more opinions about this: http://meta.stackoverflow.com/search?q=homework
Jon Skeet
I have and in particular agree with this comment as many others do.http://meta.stackoverflow.com/questions/10811/homework-on-stackoverflow/10815#10815
Ambrosia
Did you spot the first part of how to answer in the most-upvoted post in that thread though? "Try to give suggestions that will lead the asker in the correct direction rather than providing a ready-made answer."
Jon Skeet
We aren't going to agree on what is best but I just don't see the reason for you down voting my post simply because I down voted yours.
Ambrosia
Andrzej Doyle
@Ambrosia: I didn't downvote your post because you downvoted mine. I did so because it gave just code without a single word of explanation, which I believe will do more harm than good. It encourages the OP to just cut and paste from your answer into his homework, without learning anything.
Jon Skeet