Please make it simple because I am taking Java one. ----------------------------
ID: 3679635 • Letter: P
Question
Please make it simple because I am taking Java one.
---------------------------------------------------------------------------------------------------------------------
Introduction: This assignment helps you to reinforce the topics discussed in the class including
a) Relational operators, Boolean operators, data comparison
b) Selection
c) Repetition
d) And problem solving using selection and repetition
Important: This is an individual assignment. Please do not collaborate.
Grading policy: We will check
a) The correctness of your program/solution
b) Variable naming (self-describing)
c) Identification of proper data type
d) Appropriate commenting and Indentation.
Credit Card Number Verification Program:
For example, say the card number is 1234 5678 6789 1235 and its visa card. To validate it, first
add all the digits
1+2+3+4+ 5+6+7+8+ 6+7+8+9 +1+2+3+5 = 77 then get mod 10 of 77 (77%10 = 7) Since it’s
not zero, this is not a valid visa card number
First your program should ask if the credit card
is MasterCard or visa card. Then ask the 16 digit
credit card number. Then add all the digits in the
credit card number and get the modulo 10. If the
modulo 10 of the summation of all the digits is
zero, then its valid visa card. If the modulo 10 of
the summation of all the digits is 1 then its valid
MasterCard. Otherwise, the number customer
has entered is an invalid card number.
Write a Java program that reads the card type and the card number and then determine if the card
number entered is a valid card number. If the card number is a valid card, display the message
“valid card number”. Otherwise, display the message “invalid card number”.
Save your program as Assignment4.java.
Requirements:
A) You need to read the card number as a single string and each 4 numbers in the card must
be separated by a space. For example, following is a correct input
1234 5678 6789 1235
B) Card number must have 16 digits. If not, display the error message “incorrect card
number”
You may need to use some methods in java string class defend @
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
Also, you can convert a string to an integer using following technique
String myString = “5”;
int i = Integer.parseInt(myString);
I will have 5 after executing above statements.
Explanation / Answer
import java.util.Scanner;
public class Assignment4 {
public static int digitSum(int n){
int sum =0;
while(n > 0){
sum = sum + n%10;
n= n/10;
}
return sum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//reading card number
String cardNumber;
System.out.println("Enter card number");
cardNumber = sc.nextLine();
// reading card type
char cardType;
System.out.print("Enter type of card(M- MasterCard, V- visa): ");
cardType = sc.next().charAt(0); // input taking as string and extracting first character
sc.close();
int sum = 0;
// spliting cardNumber string by ' '(space): 1234 5678 6789 1235
for(String s : cardNumber.split("\s+")){
int number = Integer.parseInt(s); // converting into string
sum = sum + digitSum(number);
}
if((cardType == 'M') && (sum%10==1)){
System.out.println("Master card is valid");
}
else if((cardType == 'V') && (sum%10==0)){
System.out.println("Visa card is valid");
}
else
System.out.println("Invalid card");
}
}
/*
Output:
Enter card number
1234 5678 6789 1235
Enter type of card(M- MasterCard, V- visa): V
Invalid card
*/