23-09-2010, 07:39 PM
|
#568
|
تاريخ التسجيل: Jul 2008
كلية: كلية الحاسبات وتقنية المعلومات
التخصص: IT
نوع الدراسة: عضو هيئة تدريس
المستوى: متخرج
البلد: جــــدة
الجنس: ذكر
المشاركات: 2,477
|
رد: [cpcs 202 - برمجة 1] لديك سؤال , شيء غير مفهوم ,,, تفضل هنا , موضوع مفيد

السلام عليكم ورحمة الله وبركاته
أخي أحمد إن أمكن أتمنى ان تحل لي السؤال التالي بلغة الجافا وجزاك الله خيراً..
Credit card numbers follow certain pattern. A credit card number must be 16 digits, and must start with
• 4 for Visa cards
• 5 for Master cards
• 37 for American Express cards
• 6 for Discover cards
Suppose that you were asked to write down a Java program that validates the credit card number (if it is correct or not!). Here are the rules that determine the correctness of credit card number:
Suppose the credit card number = 4388576018402625
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*2 = 4
2*2 = 4
4*2 = 8
1*2 = 2
6*2 = 12 (1 + 2) = 3
5*2 = 10 (1 + 0) = 1
8*2 = 16 (1 +6) = 7
4*2 = 8
2) Now add all single-digit number from step 1 (4+4+8+2+3+1+7+8 = 37)
3) Add all digits in the odd places from right to left in the card number:
5+6+0+8+0+7+8+3 = 37
4) Sum the results from step 2 and 3
37 + 37 = 74
5) If the result in step 4 is divisible by 10, then credit card is valid, else it is not valid.
74 is not divisible by 10, so it is not valid!
Write a java program to accept a credit card number from keyboard as long, and then prints on screen, if it is valid or not. You should use the following:
public static int[] fillArray(long) : this method fills the credit card number in an array
public static int getSum(int[]) :calculated the summation from step 1 and 2
Public static int getOddSum(int[]): calculates the summation from step 3
Public static boolean isValid(int, int): determines if valid or not according to step 5.
|
وعليكم السلام ورحمة الله وبركاته .
تفضلي :

كود PHP:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.print("Please Enter the Number of Cridit Card : ");
Scanner sc = new Scanner(System.in);
long numberCard = sc.nextLong();
int[] digit = fillArray(numberCard);
if (digit.length != 16)
{
System.out.println("not valid.");
return;
}
Boolean isVaild = isValid(getSum(digit), getOddSum(digit));
if (isVaild)
{
System.out.println("valid.");
} else {
System.out.println("not valid.");
}
}
public static int[] fillArray (long x)
{
String txt = String.valueOf(x);
int digit [] = new int[txt.length()];
for (int i = 0; i < txt.length(); i++)
{
digit[i] = txt.charAt(i);
}
return digit;
}
public static int getSum(int[] digit)
{
int doubleEven [] = new int[digit.length / 2];
int sum = 0;
for (int i = 1, j = 0; i < digit.length; i += 2, j++)
{
doubleEven[j] = digit[i] * 2;
}
for (int i = 0 ; i < doubleEven.length ;i++)
{
sum += doubleEven[i];
}
return sum;
}
public static int getOddSum(int[] digit)
{
int oddSum[] = new int[digit.length / 2];
int sum = 0;
for (int i = 0, j = 0; i < digit.length; i += 2, j++) {
oddSum[j] = digit[i];
}
for (int i = 0; i < oddSum.length; i++) {
sum += oddSum[i];
}
return sum;
}
public static boolean isValid(int even, int odd)
{
int sum = even + odd;
if ((sum % 10) == 0 ) return true;
else return false;
}
}
بالتوفيق .
|
|
سبحان الله وبحمد ,,, سبحان الله العظيم الحمد لله كما ينبغي لجلال وجهه وعظيم سلطانه . اللهم صل على محمد وعلى آل محمد كما صليت على إبراهيم وعلى آل إبراهيم إنك حميد مجيد . اللهم بارك على محمد وعلى آل محمد كما باركت على إبراهيم وعلى آل إبراهيم إنك حميد مجيد.
|
التعديل الأخير تم بواسطة Mr.Ahmad ; 23-09-2010 الساعة 07:42 PM.
|
|
|