0

I'm taking an AP Computer Science course and have a brief question about the purpose of a particular variable in Java program. Below is my own slight adaption of a certain program in my textbook...

public class Nineteen {
        public static void main(String[] args) {
                int valid = checkNumber(6145);
        }

        public static int checkNumber(int n)
        {
                int d1,d2,d3,checkDigit,nRemaining,rem;
                checkDigit = n % 10;
                nRemaining = n / 10;
                d3 = nRemaining % 10;
                nRemaining /= 10;
                d2 = nRemaining % 10;
                nRemaining /= 10;
                d1 = nRemaining % 10;
                rem = (d1 + d2 + d3) % 7;
                System.out.println("checkNumber executed.");
                if (rem == checkDigit) {
                        System.out.println("Equal");
                }
                return 1;
        }
}

My book says the purpose of nRemaining in this program above is to enhance the readability of the program, not to be used as a left-hand operand for integer division. Can anyone help explain why this is? I'm certain the program can be rewritten in a more condensed way, but is that the most obvious purpose of it?

Eric
  • 99
  • 2
  • 9

1 Answers1

1

In essence, the purpose of additional variables in a context like this is to break down the computation process into smaller more human understandable steps. You could combine these operations into much more dense instruction sets, however then it would be more difficult to understand and, critically, to debug. To that end, a quote...

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian Kernighan

(just in case: https://stackoverflow.com/questions/1103299/help-me-understand-this-brian-kernighan-quote)

Community
  • 1
  • 1
Andrew Ring
  • 3,185
  • 1
  • 23
  • 28
  • I could see how that would be logical now. It just hasn't appealed to me as immediately. If you happen to know: why then, would `nRemaining` not be used for operand division? Thanks for your and @rahul-tripathi 's answer. Yes @iamnotmaynard thanks for pointing that out, on the side. – Eric Oct 24 '13 at 19:21
  • What do you mean when you say "operand division"? Division is a binary operation, and thus any division must have two operands. – Andrew Ring Oct 24 '13 at 19:25
  • Simply division in general. – Eric Nov 06 '13 at 19:42
  • I'm still a bit unclear about your question. nRemaining is part of division operations. The statement nRemaining /= 10; can be rewritten as nRemaining = nRemaining / 10;. The /= is just a shorthand notation to save poor programmer's wrists (slightly). – Andrew Ring Nov 06 '13 at 22:36