In: Computer Science
I need Java Code for both questions....thx
Question 1
Consider the sequence of integers defined as follows:
The first term is any positive integer.
For each term n in the sequence, the next term is computed like this: If n is even, the next term is n/2. If n is odd, the next term is 3n+1.
Stop once the sequence reaches 1.
Here are a few examples of this sequence for different values of
the first term:
• 8,4,2,1
• 12,6,3,10,5,16,8,4,2,1
• 19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
Note that all three of these eventually do reach 1. In fact, it is believed (but not known) that the sequence will always reach 1, regardless of the first term chosen. This is known as the Collatz conjecture. Despite its apparent simplicity, it has so far eluded all attempts at a proof. Mathematician Jeffrey Lagarias at the University of Michigan has claimed that “this is an extraordinarily difficult problem, completely out of reach of present day mathematics.”
We might not be able to prove the Collatz conjecture here, but we can experiment computationally with it! Within your Lab4HW folder, write a program named Collatz.java that allows the user to enter any positive integer. The program should then compute and list all the terms of the sequence until it reaches 1. At the end, show how many terms it took to get to 1 (including 1 itself).
1
Here are some examples of what your completed program might look like when you run it. Underlined parts indicate what you type in as the program is running.
Example 1
Enter starting value (must be a positive integer): 5 5 16 8
4 2 1 Number of terms: 6
Example 2
Enter starting value (must be a positive integer): 12 12 6 3
10 5 16 8 4 2 1 Number of terms: 10
Example 3
Enter starting value (must be a positive integer): 1 1 Number of terms: 1
Question 2
Credit card numbers are not completely random sequences; they follow certain rules depending on the card issuer. A MasterCard number must meet these criteria:
• Begin with 51, 52, 53, 54, 55, or something in the range
222100-272099
• 16 digits in length
• Satisfy the Luhn formula, created by IBM scientist Hans Peter
Luhn in the 1950s
Here’s how the Luhn formula works:
Double every other digit going backwards, starting from the next-to-last digit.
For each of the doubled values that exceed 9, subtract 9.
Add up all the doubled values, along with the digits that were not doubled.
If the result is a multiple of 10, the number satisfies the Luhn formula. If the result is not a multiple of 10, the number does not satisfy the Luhn formula.
For example, 2221008763790559 is a valid MasterCard number. (Don’t worry, this was randomly gener- ated and most likely doesn’t actually belong to anyone :) You can easily verify that the number begins with 222100 and is 16 digits long. To check whether it satisfies the Luhn formula:
Original number:
2221008763790559
Double every other digit going left, starting from the next-to-last digit:
4 2 4 1 0 0 16 7 12 3 14 9 0 5 10 9
For every doubled value that exceeds 9, subtract 9:
4241007733590519
Finally,addupallthemodifieddigits: 4+2+4+1+0+0+7+7+3+3+5+9+0+5+1+9=60, which is indeed a multiple of 10.
Within your Lab4HW folder, write a program named MasterCardValidator.java that allows the user to enter a credit card number. The program should then determine and print whether that number is a valid MasterCard number according to the criteria above. You can use the randomly generated MasterCard numbers from https://www.freeformatter.com/credit-card-number-generator-validator.html to help you test.
Hint: There are several ways you can read the number from the user, but I recommend reading it as a string. You can then use strName.charAt(i) to get the individual digits. However, these are treated as char values rather than int values. To convert to int, you can use one of the following:
• For a single digit: Integer.parseInt("" + strName.charAt(i)), or strName.charAt(i) - ’0’
• For multiple digits:
Integer.parseInt(strName.substring(startIndex, endIndex))
Here are some examples of what your completed program might look
like when you run it. Underlined
parts indicate what you type in as the program is running.
Example 1
Enter a card number for validation: 2221008763790559 Valid
Example 2
Enter a card number for validation: 2221018763790559 Invalid
Example 3
Enter a card number for validation: 55 Invalid
Following is the answer:
Question1:
import java.util.*;
public class Main
{
static int count = 0;
public static void sequence(int number){
if(number != 1){
if(number%2 == 0){
count++;
System.out.println(number);
sequence(number/2);
}else{
count++;
System.out.println(number);
sequence(3*number+1);
}
}else{
System.out.println(number);
count++;
}
}
public static void main(String[] args)
{
Scanner sc = new
Scanner(System.in);
System.out.println("Enter starting
value (must be a positive integer): ");
int number = sc.nextInt();
sequence(number);
System.out.println("Number of terms: " + count);
}
}
Output:
Question2:
import java.util.*;
public class Main
{
public static void validateCard(String card){
if(card.length() == 16){
if(Integer.parseInt(card.substring(0, 6)) >= 222100 &&
Integer.parseInt(card.substring(0, 5)) <=272099){
int[] array = new int[card.length()];
int total = 0;
for(int i = 0 ; i < card.length() ; i++){
if(i%2 == 0){
int sum = Integer.parseInt("" + card.charAt(i)) * 2;
if(sum > 9){
array[i] = sum - 9;
total+= sum-9;
}else{
array[i] = sum;
total+= sum;
}
}else{
array[i] = Integer.parseInt("" + card.charAt(i));
total+=Integer.parseInt("" + card.charAt(i));
}
}
if(total%10 == 0){
System.out.println("Valid");
}else{
System.out.println("Invalid");
}
}
}else{
System.out.println("Invalid");
}
}
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter a card
number for validation: ");
String card = sc.next();
validateCard(card);
}
}
Output: