To do this, I first want to look at the structure of credit card and see what
I'm given already. I'm implementing a method, is valid, which returns a
Boolean. Is valid calculates whether the credit card has a valid number. And
returns true if the number's valid and false if it's not. So now I know how to
write my test program. I need to create two credit cards. I'll create a valid
card. And I have to mark that this is a long, by writing the L at the end. And
I switched two digits to get the invalid card. Now I print out the actual value
of isValid when called on the valid card, and I expect the valid card to be
valid. The actual for the invalid card should be false. Now, it would be easy
to test whether our implementation works. In the credit card class we can see
that the first part of the pseudocode setting sum to 0 and count to 0, is the
same as the code that had before for computing the sum of digits. So I'll keep
that the same. The for each digit starting from the right part is the same. So
I'll try to keep the code for counting the iterations in place. But now,
instead of just adding the digit to the sum, I need to check whether the count
is even or odd and how big the digit is, and use that to decide what to do with
the sum. So, if the count mod 2 is 1, we add the digit to the sum. Else, if the
digit is less than 5. We add twice the digit to the sum. Otherwise, we add
twice the digit to the sum and subtract nine. And now, instead of just printing
the sum the way we did before, we need to check whether the card number is
valid. If the last digit of the sum is zero, the card number is valid. Some mod
10 equals equals 0 is true when the last digit of sum is 0, and false
otherwise. So we return that. For example, if the sum were 24, we would check
sum mod 10 would be 4 which is not 0. So, returning this result would return
false. If sum were 20, then we would check sum mod 10 is 0. So this would be
true, so we would return true, but we can't be sure that this works until we
test it. Let's compile and run the tester, huh. One of our tests failed. It's
saying that the invalid card is valid. We're going to need more information if
we want to debug this. What if I print each digit of the number? So if we run
the credit card tester. That's funny. It printed exactly the same thing. My
print line statement must not have run. For this statement not to run, we would
have to never go into the loop. What's n? If I run it again, I find that n is
this weird negative number. That looks a little bit like the result of an
integer overflow. Clearly, n can't be an int anymore. Now, if I try to compile.
I get a possible loss of precision. So if I want an int digit, I'm going to
have to cast this somehow. What if I try casting n mod 10 this way? Looks like
I didn't remove the cast that was causing trouble. Now, it looks like it's
starting with the correct n. And then taking off one digit at a time correctly.
But it's still getting the wrong answer for the valid card. I'll delete this,
now that I have that part working. And I think I actually want to print out the
digit itself, rather than n. And then I want to get some idea of what I do with
each digit. So I'll print the digit. And then, on the same line. I'll print,
add it to the sum, add twice the digit to the sum, or add twice the digit and
subtract 9. Now, if I run the tester again, I should get much better
information. It looks like the first digit is minus 9, but none of the digits
should be negative. It looks like this cast didn't work properly. Maybe it's
casting n before it takes the mod 10, instead of casting the result of the n
mod 10. Let's see if these parentheses fix it. Now, if I run the main method
again, you can see I'm still not getting what I expected. But I have some good
data here. For 3, I add twice the digit to the sum, 1, I add twice the digit to
the sum. And so on. Let's compare that with the example on the web page. In the
example, the three shouldn't get doubled. The one should though. The next one
shouldn't. The 9 should. In fact, it looks like we're adding twice the digit,
way too often here. It's seems like count mod 2, never equals 1. So let's check
what count is. With System.out.println("count:" + count). Now the main method
says count is always 0. I know how to fix that. I'll increment count. Now, for
the validCard I'm getting true when I expected true. And for the invalid card,
I'm getting false when I expected false. I have just a tiny bit of cleanup left
to do. To remove all of these extra system out print lines, that I added. I
removed all of the print statements, in the credit card class. And now when I
run the test, the output is much cleaner.