Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write Java programs that use command-line input and call the following integer f

ID: 3853516 • Letter: W

Question

Write Java programs that use command-line input and call the following integer functions. All calculations for each function should be encapsulated within the function. Use a long datatype instead of int for all input and output variables. Do NOT use string variables in any of the algorithms.

1. SumOfDivisors: The input variable X is a positive integer. The function should return as Y the sum of the divisors of X. For example, if X is 10, then Y is 1+2+5+10=18. If X is 23, then Y is 24. Whenever Y = 2X, X is called a perfect number. Test your function with the following inputs:

X = 6 X = 277 X = 3443 X = 8128 X = 298760

2. ReverseDigits: The input variable X is a positive integer. The function should return as Y an integer containing the digits of X in reverse order. Leading zeros do not appear in X or Y. For example, if X is 2645, the Y is 5462. If X is 2400, then Y is 42. If Y = X, then X is called a palindrome (symmetric). Test your function with the following inputs:

X = 45600 X = 62826 X = 157910 X = 483047 X = 901108

3. GCD: The function has two input variables X1 and X2, which are positive integers. The function should return as Y the greatest common divisor (GCD) of X1 and X2, calculated using theEuclidean algorithm (non-recursive version). Test your function with the following inputs:

X1 = 136 X2 = 120

X1 = 512 X2 = 2217

X1 = 1595 X2 = 4255

X1 = 18684 X2 = 49878

X1 = 237732 X2 = 819918

4. BaseB: The function has two integer input values X and B, with variable X between 1 and 100,000 and base B between 2 and 10. The function should return as Y the value of X expressed as a base B number. Test your function with the following inputs:

X = 359 B = 2

X = 632 B = 3

X = 757 B = 5

X = 981 B = 8

X = 8046 B = 10

5.RecursiveSeq: The input variable X is an integer between 1 and 50. The function should return as Y the X-th term of a sequence defined recursively by:

f(1) = 1

f(2) = 3

f(X) = 2*f(X-1)–2*f(X-2) for X = 3,4,5,...

Your function code should use recursion(not a loop). Test your function with the following inputs:

X = 2 X = 10 X = 24 X= 33 X = 40

For each of the above functions, state whether or not the function is one-to-one. Justify youranswers. If a function is not one-to-one, show two inputs that have the same output

This is the beginning of one:

// Program to calculate the sum of the divisors
// of a positive integer.
public class P1PartB1
{
   pulblic static void main (String args[])
   {
       long X, Y;
       if(args.length >=1)
       {
           x = Long.parseLong(args[0]);
           if(X >=1){
                   System.out.println(" X = " + X + " Y = " + Y);
           }
       }
return;
}
public static long SumOfDivisors(long x){
   long y = 0;
// ...put code here...

   return y;

   }
} // end class

Explanation / Answer

I usually distribue stuff amongst different files. Its convinient for testing. I used IntegerFunctionsDriver class for unit testing.

IntegerFunctionsDriver.java

====

public class IntegerFunctionsDriver {
    public static void main(String[] args) {      
      
        System.out.println();
  
        /**
         * tests for IntegerFunctions.sumOfDivisors();
         */
        long number;
      
        // test - 1
        number = 10;
        assert IntegerFunctions.sumOfDivisors(number) == 18;
        System.out.print(".");
      
        /**
         * tests for IntegerFunctions.revNumber();
         */
        // test - 1
        number = 12345;
        assert IntegerFunctions.revNumber(number) == 54321;
        System.out.print(".");
        // test - 2
        number = 45600;
        assert IntegerFunctions.revNumber(number) == 654;
        System.out.print(".");
      
        /**
         * tests for IntegerFunctions.gcd();
         */
        long a, b;
       
        // test - 1      
        a = 81;
        b = 153;
        assert IntegerFunctions.gcd(a, b) == 9;
        System.out.print(".");      
              
      
        System.out.println(" ");
    }
}

IntegerFunctions.java

====

public class IntegerFunctions {
    /**
     * Returns the sum of divisors.
     * @param   {long} n    Number to compute on.
     * @return {long} Sum of divisors.
     */
    public static long sumOfDivisors(long n) {
        long sum = 0;
  
        for (int i = 1; i < n+1 ; ++i) {
            if (n%i == 0) {
                //System.out.println(n + " is divisible by " + i);
                sum += i;
            }
        }
      
        return sum;
    }

    /**
     * Reverses a number.
     * @param   {long} n   Number to reverse.
     * @return {long} Reversed number.
     */  
    public static long revNumber(long n) {
        long revnum = 0;
      
        while (n > 0) {
            revnum = (n*10) + (n%10);
            n /= 10;
        }
      
        return revnum;
    }
  
    /**
     * GCD of a number
     * @param   {long} a   First number to find GCD of.
     * @param   {long} b   Second number to find GCD of.   
     * @return {long} GCD of given number.
     */
    public static long gcd(long a, long b) {
        long res;
    
        while (b > 0) {
            res = a % b;
            a = b;
            b = res;
        }
      
        return a;
    }
   
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote