Question

JAVA3. Write a recursive method to compute: xº + x1 + x2 + ... + xn 4. Write a recursive method for sum of the non-negative even

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

3)

public static double compute(double x, int n) {
    if (n == 0) {
        return 1;
    }
    return Math.pow(x, n) + compute(x, n-1);
}

4)

public static int sum(int n) {
    if (n == 0) {
        return n;
    }
    if (n % 2 == 0) {
        return n + sum(n-2);
    } else {
        return sum(n-1);
    }
}
public static void printEven(int n) {
    if (n == 0) {
        System.out.println(0);
    }
    if (n % 2 == 0) {
        System.out.println(n);
        printEven(n-2);
    } else {
        printEven(n-1);
    }
}
Add a comment
Know the answer?
Add Answer to:
JAVA 3. Write a recursive method to compute: xº + x1 + x2 + ... +...
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
  • ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...

    ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and including the given value. This function has to be recursive; you may not use loops! For example: Test Result print('%d : %d' % (5, sum_up_to(5))) 5 : 15 Q2, Write a recursive function that counts the number of odd integers in a given list. This function has to be recursive; you may not use loops! For example: Test Result print('%s : %d' % ([2,...

  • Write a complete Java program, including comments in both the main program and in each method,...

    Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • Using Java, write a recursive method that receives only one parameter, an int n, and prints...

    Using Java, write a recursive method that receives only one parameter, an int n, and prints the cubed of the numbers from 1 to n3 in descending order. Do not use any global variables or stacks. For example, passing this method the value 5, the output would be “125 64 27 8 1”. Because 13 = 1, 23 = 8, 33 = 27, 43 = 64 and so on.

  • This is in Java. Write the application, TheSentinel  to read a set of integers. Stop reading input...

    This is in Java. Write the application, TheSentinel  to read a set of integers. Stop reading input when a non-integer is read. Use this exact prompt: System.out.print("Enter an integer or Q to quit: "); Note: you will actually quit on any non-integer. Do the following things: Find and print the sum of all the even numbers Find and print the smallest of the inputs Determine if the number 7 is in the input. If 7 is in the inputs, print "7...

  • Please write in java Write a main program that runs the following two recursive methods demonstrating...

    Please write in java Write a main program that runs the following two recursive methods demonstrating that they work. #1) Write a recursive method writeSquares that accepts an integer n and prints the first n squares with the odd squares in decending order, followed by the even squares in acending order. For example • writeSquares(8): prints . 49 25 9 1 4 16 36 64 • writeSquares(11); prints 121 81 49 25 9 1 2 16 36 64 100 •...

  • *In JAVA please* Tasks This lab has two parts: Write a recursive method that converts a...

    *In JAVA please* Tasks This lab has two parts: Write a recursive method that converts a decimal number to a different base number system. Print out the results after testing your method on a few different inputs. Task 1 – Recursive Method Create a recursive method that returns a given number converted from base ten to a given other base number system ranging from two to thirty-six. A decimal number, or base ten number, can be expressed in any other...

  • Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First...

    Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First class named Recursion has the following three recursive methods: // PRECONDITION n is positive integer. Method sumTerms returns sum of terms that are // reciprocal values of first n integers, with  alternating signs.      // For example,  sumTerms(7) returns  the following:  1/1 – 1/2  + 1/3 – 1/4  + 1/5 -1/6 + 1/7. // Terms with an odd denominator have positive sign, and terms with even denominator have // negative sign.  ...

  • Write a java program to print all the powers of 2 below a certain number and...

    Write a java program to print all the powers of 2 below a certain number and calculate their sum Requirements: 1. Accept the upper limit from the user as an integer 2. Make sure the sum variable is of the “long” type. You can assume that the test output will be less than LONG MAX. 3. Print all the numbers as a running sum, and finally print their sum. Please try and restrict yourself to loops, selection statements, and calls...

  • **Write In JAVA Please!!** 2. Write a program to apply the Modified Newton's Method Tn-1 to...

    **Write In JAVA Please!!** 2. Write a program to apply the Modified Newton's Method Tn-1 to the equation ()3r2 +4 0 starting with o 3. Use m and 2 and make separate numerical runs. In each case, set the maximum number of iterations to 25, but stop the computation when the backward error, i.e. f(n), is less than 10-12, Print all intermediate points xn and backward errors f(xn). Verify the convergence rates of your numerical solutions in both cases. (For...

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