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
  • Using Java IDE Write a recursive method which takes an integer number and returns the sum...

    Using Java IDE Write a recursive method which takes an integer number and returns the sum of the numbers from 1 to that number. The method must solve the problem recursively.Then write an application which calls the method with a few different numbers and displays the return value of the method.

  • ****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...

  • note: the language should be java on netbeans. please solve the question complete. Objective: The students...

    note: the language should be java on netbeans. please solve the question complete. Objective: The students will learn how to work with recursion and improve their problem solving skills. Problem 1: Create an interactive Java Application that has a method to calculate the value of e x = 1 + (x / 1!) + (x2 / 2!) + (x3 / 3!) + … + (xn / n!) Where n and x are supplied by the user. Your must call 2...

  • 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...

  • 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...

  • 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.

  • In Java Write a recursive method called sumUpto(n) which will return the sum of the first...

    In Java Write a recursive method called sumUpto(n) which will return the sum of the first n integers. For example, sumUpto(4) will return 10 because 1+2+3+4 = 10. sumUpto(5) will return 15 because 1+2+3+4+5=15. So you can see that sumUpto(5) = sumUpto(4) + 5. Make this more general for n : sumUpto(n) = sumUpto(??) + n Figure out the base case and write the method……….. import java.util.Scanner; public class Lab12Num1 { public static int sumUpto(int num) {    } public...

  • 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 •...

  • Using your choice of language, C#, Java, or pseudocode, write a recursive method that receives only...

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

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