7 Replies - 2544 Views - Last Post: 03 May 2012 - 11:47 AM Rate Topic: -----

#1 MrsQ  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 29
  • Joined: 20-September 08

Luhn Algorithm - problem getting sum of double every 2nd digit

Posted 02 May 2012 - 07:31 PM

Hi,

I am working on a Luhn Algorithm problem and am having trouble getting the sumDoubleSecondDigits() code to work properly. The credit card number that I am passing in is 4388576018402626. The intSum returned should = 37. I am able to get the odd number method to work, just not this one. Here are the directions that go along with the method:

1. Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.
2. Add up all the single digit numbers from step 1
3. Add up all digits in odd places from right to left in the card number.
4. Sum the results from Step 2 and Step 3
5. Divide the result from Step 4 by 10 and if there is no remainder than the number is valid. If there is a remainder the number is invalid.

Any help would be greatly appreciated!

	//method to reduce numbers to a single digit
	private int reduceNumber(int iNumberIn)
	{
		int intDigit = 0;

		while(iNumberIn > 9)

		{
			// Get the right most digit:
			 intDigit += iNumberIn % 10;

			 // Remove the rightmost digit you just extracted:
			 iNumberIn /= 10;

		}  // end while()

		return intDigit;
	}//end reduceNumber method

	//method to sum up double every second digit from right to left
	private int sumDoubleSecondDigits(long lNumIn)
	{
		int intTempNum;
		int intSum = 0;

		while(lNumIn > 0)
		{
			intTempNum = 0;

			// Remove the rightmost digit:
			 lNumIn /= 10;

			// Get the right most digit:
			 intTempNum = (int)lNumIn % 10;

			 //Multiply the digit by 2
			 intTempNum *= 2;

			 reduceNumber(intTempNum);

			 // Remove the rightmost digit:
			 lNumIn /= 10;

			 intSum += intTempNum;

		}  // end while()

		return intSum;
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Luhn Algorithm - problem getting sum of double every 2nd digit

#2 MrsQ  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 29
  • Joined: 20-September 08

Re: Luhn Algorithm - problem getting sum of double every 2nd digit

Posted 02 May 2012 - 07:51 PM

Okay, I just found one error, but I'm still not getting the right sum.
Line 39 should read

intTempNum = reduceNumber(intTempNum)

but even with that change, I still get the wrong total.
Was This Post Helpful? 0
  • +
  • -

#3 MrsQ  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 29
  • Joined: 20-September 08

Re: Luhn Algorithm - problem getting sum of double every 2nd digit

Posted 02 May 2012 - 08:22 PM

I found the problem and it works properly now! Here is my revised code that made it work. Thank you all for taking time to look!

//method to reduce numbers to a single digit
	private int reduceNumber(int iNumberIn)
	{
		int intDigit = 0;

		while(iNumberIn > 9)

		{
			// Get the right most digit:
			intDigit += iNumberIn % 10;

			// Remove the rightmost digit you just extracted:
			iNumberIn /= 10;

			//Add the two digits together // I left out this important step
			intDigit += iNumberIn;
		}// end while()

		return intDigit;
	}//end reduceNumber method

	//method to sum up double every second digit from right to left
	private int sumDoubleSecondDigits(long lNumIn)
	{
		int intTempNum;
		int intSum = 0;

		while(lNumIn > 0)
		{
			intTempNum = 0;

			// Remove the rightmost digit:
			 lNumIn /= 10;

			// Get the right most digit:
			 intTempNum += lNumIn % 10;

			 //Multiply the digit by 2
			 intTempNum *= 2;

			 if(intTempNum > 9)   //I needed to add an if statement here
			 {
			 intTempNum = reduceNumber(intTempNum);
			 }
			 // Remove the rightmost digit:
			 lNumIn /= 10;

			 intSum += intTempNum;

		}  // end while()

		return intSum;
	}


Was This Post Helpful? 2
  • +
  • -

#4 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Luhn Algorithm - problem getting sum of double every 2nd digit

Posted 02 May 2012 - 08:25 PM

Heh... nice job!
Was This Post Helpful? 0
  • +
  • -

#5 MrsQ  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 29
  • Joined: 20-September 08

Re: Luhn Algorithm - problem getting sum of double every 2nd digit

Posted 02 May 2012 - 08:42 PM

Thanks! ... Now on to the next problem I'm having...

I need to

o Code a method called: public boolean isValid(long lCreditCardIn ) that will validate the Credit Card that is passed to it.
o Code a method called: public boolean isValid() that will validate the Credit Card that was passed to the constructor that accepted the Credit Card number.
o HINT: Donít put the credit card validation logic into both of the overloaded isValid methods. How can you share the code?

Here is what I have. As you can see, I'm at a loss for what the instructor means for the second method...

Again, any help would be greatly appreciated!

	//Method that will validate the credit card that is passed to it
	public boolean isValid(long lCreditCardIn)
	{
		intEvenSum = sumDoubleSecondDigits(lCreditCardIn);
		intOddSum = sumOddDigits(lCreditCardIn);

		intCheckSum = (intEvenSum + intOddSum) % 10;

		if( intCheckSum == 0 )
		{
			boolIsValid = true;
		}

		return boolIsValid;

	}//end isValid method

	//Method that will validate the credit card that was passed to the constructor that accepted the Credit Card Number
	public boolean isValid()
	{
		//*****NOT SURE WHAT I NEED HERE*****
		return boolIsValid;

	}//end isValid method2


Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,858
  • Joined: 27-December 08

Re: Luhn Algorithm - problem getting sum of double every 2nd digit

Posted 02 May 2012 - 09:01 PM

Quote

Code a method called: public boolean isValid() that will validate the Credit Card that was passed to the constructor that accepted the Credit Card number.

We're at a loss for all of your code, but this instruction is stating you should be using good OO practices. Specifically, the instance variable for the credit card number should be stored in your class. Pass that to your isValid(long lCreditCardIn) from your isValid() method.
Was This Post Helpful? 1
  • +
  • -

#7 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Luhn Algorithm - problem getting sum of double every 2nd digit

Posted 02 May 2012 - 10:46 PM

View PostMrsQ, on 03 May 2012 - 02:42 PM, said:

Thanks! ... Now on to the next problem I'm having...

I need to

o Code a method called: public boolean isValid(long lCreditCardIn ) that will validate the Credit Card that is passed to it.
o Code a method called: public boolean isValid() that will validate the Credit Card that was passed to the constructor that accepted the Credit Card number.
o HINT: Donít put the credit card validation logic into both of the overloaded isValid methods. How can you share the code?

Here is what I have. As you can see, I'm at a loss for what the instructor means for the second method...

Again, any help would be greatly appreciated!

	

	//Method that will validate the credit card that was passed to the constructor that accepted the Credit Card Number
	public boolean isValid()
	{
		//*****NOT SURE WHAT I NEED HERE*****
		return boolIsValid;

	}//end isValid method2


To return boolIsValid you need to store it in scope accessible by the method.
Was This Post Helpful? 1
  • +
  • -

#8 MrsQ  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 29
  • Joined: 20-September 08

Re: Luhn Algorithm - problem getting sum of double every 2nd digit

Posted 03 May 2012 - 11:47 AM

Thanks for your feedback all!

Between your feedback and a little re-reading on my part, I figured it out. A brief chat with the professor confirms :-)

Here is my revised code, hopefully it will be helpful to anyone with a similar question.
     private long lCreditCard = 0; //This was coded previously, I just hadn't included it with my post

	//Code the constructor to accept the credit card number to the class:  (Also coded previously)
	public validateCreditCard()
	{

	}


	public validateCreditCard(long lCCNumber)
	{

		lCreditCard = lCCNumber;
		boolIsValid = isValid(lCreditCard);

	}
//Method that will validate the credit card that is passed to it
	public boolean isValid(long lCreditCardIn)
	{
		intEvenSum = sumDoubleSecondDigits(lCreditCardIn);
		intOddSum = sumOddDigits(lCreditCardIn);

		intCheckSum = (intEvenSum + intOddSum) % 10;

		if(intCheckSum == 0)
		{
			boolIsValid = true;
		}

		else
		{
			boolIsValid = false;
		}
		return boolIsValid;

	}//end isValid method

	//Method that will validate the credit card that was passed to the constructor that accepted the Credit Card Number
	public boolean isValid()
	{
		boolIsValid = isValid(lCreditCard);

		return boolIsValid;
	}//end isValid method2


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1