Question

In: Computer Science

I need Java Code for both questions....thx Question 1 Consider the sequence of integers defined as...

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
    

Solutions

Expert Solution

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:


Related Solutions

I need java code for this ..thx Question 2 Credit card numbers are not completely random...
I need java code for this ..thx 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...
I need an idea of Java code that will convert an integer (1 to 3,999) into...
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
Java question: I need to fix a point class (code below) Thank you! /** * A...
Java question: I need to fix a point class (code below) Thank you! /** * A point, implemented as a location without a shape. */ public class Point extends Location { // TODO your job // HINT: use a circle with radius 0 as the shape! public Point(final int x, final int y) { super(-1, -1, null); assert x >= 0; assert y >= 0; } }
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
In Java, we typically iterate over a sequence of integers as follows: for (int i=0; i<10;...
In Java, we typically iterate over a sequence of integers as follows: for (int i=0; i<10; i = i + 2) { ... } Suppose we use a version of Java that only supports for-each loops, i.e. it only allows you to iterate through the elements of an Iterable In order to still be able to iterate over a sequence of numbers, you need to create such an Iterable, which we will call Range. Modify the class Range.java so that...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
Java Program Sequential Search You are given a sequence of n integers S and a sequence...
Java Program Sequential Search You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S. Do not use any import sort packages. Input: In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers are given. Output:...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise. A sequence of values is Fibonnaci if every third value is equal to sum of the previous two. Eg., 3,4,7,11,18,29 is a Fibonacci sequence whereas 1,2,3,4 is not, because 2+3 is not equal to 4....
(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT