4 Replies - 867 Views - Last Post: 08 October 2012 - 12:50 PM Rate Topic: -----

#1 Sabii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-October 12

Problems with bulding a program about check digits

Posted 05 October 2012 - 05:12 PM

Hello everybody,


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


Is This A Good Question/Topic? 0
  • +

Replies To: Problems with bulding a program about check digits

#2 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 126
  • View blog
  • Posts: 824
  • Joined: 25-October 08

Re: Problems with bulding a program about check digits

Posted 05 October 2012 - 05:58 PM

What code you have posted looks more like Java and less like Javascript.
Are you sure you have posted to the correct forum for the type of help you want?
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3467
  • View blog
  • Posts: 9,951
  • Joined: 08-June 10

Re: Problems with bulding a program about check digits

Posted 06 October 2012 - 12:00 AM

Javascript != Java

moved to Java Forum.
Was This Post Helpful? 0
  • +
  • -

#4 Sabii  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-October 12

Re: Problems with bulding a program about check digits

Posted 06 October 2012 - 03:01 PM

Well, I am new to this forum, so is my question now in the right forum?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8323
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problems with bulding a program about check digits

Posted 08 October 2012 - 12:50 PM

A few weird things in your code

        // why do you pass a String to your method if you don't use it
        public static boolean check (String string){
        int i = 100;
        // you've just set i to 100 so why checking if i >= 10 && i <= 99
        // this firts condition will always be false
        if (i >= 10 && i<=99
        // the second condition will never be tested as the first condition is false
        // but in any case this statement will only be true for 11, 22, 33, ..., 88, 99 
        && (i / 10 !=i % 10)){
        // I don't really see what you try to test
		

        // with an else like that, your method will always return true
        return true;
        } else{
        return true;
        // so it is kind of useless


}
}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1