Question

Create, compile and run the JAVA program. Use the methods concepts discussed in the class to...

Create, compile and run the JAVA program. Use the methods concepts discussed in the class to develop the program. This assignment is an individual assignment – so has to be done individually by each student. Please submit the JAVA file without any compilation error. Please submit the assignment using blackboard. FYI: - NO USE OF ARRAYS or any type of collection variables. Credit card validation Problem. A credit card number must have between 13 and 16 digits. It must start with: 4 for visa cards 5 for master cards 37 for American express cards 6 for discover cards. This Code must be simple and beginner friendly.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


import java.util.*;

public class card {

public static void main(String[] args) {

System.out.println("Enter the card number:");
//Scanner class is used to read the input from user
Scanner s = new Scanner(System.in);
//s.nextLong is used to read long int
//Here long is used because card number must have 13 to 16 digits.To store long number we habe to use long
long num = s.nextLong();
long temp = num;
int NoOfDigits = 0,card_num;
//loop to count the number of digits
//Example temp = 123
//First iteration
//temp = 123/10 = 12
//NoofDigits = NoOfDigits + 1 = 0 + 1 = 1
//Second iteration: temp = 12
//temp = 12 /10 = 1
//NoofDigits = NoOfDigits + 1 = 1 + 1 = 2
//Third iteration: temp = 1
//temp = 1/10 = 0
//NoofDigits = NoOfDigits + 1 = 2 + 1 = 3
//temp > 0 = 0> 0, condition fails.So loop terminated.
//NoOfDigits = 3
while(temp > 0)
{
       temp = temp / 10;
       NoOfDigits = NoOfDigits + 1;
}
//Condition to check NoOfDigits is less than 13 and greater than 16
if(NoOfDigits <13 || NoOfDigits > 16)
{
    System.out.println("Invalid card number.Enter card number digits between 13 to 16.Enter digits are :"+NoOfDigits);
   
}
//if NoOfDigits are in between 13 and 16 this condition will execute.
else
{
    //Math.pow(x,y) - means power of x and y(x^y)
    //To get the first digit of a number we can use below formula
    //Example : num = 123456
    //To get the first digit we have to divide the num with 100000
    //'/' gives the quotient and '^' - power of
    //123456/100000 = 1
    //So num / 10 ^ (digits - 1) gives first digit
    //To get first and second
    //num / 10 ^ (digits - 2)
    //(int) is used to convert to type int and (long) is used convert the type into long
    card_num = (int)(num /(long)Math.pow(10,NoOfDigits-1));
    //condition to check visa card
    if(card_num == 4)
    {
      System.out.println("This card is visa card");
    }
    //condition to check master card
    else if(card_num == 5)
    {
      System.out.println("This card is master card");
    }
    //condition to check discover card
    else if(card_num == 6)
    {
      System.out.println("This card is discover card");
    }
    //condition to check American Express card
    else if(card_num == 3)
    {
      card_num = (int)(num /(long)Math.pow(10,NoOfDigits-2));
     
      if(card_num == 37)
      System.out.println("This card is American express card");

else

  System.out.println("Invalid card.There is no matching digits with given card number");
    }
    //Invalid card number
    else
    {
      System.out.println("Invalid card.There is no matching digits with given card number");
    }
}
}
}

card.java X 3 import java.util.*; 5 public class card { 7 public static void main(String[] args) { 10 11. 12 13 14 16 17 18 Scard.java X 30 31. 32 33 34 35 while(temp > 0) { temp = temp / 10; NoofDigits = NoOfDigits + 1; } [/Condition to check NoOfDiif(card_num == 4) System.out.println(This card is visa card); // condition to check master card else if(card_num == 5), Syselse if(card_num == 6), System.out.println(This card is discover card); //condition to check American Express card else if(Enter the card number: 4567891567890123 This card is visa cardEnter the card number: 567891235686789 This card is master cardEnter the card number: 61336789677898 This card is discover card,Enter the card number: 379890124567898 This card is American express cardEnter the card number: 12345678912345678 Invalid card number. Enter card number digits between 13 to 16. Enter digits are : 1Enter the card number: 12345678914567 Invalid card. There is no matching digits with given card number

Add a comment
Know the answer?
Add Answer to:
Create, compile and run the JAVA program. Use the methods concepts discussed in the class to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter...

    REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter a credit card number. Display whether the number is valid and the type of credit cards (i.e., Visa, MasterCard, American Express, Discover, and etc). The requirements for this assignment are listed below: • Create a class (CreditNumberValidator) that contains these methods: // Return true if the card number is valid. Boolean isValid(String cardNumber); // Get result from Step 2. int sumOfDoubleEvenPlace(String cardNumber); // Return...

  • Objective: To implement the programming languages features discussed in class and to develop a program that...

    Objective: To implement the programming languages features discussed in class and to develop a program that uses Graphical User Interfaces (GUI) that provides a friendly environment for users. Project Assignment Design and implement a Hotel Reservation System. The hotel has two types of rooms. One is regular room that has two beds. Another is deluxe room that has two beds and a safe. The regular room price is $120 per night. The deluxe room is $130 per night. A safe...

  • Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit...

    Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit card number and determines whether it is valid or not. (Much of this assignment is taken from exercise 6.31 in the book) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits, and must start with: 4 for Visa cards 5 for Master cards 6 for Discover cards 37 for American Express cards The algorithm for determining...

  • I should use the array and loop to create a java program according to the instruction,...

    I should use the array and loop to create a java program according to the instruction, but I have no idea how to do it. Introduction This lab assignment continues to give you practice using loops, particularly loops with variable termination conditions, and it also provides you an opportunity to use one-dimensional arrays. Recall that an array is used to store a collection of data. The data can be values of Java primitive data types or else objects (for instance,...

  • Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns:...

    Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card...

  • Note: Please write the Pseudocode using Arrays method in Java language. Program 2 (40%); For this...

    Note: Please write the Pseudocode using Arrays method in Java language. Program 2 (40%); For this assignment, we're going to make a game. Imagine you initially start with a random "hand" of two cards (values 2-9). Your goal is to continue to add to your hand until the sum of the cards is between 21 25, at which point youu win. Further, if you hold 5 cards, you win. However, if you exceed a sum of 25, you lose. Admittedly,...

  • CIT 149 Java 1 programming question Use DrJava to compile the following try-catch program ? The...

    CIT 149 Java 1 programming question Use DrJava to compile the following try-catch program ? The Assignment ? Specifications General Structure your file name and class name on the following pattern: The first three letters of your last name (begin with upper case.). Then the first two letters of your first name (begin with upper case.). Follow this with the name of the program: TryCatch. For a student called ’John Doe,’ the class name and file name would be: DoeJoTryCatch...

  • *Use Java to create this program* For this assignment, you will be building a Favorite Songs...

    *Use Java to create this program* For this assignment, you will be building a Favorite Songs application. The application should have a list with the items displayed, a textbox for adding new items to the list, and four buttons: Add, Remove, Load, and Save. The Add button takes the contents of the text field (textbox) and adds the item in it to the list. Your code must trim the whitespace in front of or at the end of the input...

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • Class StringImproved public class StringImproved extends java.lang.Object This program replaces the string class with a mutable...

    Class StringImproved public class StringImproved extends java.lang.Object This program replaces the string class with a mutable string. This class represents a character array and provide functionalities need to do basic string processing. It contains an array of characters, representing a textual string. Overview In Java, Strings are a great device for storing arrays of characters. One limitation of Strings in Java, however (there is always at least one), is that they are immutable (ie. they cannot be changed once created)....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT