# 3. Credit card numbers follow certain patterns. A credit card numbe...

## Question

3. Credit card numbers follow certain patterns. 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, and 6 for Discover cards. In 1954, Hans Luhn of IBM proposed the following algorithm for validating credit card numbers:
a. Double every second digit from right to left (e.g., if number is 3 => 3 * 2 => 6) and add them together.
b. If this doubling results in a two-digit number, then add the two digits to get a single-digit number (e.g., if number is 5 => 5 * 2 => 10 => 1+0 => 1).
So, for the credit card number 4388576018402626, doubling all second digits from the right results in (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).
This totals to 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37. Add all digits in the odd places from right to left.
The leftmost digit of the credit card number is at index 0; 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38.
Add results from steps (a) and (b) and see if divisible by 10. If it is, then the card number is valid; otherwise invalid. 37 + 38 = 75 is not divisible by 10, so it is an invalid credit card number.
Implement Luhn’s algorithm in a program to determine whether a given credit card number is valid or not. You must test if the number of digits in the input is in the valid range (13 to 16), run Luhn’s algorithm to test its validity, and if it is valid, print the name of the company that offers that credit card number.

5. Create three classes: Village, Citizen, and ComputeIntellect.
The Village class has an instance variable called numberOfCitizens and an array that holds a maximum of 100 Citizen objects.
The Citizen class has citizenId and educationalQualification as instance variables.
The ComputeIntellect class has a distributionOfQualification() method.
Create 100 Citizen objects using citizenId for the range [1 to 100]. Randomly generate the educational qualification in the range [1 to 4], where 1 = high school, 2 = undergraduate, 3 = postgraduate, and 4 = doctorate.
Store these 100 objects in a Village object using an array – any array of your choice.
The distributionOfQualification() method loops through the 100 objects and counts the number of citizens corresponding to each of the four educational qualifications.

7. Create a Person class that includes the name of the person, the weight of the person (in pounds), and the height of the person (in inches). For the data listed in the table below, create four Person objects. Compute their individual body mass index (BMI) and store it as part of these objects. Further, determine their weight category (see below) and add that information as part of the object as well. Store each of these four Person objects, their corresponding BMI, and weight category in a different ArrayList and develop get and set methods to access elements in that ArrayList.

Name, Weight (pounds), Height (inches)
Andrew 125.5 55.1
Boyd 150.0 67
Cathy 135 72.3
Donna 190 64

BMI is calculated using the following formula: BMI = weight(lb) * 703 / (height(in))²

BMI can indicate the following categories:
• Underweight when BMI is less than 18.5
• Normal weight when BMI is between 18.5 and 25
• Overweight when BMI is between 25 and 30
• Obese when BMI is 30 or greater

9. Create a 10x10 matrix as a 2D array. See a sample array below.
0 1 2 3 4 … 9
0 R1[0, 0] [0, 1] [0, 2] [0, 3] [0, 4] … [0, 9]
1 [1, 0] [1, 1] [1, 2] [1, 3] [1, 4] … [1, 9]
2 [2, 0] [2, 1] [2, 2] [2, 3] [2, 4] … [2, 9]
3 [3, 0] [3, 1] [3, 2] [3, 3] [3, 4] … [3, 9]
4 [4, 0] [4, 1] [4, 2] [4, 3] [4, 4] … [4, 9]
… … … … … … … …
9 [9, 0] [9, 1] [9, 2] [9, 3] [9, 4] … [9, 9]

Assume that a robot is placed in position [0, 0]. Now randomly generate a move. The move could take the robot to one of the eight possible adjacent slots – {up, down, left, right, left-up-corner, left-down-corner, right-up-corner, and right-down-corner} – these slots are represented by {1, 2, 3, 4, 5, 6, 7, 8}. However, at [0, 0], the robot only has three possible slots to move to – right, down, right-down-corner.
Create another robot called R2 and place it on [9, 9].
Now randomly generate an integer in the range of [1 to 8]. This first random integer corresponds to a possible move for Robot R1. If the move is valid, then move R1 to its new slot. A move is invalid if it takes the robot out of bounds of the [10x10] matrix. If the move is invalid, then keep generating random integers until a valid move is found.
Repeat this procedure for the second Robot R2.
If both R1 and R2 are in the same slot, then stop, print the final slot, print the sequence of random numbers that led R1 to this slot, and the print the sequence of random numbers that led R2 to the same slot.
Implement this program with a Robot class and a MovingRobot subclass

## Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

package Assignment2;

import java.util.Scanner;

public class CreditCard {

private int evenSum;
private int oddSum;
private int sum;
private String ccNumber;
private String company;

/**
* Create an instance of class CreditCard. Initialize instance
* ccNumber variable to value of the argument passed through the constructor parameter
*/
public CreditCard(String num) {
ccNumber = num;
}

/**
* This method checks if first digits belong to digits of known card companies
*/
public boolean validateCompany() {
char[] cc = ccNumber.toCharArray();
if(cc[0] == '4' || cc[0] == '5' || (cc[0] == '3' && cc[1] == '7') || cc[0] == '6')
return true;
else
return false;
}

/**
* This method checks if the entered credit card number is between 13 and 16 characters.
*/
public boolean validateLength() {
int l = ccNumber.length();
if(l == 13 || l == 14 || l == 15 || l == 16)
return true;
else
return false;
}

/**
* This method checks if the entered credit card number consists of numbers only. Returns false if there are other characters.
*/
public boolean validateNumber() {
int countNum = 0;
char[] cc = ccNumber.toCharArray();
for(int i = 0; i < cc.length; i++) {
if(cc[i] >= '0' && cc[i] <= '9')
countNum++;
}
if(countNum == cc.length)
return true;
else
return false;
}

/**
* This method validates odd and even sums based on text problem requirements. Returns false if the sum is equal to 0.
* That's because a credit card number cannot be entered as 000000000000000, for example.
*/
public boolean validateSums() {
char[] cc = ccNumber.toCharArray();
int[] num = new int[cc.length];

for (int i = 0; i < cc.length; i++)
num[i] = Character.digit(cc[i], 10);

for(int i = cc.length - 2; i >= 0; i -= 2) {
int doubling = num[i] * 2;
if(doubling >= 10)
evenSum += doubling / 10 + doubling % 10;
else
evenSum += doubling;
}

for(int i = cc.length - 1; i >= 0; i -= 2)
oddSum += num[i];

sum = evenSum + oddSum;

if(sum == 0)
return false;
else
return true;
}

/**
* This method checks if the previously found sum is divisble by 10.
*/
public boolean isDivisibleBy10() {
if(sum % 10 == 0)
return true;
else
return false;
}

/**
* This method checks if all previously defined conditions are satisfied (are true).
* If at least one of them is false, returns false.
*/
public boolean isValid() {
if(validateCompany() && validateLength() && validateNumber() && validateSums() && isDivisibleBy10())
return true;
else
return false;
}...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

\$55.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

### Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.