My assignment sounds like this:
Check-digits are used to check whether a number is correctly entered by a user (for example a bank account number or CPR-number). By using check digits many but not all errors can be found. The check digit is added the original number such that the test can be easily performed. We describe an algorithm below. The number is given as string and the check digit is already added.
Let xn-1xn-2 ….. x2x1x0, be an n-digit decimal number, that is xi ; (0,1; 2; 3; 4; 5; 6; 7; 8; 9), for all i. The algorithm replaces every digit with an odd index by twice the digit if that
is less then 10. If twice the digit is 10 or larger then take the modulus with respect to
10 and add 1. That is, 2 is replaced 4, 7 by 5 = (14 mod 10) + 1. Then all the digits
of the resulting number are summed up, i.e. the cross sum is computed. If that sum
has remainder 0 modulo 10 the number is accepted, otherwise not. The digits with an
even index are not changed.
Write a class NumberCheck which contains a method:
public static boolean check (String number )
which returns true if the number is accepted and false otherwise. Use exactly the
names speci_ed above for the class and method.
For example, the number 3475 becomes 6455 after replacement of the odd-indexed digits
and is accepted by the program because the cross sum of 6455 is 20 and 0 = 20 mod 10.
The number 41032 is rejected.
You may assume that the string number only contains decimal digits; you do not have
to check that.
On Campus net you _nd a Java-_le NumberCheckTest.java. Put this into the same
directory as your _le NumberCheck.java. Then execute NumberCheckTest.java and
it will perform a test on your implementation.
Hint: Do not try to do everything at the same time. Break the problem down into
smaller tasks. For example: how to get the digits of the string the desired order, how
to turn a character into a numerical value, etc.
Untill now I have written this code and want help making it better:
package aflop01_12;
public class NumberCheck {
public static boolean check (String string){
int i = 100;
if (i >= 10 && i<=99 && (i / 10 !=i % 10)){
return true;
} else{
return true;
}
}
}
thanks for helping
This post has been edited by Dormilich: 05 October 2012 - 11:58 PM
Reason for edit:: fixed code tags

New Topic/Question
Reply



MultiQuote




|